-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoexec.go
317 lines (258 loc) · 8.24 KB
/
goexec.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
309
310
311
312
313
314
315
316
317
package goexec
import (
"os"
"os/exec"
"github.com/brad-jones/goasync/v2/task"
"github.com/brad-jones/goerr/v2"
)
// Cmd provides a fluent, decorator based API for os/exec.
//
// Cmd("ping", Args("-c", "4", "1.1.1.1"))
func Cmd(cmd string, decorators ...func(*exec.Cmd) error) (c *exec.Cmd, err error) {
defer goerr.Handle(func(e error) { c = nil; err = e })
c = exec.Command(cmd)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
for _, decorator := range decorators {
goerr.Check(decorator(c))
}
if c.Env == nil {
c.Env = os.Environ()
}
return
}
// MustCmd does the same as Cmd but panics if an error is encountered.
func MustCmd(cmd string, decorators ...func(*exec.Cmd) error) *exec.Cmd {
c, e := Cmd(cmd, decorators...)
goerr.Check(e)
return c
}
// Run is a convenience function for simple cases.
//
// Instead of:
// Cmd("ping", Args("-c", "4", "8.8.8.8")).Run()
//
// You might write:
// Run("ping", "-c", "4", "8.8.8.8")
func Run(cmd string, args ...string) (err error) {
defer goerr.Handle(func(e error) { err = e })
c, err := Cmd(cmd, Args(args...))
goerr.Check(err)
goerr.Check(c.Run())
return
}
// MustRun does the same as Run but panics if an error is encountered.
func MustRun(cmd string, args ...string) {
goerr.Check(Run(cmd, args...))
}
// RunAsync does the same as Run but does so asynchronously.
func RunAsync(cmd string, args ...string) *task.Task {
return task.New(func(t *task.Internal) {
MustRun(cmd, args...)
})
}
// RunBuffered is a convenience function for simple cases.
//
// Instead of:
// RunBufferedCmd(Cmd("ping", Args("-c", "4", "8.8.8.8")))
//
// You might write:
// RunBuffered("ping", "-c", "4", "8.8.8.8")
//
// NOTE: `RunBuffered()` returns stdOut and stdErr as strings if you want the
// untouched byte arrays you could use `RunBufferedCmd()` instead.
func RunBuffered(cmd string, args ...string) (out *StdStrings, err error) {
defer goerr.Handle(func(e error) { out = nil; err = e })
c, err := Cmd(cmd, Args(args...))
goerr.Check(err)
o, err := RunBufferedCmd(c)
goerr.Check(err)
return &StdStrings{string(o.StdOut), string(o.StdErr)}, err
}
// MustRunBuffered does the same as RunBuffered but panics if an error is encountered.
func MustRunBuffered(cmd string, args ...string) *StdStrings {
out, err := RunBuffered(cmd, args...)
goerr.Check(err)
return out
}
// RunBufferedAsync does the same as RunBuffered but does so asynchronously.
func RunBufferedAsync(cmd string, args ...string) *task.Task {
return task.New(func(t *task.Internal) {
t.Resolve(MustRunBuffered(cmd, args...))
})
}
// RunPrefixed is a convenience function for simple cases.
//
// Instead of:
// RunPrefixedCmd("foo", Cmd("ping", Args("-c", "4", "8.8.8.8")))
//
// You might write:
// RunPrefixed("foo", "ping", "-c", "4", "8.8.8.8")
func RunPrefixed(prefix, cmd string, args ...string) (err error) {
defer goerr.Handle(func(e error) { err = e })
c, err := Cmd(cmd, Args(args...))
goerr.Check(err)
goerr.Check(RunPrefixedCmd(prefix, c))
return
}
// MustRunPrefixed does the same as RunPrefixed but panics if an error is encountered.
func MustRunPrefixed(prefix, cmd string, args ...string) {
goerr.Check(RunPrefixed(prefix, cmd, args...))
}
// RunPrefixedAsync does the same as RunPrefixed but does so asynchronously.
func RunPrefixedAsync(prefix, cmd string, args ...string) *task.Task {
return task.New(func(t *task.Internal) {
MustRunPrefixed(prefix, cmd, args...)
})
}
// RunPrefixedCmd will prefix all StdOut and StdErr with given prefix.
// This is useful when running many commands concurrently,
// output will look similar to docker-compose.
func RunPrefixedCmd(prefix string, cmd *exec.Cmd) (err error) {
defer goerr.Handle(func(e error) { err = e })
stdOutPipeR, stdOutPipeW, err := os.Pipe()
goerr.Check(err)
cmd.Stdout = stdOutPipeW
stdErrPipeR, stdErrPipeW, err := os.Pipe()
goerr.Check(err)
cmd.Stderr = stdErrPipeW
return prefixed(prefix,
os.Stdout, os.Stderr,
stdOutPipeR, stdErrPipeR,
stdOutPipeW, stdErrPipeW, func() error {
return cmd.Run()
},
)
}
// MustRunPrefixedCmd does the same as RunPrefixedCmd but panics if an error is encountered.
func MustRunPrefixedCmd(prefix string, cmd *exec.Cmd) {
goerr.Check(RunPrefixedCmd(prefix, cmd))
}
// RunPrefixedCmdAsync does the same as RunPrefixedCmd but does so asynchronously.
func RunPrefixedCmdAsync(prefix string, cmd *exec.Cmd) *task.Task {
return task.New(func(t *task.Internal) {
RunPrefixedCmd(prefix, cmd)
})
}
// RunBufferedCmd will buffer all StdOut and StdErr, returning the buffers.
// This is useful when you wish to parse the results of a command.
func RunBufferedCmd(cmd *exec.Cmd) (out *StdBytes, err error) {
defer goerr.Handle(func(e error) { out = nil; err = e })
stdOutPipeR, stdOutPipeW, err := os.Pipe()
goerr.Check(err)
cmd.Stdout = stdOutPipeW
stdErrPipeR, stdErrPipeW, err := os.Pipe()
goerr.Check(err)
cmd.Stderr = stdErrPipeW
return buffered(
stdOutPipeR, stdErrPipeR,
stdOutPipeW, stdErrPipeW, func() error {
return cmd.Run()
},
)
}
// MustRunBufferedCmd does the same as RunBufferedCmd but panics if an error is encountered.
func MustRunBufferedCmd(cmd *exec.Cmd) *StdBytes {
out, err := RunBufferedCmd(cmd)
goerr.Check(err)
return out
}
// RunBufferedCmdAsync does the same as RunBufferedCmd but does so asynchronously.
func RunBufferedCmdAsync(cmd *exec.Cmd) *task.Task {
return task.New(func(t *task.Internal) {
t.Resolve(MustRunBufferedCmd(cmd))
})
}
// Pipe will send the output of the first command
// to the input of the second and so on.
func Pipe(cmds ...*exec.Cmd) (err error) {
defer goerr.Handle(func(e error) { err = e })
for key, cmd := range cmds {
if key > 0 {
cmds[key-1].Stdout = nil
pipe, err := cmds[key-1].StdoutPipe()
goerr.Check(err)
cmd.Stdin = pipe
}
}
for _, cmd := range cmds {
goerr.Check(cmd.Start())
}
for _, cmd := range cmds {
goerr.Check(cmd.Wait())
}
return
}
// MustPipe does the same as Pipe but panics if an error is encountered.
func MustPipe(cmds ...*exec.Cmd) {
goerr.Check(Pipe(cmds...))
}
// PipeAsync does the same as Pipe but does so asynchronously.
func PipeAsync(cmds ...*exec.Cmd) *task.Task {
return task.New(func(t *task.Internal) {
MustPipe(cmds...)
})
}
// PipePrefixed will prefix all StdOut and StdErr with given prefix.
// This is useful when running many pipes concurrently,
// output will look similar to docker-compose.
func PipePrefixed(prefix string, cmds ...*exec.Cmd) (err error) {
defer goerr.Handle(func(e error) { err = e })
stdOutPipeR, stdOutPipeW, err := os.Pipe()
goerr.Check(err)
stdErrPipeR, stdErrPipeW, err := os.Pipe()
goerr.Check(err)
for _, cmd := range cmds {
cmd.Stderr = stdErrPipeW
}
cmds[len(cmds)-1].Stdout = stdOutPipeW
return prefixed(prefix,
os.Stdout, os.Stderr,
stdOutPipeR, stdErrPipeR,
stdOutPipeW, stdErrPipeW, func() error {
return Pipe(cmds...)
},
)
}
// MustPipePrefixed does the same as PipePrefixed but panics if an error is encountered.
func MustPipePrefixed(prefix string, cmds ...*exec.Cmd) {
goerr.Check(PipePrefixed(prefix, cmds...))
}
// PipePrefixedAsync does the same as PipePrefixed but does so asynchronously.
func PipePrefixedAsync(prefix string, cmds ...*exec.Cmd) *task.Task {
return task.New(func(t *task.Internal) {
MustPipePrefixed(prefix, cmds...)
})
}
// PipeBuffered will buffer all StdOut and StdErr, returning the buffers.
// This is useful when you wish to parse the results of a pipe.
func PipeBuffered(cmds ...*exec.Cmd) (out *StdBytes, err error) {
defer goerr.Handle(func(e error) { out = nil; err = e })
stdOutPipeR, stdOutPipeW, err := os.Pipe()
goerr.Check(err)
stdErrPipeR, stdErrPipeW, err := os.Pipe()
goerr.Check(err)
for _, cmd := range cmds {
cmd.Stderr = stdErrPipeW
}
cmds[len(cmds)-1].Stdout = stdOutPipeW
return buffered(
stdOutPipeR, stdErrPipeR,
stdOutPipeW, stdErrPipeW, func() error {
return Pipe(cmds...)
},
)
}
// MustPipeBuffered does the same as PipeBuffered but panics if an error is encountered.
func MustPipeBuffered(cmds ...*exec.Cmd) *StdBytes {
out, err := PipeBuffered(cmds...)
goerr.Check(err)
return out
}
// PipeBufferedAsync does the same as PipeBuffered but does so asynchronously.
func PipeBufferedAsync(cmds ...*exec.Cmd) *task.Task {
return task.New(func(t *task.Internal) {
MustPipeBuffered(cmds...)
})
}