-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
308 lines (274 loc) · 8.13 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Package script is a library facilitating the creation of programs that resemble
// bash scripts.
package script
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
// ProcessResult contains the results of a process execution be it successful or not.
type ProcessResult struct {
Cmd *exec.Cmd
Process *os.Process
ProcessState *os.ProcessState
ProcessError error
stdoutBuffer *bytes.Buffer
stderrBuffer *bytes.Buffer
}
// CommandConfig defines details of command execution.
type CommandConfig struct {
RawStdout bool
RawStderr bool
OutputStdout bool
OutputStderr bool
ConnectStdin bool
Detach bool
}
// NewProcessResult creates a new empty ProcessResult
func NewProcessResult() *ProcessResult {
p := &ProcessResult{}
p.stdoutBuffer = bytes.NewBuffer(make([]byte, 0, 100))
p.stderrBuffer = bytes.NewBuffer(make([]byte, 0, 100))
return p
}
// Output returns a string representation of the output of the process denoted
// by this struct.
func (pr *ProcessResult) Output() string {
return pr.stdoutBuffer.String()
}
// TrimmedOutput returns a string representation of the output of the process denoted
// by this struct with surrounding whitespace removed.
func (pr *ProcessResult) TrimmedOutput() string {
return strings.TrimSpace(pr.Output())
}
// Error returns a string representation of the stderr output of the process denoted
// by this struct.
func (pr *ProcessResult) Error() string {
return pr.stderrBuffer.String()
}
// Successful returns true iff the process denoted by this struct was run
// successfully. Success is defined as the exit code being set to 0.
func (pr *ProcessResult) Successful() bool {
code, err := pr.ExitCode()
if err != nil {
return false
}
return code == 0
}
// StateString returns a string representation of the process denoted by
// this struct
func (pr *ProcessResult) StateString() string {
state := pr.ProcessState
exitCode, err := pr.ExitCode()
exitCodeString := "?"
if err == nil {
exitCodeString = strconv.Itoa(exitCode)
}
return fmt.Sprintf("PID: %d, Exited: %t, Exit Code: %s, Success: %t, User Time: %s", state.Pid(), state.Exited(), exitCodeString, state.Success(), state.UserTime())
}
// ExitCode returns the exit code of the command denoted by this struct
func (pr *ProcessResult) ExitCode() (int, error) {
var (
waitStatus syscall.WaitStatus
exitError *exec.ExitError
)
ok := false
if pr.ProcessError != nil {
exitError, ok = pr.ProcessError.(*exec.ExitError)
}
if ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
} else {
if pr.ProcessState == nil {
return -1, errors.New("no exit code available")
}
waitStatus = pr.ProcessState.Sys().(syscall.WaitStatus)
}
return waitStatus.ExitStatus(), nil
}
// CommandPath finds the full path of a binary given its name.
// also see https://golang.org/pkg/os/exec/#LookPath
func (c *Context) CommandPath(name string) (path string) {
path, err := exec.LookPath(name)
if err != nil {
return ""
}
return
}
// CommandExists checks if a given binary exists in PATH.
func (c *Context) CommandExists(name string) bool {
return c.CommandPath(name) != ""
}
// MustCommandExist ensures a given binary exists in PATH, otherwise panics.
func (c *Context) MustCommandExist(name string) {
if !c.CommandExists(name) {
panic(fmt.Errorf("Command %s is not available. Please make sure it is installed and accessible.", name))
}
}
// ExecuteRaw executes a system command without touching stdout and stderr.
func (c *Context) ExecuteRaw(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
RawStdout: true,
RawStderr: true,
ConnectStdin: true,
}, command)
return
}
// ExecuteDebug executes a system command, stdout and stderr are piped
func (c *Context) ExecuteDebug(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: true,
OutputStderr: true,
ConnectStdin: true,
}, command)
return
}
// ExecuteSilent executes a system command without outputting stdout (it is
// still captured and can be retrieved using the returned ProcessResult)
func (c *Context) ExecuteSilent(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: true,
ConnectStdin: true,
}, command)
return
}
// ExecuteFullySilent executes a system command without outputting stdout or
// stderr (both are still captured and can be retrieved using the returned ProcessResult)
func (c *Context) ExecuteFullySilent(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: false,
ConnectStdin: true,
}, command)
return
}
// MustExecuteDebug ensures a system command to be executed, otherwise panics
func (c *Context) MustExecuteDebug(command Command) (pr *ProcessResult) {
pr, err := c.Execute(CommandConfig{
OutputStdout: true,
OutputStderr: true,
ConnectStdin: true,
}, command)
if err != nil {
panic(err)
}
return
}
// MustExecuteSilent ensures a system command to be executed without outputting
// stdout, otherwise panics
func (c *Context) MustExecuteSilent(command Command) (pr *ProcessResult) {
pr, err := c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: true,
ConnectStdin: true,
}, command)
if err != nil {
panic(err)
}
return
}
// MustExecuteFullySilent ensures a system command to be executed without
// outputting stdout and stderr, otherwise panics
func (c *Context) MustExecuteFullySilent(command Command) (pr *ProcessResult) {
pr, err := c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: false,
ConnectStdin: true,
}, command)
if err != nil {
panic(err)
}
return
}
// Execute executes a system command according to given CommandConfig.
func (c *Context) Execute(cc CommandConfig, command Command) (pr *ProcessResult, err error) {
cmd, pr := c.prepareCommand(cc, command)
if cc.Detach {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
err = cmd.Start()
if err != nil {
return
}
pr.Process = cmd.Process
if !cc.Detach {
c.WaitCmd(pr)
}
return
}
// ExecuteDetachedDebug executes a system command, stdout and stderr are piped.
// The command is executed in the background (detached).
func (c *Context) ExecuteDetachedDebug(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: true,
OutputStderr: true,
Detach: true,
}, command)
return
}
// ExecuteDetachedSilent executes a system command without outputting stdout (it is
// still captured and can be retrieved using the returned ProcessResult).
// The command is executed in the background (detached).
func (c *Context) ExecuteDetachedSilent(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: true,
Detach: true,
}, command)
return
}
// ExecuteDetachedFullySilent executes a system command without outputting stdout or
// stderr (both are still captured and can be retrieved using the returned ProcessResult).
// The command is executed in the background (detached).
func (c *Context) ExecuteDetachedFullySilent(command Command) (pr *ProcessResult, err error) {
pr, err = c.Execute(CommandConfig{
OutputStdout: false,
OutputStderr: false,
Detach: true,
}, command)
return
}
func (c Context) prepareCommand(cc CommandConfig, command Command) (*exec.Cmd, *ProcessResult) {
pr := NewProcessResult()
cmd := exec.Command(command.Binary(), command.Args()...)
pr.Cmd = cmd
cmd.Dir = c.workingDir
cmd.Env = c.GetFullEnv()
if cc.RawStdout {
cmd.Stdout = os.Stdout
} else {
if !cc.OutputStdout {
cmd.Stdout = pr.stdoutBuffer
} else {
cmd.Stdout = io.MultiWriter(c.stdout, pr.stdoutBuffer)
}
}
if cc.RawStderr {
cmd.Stderr = os.Stderr
} else {
if !cc.OutputStderr {
cmd.Stderr = pr.stderrBuffer
} else {
cmd.Stderr = io.MultiWriter(c.stderr, pr.stderrBuffer)
}
}
if cc.ConnectStdin {
cmd.Stdin = c.stdin
}
return cmd, pr
}
// WaitCmd waits for a command to be finished (useful on detached processes).
func (c Context) WaitCmd(pr *ProcessResult) {
err := pr.Cmd.Wait()
pr.ProcessState = pr.Cmd.ProcessState
pr.ProcessError = err
}