forked from shutterstock/go-stockutil
/
cmd.go
588 lines (487 loc) · 13.2 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Utilities that make executing commands on the local system a little bit easier.
package executil
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/mattn/go-shellwords"
)
type CommandStatusFunc func(Status)
type OutputLineFunc func(string, bool)
type Status struct {
StartedAt time.Time
StoppedAt time.Time
Running bool
Successful bool
ExitCode int
Error error
PID int
Cmd *Cmd
}
func (self Status) Took() time.Duration {
if !self.StartedAt.IsZero() {
if !self.StoppedAt.IsZero() {
return self.StoppedAt.Sub(self.StartedAt)
}
}
return 0
}
func (self Status) String() string {
if self.Error != nil {
return self.Error.Error()
} else if self.Running {
if self.PID > 0 {
return fmt.Sprintf("PID %d has been running for %v", self.PID, time.Since(self.StartedAt))
} else {
return fmt.Sprintf("Process has been running for %v, PID unknown", time.Since(self.StartedAt))
}
} else if !self.StoppedAt.IsZero() {
if self.PID > 0 {
return fmt.Sprintf("PID %d exited with status %d, took %v", self.PID, self.ExitCode, self.Took())
} else {
return fmt.Sprintf("Process exited with status %d, took %v, PID unknown", self.ExitCode, self.Took())
}
} else if self.StartedAt.IsZero() {
return fmt.Sprintf("Process has not started yet")
} else {
return fmt.Sprintf("Process status is unknown")
}
}
// A wrapper for exec.Cmd that provides helpful callbacks and monitoring details that are challenging
// to implement.
type Cmd struct {
*exec.Cmd
// An interval of time on which the command should be actively checked for run and exit status.
MonitorInterval time.Duration
// How long the command may run for before being killed.
Timeout time.Duration
// Whether the command invocation should inherit the environment variables of the calling process.
InheritEnv bool
// Called when immediately before the command is executed.
OnStart CommandStatusFunc
// Called whenever the monitor check is performed.
OnMonitor CommandStatusFunc
// Called when the command exits, regardless of success or failure.
OnComplete CommandStatusFunc
// Called when the command exits with a non-error status (code 0)
OnSuccess CommandStatusFunc
// Called when the command exits with an error status (non-zero exit code, security, invocation, or resource error)
OnError CommandStatusFunc
// Called when a line of standard output is written.
OnStdout OutputLineFunc
// Called when a line of standard error is written.
OnStderr OutputLineFunc
// If specified, this function will determine how to tokenize the stdout stream and when to call OnStdout. Defaults to bufio.ScanLines.
StdoutSplitFunc bufio.SplitFunc
// If specified, this function will determine how to tokenize the stderr stream and when to call OnStderr. Defaults to bufio.ScanLines.
StderrSplitFunc bufio.SplitFunc
// Specifies that the spawned process should inherit the same Process Group ID (PGID) as the parent.
InheritParent bool
status Status
finalStatus *Status
statusLock sync.Mutex
reallyDone *sync.WaitGroup
finished chan bool
exitError error
reader io.ReadCloser
writer io.WriteCloser
hasWaited bool
}
func Wrap(cmd *exec.Cmd) *Cmd {
return new(cmd)
}
func Command(name string, arg ...string) *Cmd {
return new(exec.Command(name, arg...))
}
func ShellCommand(cmdline string) *Cmd {
if shell := FindShell(); shell != `` {
return Command(shell, `-c`, cmdline)
}
return nil
}
// Run a command and return the standard output. If the first argument contains
// a command and its arguments, it will be executed in the user's shell using FindShell.
// Otherwise, the first argument will be treated as a command and the remaining arguments
// will be passed in parameterized.
func ShellOut(cmdOrLine string, args ...interface{}) ([]byte, error) {
var cmd *Cmd
if va, err := shellwords.Parse(cmdOrLine); err == nil {
if len(va) == 1 {
cmd = Command(va[0], sliceutil.Stringify(args)...)
} else {
cmd = ShellCommand(strings.Join(
append(va, sliceutil.Stringify(args)...),
` `,
))
}
if out, err := cmd.Output(); err == nil {
return out, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
// A panicky version of ShellOut.
func MustShellOut(cmdOrLine string, args ...interface{}) []byte {
if out, err := ShellOut(cmdOrLine, args...); err == nil {
return out
} else {
panic(err.Error())
}
}
// Attempts to call ShellOut, but will return nil if there is an error. Does not panic.
func ShouldShellOut(cmdOrLine string, args ...interface{}) []byte {
if out, err := ShellOut(cmdOrLine, args...); err == nil {
return out
} else {
return nil
}
}
func new(wrap *exec.Cmd) *Cmd {
return &Cmd{
Cmd: wrap,
MonitorInterval: (500 * time.Millisecond),
status: Status{},
finished: make(chan bool),
reallyDone: &sync.WaitGroup{},
}
}
func (self *Cmd) prestart() error {
self.statusLock.Lock()
self.hasWaited = false
self.finalStatus = nil
self.status = Status{}
self.status.StartedAt = time.Now()
self.status.Running = true
self.statusLock.Unlock()
if self.InheritParent {
self.Cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: os.Getpid(),
}
}
self.updateStatus()
if fn := self.OnStart; fn != nil {
fn(self.status)
}
go self.startMonitoringCommand()
if self.InheritEnv {
self.Cmd.Env = append(os.Environ(), self.Cmd.Env...)
}
if fn := self.OnStdout; fn != nil {
if out, err := self.StdoutPipe(); err == nil {
go func(rc io.ReadCloser) {
defer rc.Close()
var splitfn = self.StdoutSplitFunc
if splitfn == nil {
splitfn = bufio.ScanLines
}
var splitter = stringutil.NewScanInterceptor(splitfn)
var scanner = bufio.NewScanner(rc)
scanner.Split(splitter.Scan)
for scanner.Scan() {
fn(scanner.Text(), false)
}
}(out)
} else {
return fmt.Errorf("stdout: %v", err)
}
}
if fn := self.OnStderr; fn != nil {
if serr, err := self.StderrPipe(); err == nil {
go func(rc io.ReadCloser) {
defer rc.Close()
var splitfn = self.StderrSplitFunc
if splitfn == nil {
splitfn = bufio.ScanLines
}
var splitter = stringutil.NewScanInterceptor(splitfn)
var scanner = bufio.NewScanner(rc)
scanner.Split(splitter.Scan)
for scanner.Scan() {
fn(scanner.Text(), false)
}
}(serr)
} else {
return fmt.Errorf("stdout: %v", err)
}
}
return nil
}
func (self *Cmd) CombinedOutput() ([]byte, error) {
defer self.killAndWait()
if err := self.prestart(); err == nil {
var out, err = self.Cmd.CombinedOutput()
self.exitError = err
self.updateStatus()
return out, err
} else {
return nil, err
}
}
func (self *Cmd) String() string {
if out, err := self.Output(); err == nil {
return string(out)
} else {
return ``
}
}
func (self *Cmd) Output() ([]byte, error) {
defer self.killAndWait()
if err := self.prestart(); err == nil {
var out, err = self.Cmd.Output()
self.exitError = err
self.updateStatus()
return out, err
} else {
return nil, err
}
}
func (self *Cmd) SetEnv(key string, value interface{}) {
kv := fmt.Sprintf("%v=%s", key, typeutil.String(value))
for i, pair := range self.Cmd.Env {
k, _ := stringutil.SplitPair(pair, `=`)
if k == key {
self.Cmd.Env[i] = kv
return
}
}
self.Cmd.Env = append(self.Cmd.Env, kv)
}
func (self *Cmd) Run() error {
defer self.killAndWait()
if err := self.prestart(); err == nil {
err := self.Cmd.Run()
self.exitError = err
self.updateStatus()
return err
} else {
return err
}
}
func (self *Cmd) Start() error {
if err := self.prestart(); err == nil {
if err := self.Cmd.Start(); err != nil {
self.exitError = err
self.updateStatus()
return err
} else {
return nil
}
} else {
return err
}
}
// Return the current status of the process.
func (self *Cmd) Status() Status {
self.statusLock.Lock()
defer self.statusLock.Unlock()
return self.status
}
// Kill the running command.
func (self *Cmd) Kill() error {
if p := self.Process; p != nil {
return p.Kill()
} else {
return nil
}
}
// Wait for the process to complete, then return the last status.
// Process must have been started using the Start() function.
func (self *Cmd) WaitStatus() Status {
if !self.hasWaited {
self.exitError = self.Wait()
self.hasWaited = true
}
if xe := self.exitError; xe != nil {
switch xe.Error() {
case `exec: Wait was already called`:
self.exitError = nil
}
}
self.updateStatus()
return self.status
}
// Implements io.Reader, sourcing data from the command's standard output. If the command is not
// already running, it will be started.
func (self *Cmd) Read(p []byte) (int, error) {
if self.reader == nil {
if err := self.interceptStdout(); err != nil {
return 0, err
}
if err := self.Start(); err != nil {
return 0, err
}
}
return self.reader.Read(p)
}
// Implements io.Writer, writing data to the commands standard input. If the command is not already
// running, it will be started.
func (self *Cmd) Write(p []byte) (int, error) {
if self.writer == nil {
if err := self.interceptStdout(); err != nil {
return 0, err
}
if err := self.interceptStdin(); err != nil {
return 0, err
}
if err := self.Start(); err != nil {
return 0, err
}
}
return self.writer.Write(p)
}
// Implements io.Closer, killing the underlying process, waiting for it to exit, then returning.
func (self *Cmd) Close() error {
var merr error
if w := self.writer; w != nil {
merr = log.AppendError(merr, w.Close())
}
if r := self.reader; r != nil {
merr = log.AppendError(merr, r.Close())
}
merr = log.AppendError(merr, self.killAndWait())
return merr
}
// Notify the command that no further standard input will be written.
func (self *Cmd) CloseInput() error {
if w := self.writer; w != nil {
return w.Close()
} else {
return nil
}
}
func (self *Cmd) interceptStdout() (err error) {
self.reader, err = self.StdoutPipe()
return
}
func (self *Cmd) interceptStdin() (err error) {
self.writer, err = self.StdinPipe()
return
}
func (self *Cmd) killAndWait() error {
// this quits the startMonitoringCommand() monitoring loop
select {
case self.finished <- true:
// this waits for startMonitoringCommand() to fire callbacks and actually exit
self.reallyDone.Wait()
default:
}
if status := self.finalStatus; status != nil {
switch msg := typeutil.String(status.Error); msg {
case `signal: killed`:
return nil
default:
return status.Error
}
} else {
return nil
}
}
// a goroutine that is launched whenever a command is started
func (self *Cmd) startMonitoringCommand() {
self.reallyDone.Add(1)
// if there's a timeout set, and its shorter than our monitor interval,
// reduce the monitor interval to that
if self.Timeout > 0 && self.Timeout < self.MonitorInterval {
self.MonitorInterval = self.Timeout
}
var ticker = time.NewTicker(self.MonitorInterval)
MonitorLoop:
for self.Timeout == 0 || time.Since(self.status.StartedAt) < self.Timeout {
select {
case <-ticker.C:
self.updateStatus()
if !self.status.Running {
break MonitorLoop
}
if fn := self.OnMonitor; fn != nil {
fn(self.Status())
}
case <-self.finished:
self.Kill()
break MonitorLoop
}
}
ticker.Stop()
self.updateStatus()
var final = self.WaitStatus()
self.finalStatus = &final
// fire off callbacks
if fn := self.OnComplete; fn != nil {
fn(final)
}
if self.finalStatus.Successful {
if fn := self.OnSuccess; fn != nil {
fn(final)
}
} else if fn := self.OnError; fn != nil {
fn(final)
}
self.reallyDone.Done()
}
func (self *Cmd) updateStatus() {
self.statusLock.Lock()
defer self.statusLock.Unlock()
self.status.Cmd = self
var pstate *os.ProcessState
// handle known exit errors
if self.exitError != nil {
self.status.Error = self.exitError
self.status.Running = false
self.status.StoppedAt = time.Now()
if xerr, ok := self.exitError.(*exec.ExitError); ok {
pstate = xerr.ProcessState
} else {
return
}
} else if s := self.Cmd.ProcessState; s != nil {
pstate = s
}
// check ProcessState
if pstate != nil {
self.status.PID = pstate.Pid()
self.status.Successful = pstate.Success()
if status, ok := pstate.Sys().(syscall.WaitStatus); ok {
if status.Exited() {
self.status.Running = false
self.status.ExitCode = status.ExitStatus()
self.status.StoppedAt = time.Now()
if status.Signaled() {
if sig := status.StopSignal(); sig != 0 {
self.status.Error = errors.New(sig.String())
} else if sig := status.Signal(); sig != 0 {
self.status.Error = errors.New(sig.String())
}
} else if e := self.status.ExitCode; e != 0 {
self.status.Error = fmt.Errorf("Process exited with status %d", e)
}
} else if status.Stopped() {
self.status.Running = false
} else {
self.status.Running = true
}
}
} else if process := self.Cmd.Process; process != nil {
self.status.PID = process.Pid
}
}
func CommandContext(ctx context.Context, name string, arg ...string) *Cmd {
return &Cmd{
Cmd: exec.CommandContext(ctx, name, arg...),
}
}