forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
117 lines (106 loc) · 2.46 KB
/
process.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tests
import (
"io"
"io/ioutil"
"os"
"os/exec"
"time"
)
// execution process
type Process struct {
ExecPath string
Args []string
Pid int
StartTime time.Time
EndTime time.Time
Cmd *exec.Cmd `json:"-"`
ExitState *os.ProcessState `json:"-"`
StdinPipe io.WriteCloser `json:"-"`
StdoutPipe io.ReadCloser `json:"-"`
StderrPipe io.ReadCloser `json:"-"`
}
// dir: The working directory. If "", os.Getwd() is used.
// name: Command name
// args: Args to command. (should not include name)
func StartProcess(dir string, name string, args []string) (*Process, error) {
proc, err := CreateProcess(dir, name, args)
if err != nil {
return nil, err
}
// cmd start
if err := proc.Cmd.Start(); err != nil {
return nil, err
}
proc.Pid = proc.Cmd.Process.Pid
return proc, nil
}
// Same as StartProcess but doesn't start the process
func CreateProcess(dir string, name string, args []string) (*Process, error) {
var cmd = exec.Command(name, args...) // is not yet started.
// cmd dir
if dir == "" {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
cmd.Dir = pwd
} else {
cmd.Dir = dir
}
// cmd stdin
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
proc := &Process{
ExecPath: name,
Args: args,
StartTime: time.Now(),
Cmd: cmd,
ExitState: nil,
StdinPipe: stdin,
StdoutPipe: stdout,
StderrPipe: stderr,
}
return proc, nil
}
// stop the process
func (proc *Process) Stop(kill bool) error {
if kill {
// fmt.Printf("Killing process %v\n", proc.Cmd.Process)
return proc.Cmd.Process.Kill()
}
return proc.Cmd.Process.Signal(os.Interrupt)
}
// wait for the process
func (proc *Process) Wait() {
err := proc.Cmd.Wait()
if err != nil {
// fmt.Printf("Process exit: %v\n", err)
if exitError, ok := err.(*exec.ExitError); ok {
proc.ExitState = exitError.ProcessState
}
}
proc.ExitState = proc.Cmd.ProcessState
proc.EndTime = time.Now() // TODO make this goroutine-safe
}
// ReadAll calls ioutil.ReadAll on the StdoutPipe and StderrPipe.
func (proc *Process) ReadAll() (stdout []byte, stderr []byte, err error) {
outbz, err := ioutil.ReadAll(proc.StdoutPipe)
if err != nil {
return nil, nil, err
}
errbz, err := ioutil.ReadAll(proc.StderrPipe)
if err != nil {
return nil, nil, err
}
return outbz, errbz, nil
}