github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/backups/exec.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups
     5  
     6  import (
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  )
    12  
    13  // runCommand execs the provided command. It exists
    14  // here so it can be overridden in export_test.go
    15  func runCommand(cmd string, args ...string) error {
    16  	command := exec.Command(cmd, args...)
    17  	out, err := command.CombinedOutput()
    18  	if err == nil {
    19  		return nil
    20  	}
    21  	if _, ok := err.(*exec.ExitError); ok && len(out) > 0 {
    22  		return errors.Errorf(
    23  			"error executing %q: %s",
    24  			cmd,
    25  			strings.Replace(string(out), "\n", "; ", -1),
    26  		)
    27  	}
    28  	return errors.Annotatef(err, "error executing %q", cmd)
    29  }