-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
57 lines (46 loc) · 1.15 KB
/
utils.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
package main
import (
"io"
"os/exec"
"syscall"
)
type execResult struct {
io.ReadCloser
Status int
Output []byte
readIndex int64
}
func (res *execResult) Close() error {
return nil
}
func (res *execResult) Read(p []byte) (n int, err error) {
if res.readIndex >= int64(len(res.Output)) {
err = io.EOF
return
}
n = copy(p, res.Output[res.readIndex:])
res.readIndex += int64(n)
return
}
func execShell(dir, cmd string) (res *execResult, err error) {
res = &execResult{}
sh := exec.Command("/bin/sh", "-c", cmd)
if dir != "" {
sh.Dir = dir
}
res.Output, err = sh.CombinedOutput()
if err != nil {
// Shamelessly borrowed from https://github.com/prologic/je/blob/master/job.go#L247
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
res.Status = status.ExitStatus()
}
}
}
return
}