-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
51 lines (45 loc) · 981 Bytes
/
run.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
package exe
import (
"fmt"
"os/exec"
"strings"
"time"
)
func runWithSettings(cmd *exec.Cmd, setting ExecuteSetting) (out string, err error) {
var timer *time.Timer
exited := false
var errFromTimeout error
if setting.timeout != 0 {
timer = time.AfterFunc(setting.timeout, func() {
if !exited {
errFromTimeout = cmd.Process.Kill()
}
})
}
switch {
case setting.start && setting.output:
panic("can't do start with output")
case setting.start && !setting.output:
err = cmd.Start()
case !setting.start && setting.output:
var byteOut []byte
byteOut, err = cmd.Output()
if byteOut != nil {
out = strings.TrimSpace(string(byteOut))
}
default:
err = cmd.Run()
}
if errFromTimeout != nil {
if err != nil {
err = fmt.Errorf("error from timeout %v wraps err: %v", errFromTimeout, err)
} else {
err = fmt.Errorf("error from timeout: %v", errFromTimeout)
}
}
exited = true
if timer != nil {
timer.Stop()
}
return out, err
}