-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_command.go
72 lines (63 loc) · 1.51 KB
/
run_command.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
package main
import (
"bytes"
"errors"
"log"
"os/exec"
"runtime"
"time"
)
func runcmd(command string) (res string) {
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/c"
} else {
shell = "/bin/sh"
flag = "-c"
}
res, err := run(10, shell, flag, command)
if err != nil {
log.Println(err)
return
}
return
}
func run(timeout int, command string, args ...string) (res string, err error) {
// instantiate new command
cmd := exec.Command(command, args...)
// get pipe to standard output
stdout, err := cmd.StdoutPipe()
if err != nil {
return res, errors.New("cmd.StdoutPipe() error: " + err.Error())
}
// start process via command
if err = cmd.Start(); err != nil {
return res, errors.New("cmd.Start() error: " + err.Error())
}
// setup a buffer to capture standard output
var buf bytes.Buffer
// create a channel to capture any errors from wait
done := make(chan error)
go func() {
if _, err := buf.ReadFrom(stdout); err != nil {
panic("buf.Read(stdout) error: " + err.Error())
}
done <- cmd.Wait()
}()
// block on select, and switch based on actions received
select {
case <-time.After(time.Duration(timeout) * time.Second):
if err := cmd.Process.Kill(); err != nil {
return res, errors.New("failed to kill: " + err.Error())
}
return "", errors.New("command timed out")
case err = <-done:
if err != nil {
close(done)
return res, errors.New("process done, with error: " + err.Error())
}
return buf.String(), nil
}
return "", nil
}