-
Notifications
You must be signed in to change notification settings - Fork 4
/
exec.go
74 lines (71 loc) · 1.75 KB
/
exec.go
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
73
74
package dads
import (
"bytes"
"io"
"os"
"os/exec"
)
// ExecCommand - execute command given by array of strings with eventual environment map
func ExecCommand(ctx *Ctx, cmdAndArgs []string, cwd string, env map[string]string) (sout, serr string, err error) {
command := cmdAndArgs[0]
arguments := cmdAndArgs[1:]
if ctx.Debug > 1 {
Printf("executing command %s:%v:%+v\n", cwd, env, cmdAndArgs)
}
cmd := exec.Command(command, arguments...)
if len(env) > 0 {
newEnv := os.Environ()
for key, value := range env {
newEnv = append(newEnv, key+"="+value)
}
cmd.Env = newEnv
}
var (
stdOut bytes.Buffer
stdErr bytes.Buffer
)
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
if cwd != "" {
cmd.Dir = cwd
}
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
sout = stdOut.String()
serr = stdErr.String()
if ctx.Debug > 1 {
Printf("executed command %s:%v:%+v -> (%v,%s,%s)\n", cwd, env, cmdAndArgs, err, StringTrunc(sout, 0x400, true), StringTrunc(serr, 0x400, true))
}
return
}
// ExecCommandPipe - execute command given by array of strings with eventual environment map, return STDOUT pipe to read from
func ExecCommandPipe(ctx *Ctx, cmdAndArgs []string, cwd string, env map[string]string) (stdOutPipe io.ReadCloser, cmd *exec.Cmd, err error) {
command := cmdAndArgs[0]
arguments := cmdAndArgs[1:]
if ctx.Debug > 1 {
Printf("executing non-blocking command %s:%v:%+v\n", cwd, env, cmdAndArgs)
}
cmd = exec.Command(command, arguments...)
if len(env) > 0 {
newEnv := os.Environ()
for key, value := range env {
newEnv = append(newEnv, key+"="+value)
}
cmd.Env = newEnv
}
if cwd != "" {
cmd.Dir = cwd
}
stdOutPipe, err = cmd.StdoutPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return
}
return
}