github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/storage/provider/utils.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provider
     5  
     6  import (
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  	"github.com/juju/loggo"
    12  )
    13  
    14  var logger = loggo.GetLogger("juju.storage.provider")
    15  
    16  // runCommandFunc is a function type used for running commands
    17  // on the local machine. We use this rather than os/exec directly
    18  // for testing purposes.
    19  type runCommandFunc func(cmd string, args ...string) (string, error)
    20  
    21  // logAndExec logs the specified command and arguments, executes
    22  // them, and returns the combined stdout/stderr and an error if
    23  // the command fails.
    24  func logAndExec(cmd string, args ...string) (string, error) {
    25  	logger.Debugf("running: %s %s", cmd, strings.Join(args, " "))
    26  	c := exec.Command(cmd, args...)
    27  	output, err := c.CombinedOutput()
    28  	if err != nil {
    29  		output := strings.TrimSpace(string(output))
    30  		if len(output) > 0 {
    31  			err = errors.Annotate(err, output)
    32  		}
    33  	}
    34  	return string(output), err
    35  }