1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package util
import ( "bytes" "io" "os" "os/exec" "strings"
"github.com/buildkite/shellwords" "github.com/pkg/errors" )
func StartProcess(cmd string, path string, in io.Reader, out io.Writer, er io.Writer, env ...string) (*exec.Cmd, error) { args, err := shellwords.Split(cmd) if err != nil { return nil, err } if len(args) == 0 { return nil, errors.New("no arguments provided") } firstArg := args[0]
if !strings.Contains(firstArg, "/") && !strings.Contains(firstArg, "\\") { firstArg, err = exec.LookPath(firstArg) if err != nil { return nil, errors.Wrapf(err, "unable to look up cmd [%s]", firstArg) } }
if in == nil { in = os.Stdin } if out == nil { out = os.Stdout } if er == nil { er = os.Stderr }
c := &exec.Cmd{Path: firstArg, Args: args, Env: env, Stdin: in, Stdout: out, Stderr: er, Dir: path} err = c.Start() if err != nil { return nil, errors.Wrapf(err, "unable to start [%s] (%T)", cmd, err) } return c, nil }
func RunProcess(cmd string, path string, in io.Reader, out io.Writer, er io.Writer, env ...string) (int, error) { c, err := StartProcess(cmd, path, in, out, er, env...) if err != nil { return -1, err } err = c.Wait() if err != nil { ec, ok := err.(*exec.ExitError) //nolint:errorlint if ok { return ec.ExitCode(), nil } return -1, errors.Wrapf(err, "unable to run [%s] (%T)", cmd, err) } return 0, nil }
func RunProcessSimple(cmd string, path string) (int, string, error) { var buf bytes.Buffer ec, err := RunProcess(cmd, path, nil, &buf, &buf) if err != nil { return -1, "", err } return ec, buf.String(), nil }
|