github.com/thetechnoweenie/graven@v1.0.2/util/executor.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  )
     9  
    10  func RunCommand(cwd string, env []string, cmd string, args ...string) (string, string, error) {
    11  	stdout := &bytes.Buffer{}
    12  	stderr := &bytes.Buffer{}
    13  
    14  	var c *exec.Cmd
    15  	c = exec.Command(cmd, args...)
    16  	c.Stdout = stdout
    17  	c.Stderr = stderr
    18  	c.Stdin = os.Stdin
    19  	c.Dir = cwd
    20  	c.Env = env
    21  
    22  	err := c.Run()
    23  	if err != nil {
    24  		return stdout.String(), stderr.String(), fmt.Errorf("Error running command: %v\n%v", cmd, err)
    25  	}
    26  
    27  	if !c.ProcessState.Success() {
    28  		return stdout.String(), stderr.String(), fmt.Errorf("Command exited in an error state: %v", cmd)
    29  	}
    30  	return stdout.String(), stderr.String(), err
    31  }