forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
90 lines (83 loc) · 1.74 KB
/
cmd.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package cmder exec cmd and catch the result.
package cmder
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"time"
"unsafe"
)
var cmdArg [2]string
func init() {
if runtime.GOOS == "windows" {
cmdArg[0] = "cmd"
cmdArg[1] = "/c"
} else {
cmdArg[0] = "/bin/sh"
cmdArg[1] = "-c"
}
}
// Run exec cmd and catch the result.
// Waits for the given command to finish with a timeout.
// If the command times out, it attempts to kill the process.
func Run(cmdLine string, timeout ...time.Duration) *Result {
cmd := exec.Command(cmdArg[0], cmdArg[1], cmdLine)
var ret = new(Result)
cmd.Stdout = &ret.buf
cmd.Stderr = &ret.buf
cmd.Env = os.Environ()
ret.err = cmd.Start()
if ret.err != nil {
return ret
}
if len(timeout) == 0 || timeout[0] <= 0 {
ret.err = cmd.Wait()
return ret
}
timer := time.NewTimer(timeout[0])
done := make(chan error)
go func() { done <- cmd.Wait() }()
select {
case ret.err = <-done:
timer.Stop()
case <-timer.C:
if err := cmd.Process.Kill(); err != nil {
ret.err = fmt.Errorf("command timed out and killing process fail: %s", err.Error())
} else {
// wait for the command to return after killing it
<-done
ret.err = errors.New("command timed out")
}
}
return ret
}
// Result cmd exec result
type Result struct {
buf bytes.Buffer
err error
str *string
}
// Err returns the error log.
func (r *Result) Err() error {
if r.err == nil {
return nil
}
r.err = errors.New(r.String())
return r.err
}
// String returns the exec log.
func (r *Result) String() string {
if r.str == nil {
b := bytes.TrimSpace(r.buf.Bytes())
if r.err != nil {
b = append(b, ' ', '(')
b = append(b, r.err.Error()...)
b = append(b, ')')
}
r.str = (*string)(unsafe.Pointer(&b))
}
return *r.str
}