github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/utils/exec.go (about)

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