You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
605 B
34 lines
605 B
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
)
|
|
|
|
func ExecCmd(cmdName string, cmdArgs []string, workingDir string, environ []string) (string, error) {
|
|
|
|
cmd := exec.Command(cmdName, cmdArgs...)
|
|
var stdBuffer bytes.Buffer
|
|
mw := io.MultiWriter(os.Stdout, &stdBuffer)
|
|
|
|
cmd.Stdout = mw
|
|
cmd.Stderr = mw
|
|
|
|
if environ != nil {
|
|
cmd.Env = environ
|
|
}
|
|
if workingDir != "" && path.IsAbs(workingDir) {
|
|
cmd.Dir = workingDir
|
|
} else if workingDir != "" {
|
|
return "", errors.New("Working Directory must be an absolute path")
|
|
}
|
|
|
|
err := cmd.Run()
|
|
return stdBuffer.String(), err
|
|
|
|
}
|