-
Notifications
You must be signed in to change notification settings - Fork 4
/
commander.go
161 lines (132 loc) · 2.98 KB
/
commander.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
package leaf
import (
"context"
"fmt"
"os"
"os/exec"
"syscall"
"github.com/kballard/go-shellquote"
)
var errEmptyCmd = fmt.Errorf("empty command")
// Commander has a set of commands that run in order
// and exit when the context is canceled.
type Commander struct {
Commands []string
OnStart func(*Command)
OnError func(error)
OnExit func()
ExitOnError bool
done chan bool
}
// NewCommander creates a new commander.
func NewCommander(commander Commander) *Commander {
return &Commander{
Commands: commander.Commands,
OnStart: commander.OnStart,
OnError: commander.OnError,
OnExit: commander.OnExit,
ExitOnError: commander.ExitOnError,
done: make(chan bool, 1),
}
}
// Done signals that the commander is done running the commands.
func (c *Commander) Done() <-chan bool {
return c.done
}
// Run executes the commands in order. It stops when the
// context is canceled.
func (c *Commander) Run(ctx context.Context) {
defer func() {
// signal done when running commands is complete
// or the function exits, eitherway.
c.done <- true
c.OnExit()
}()
for _, command := range c.Commands {
cmd, err := NewCommand(command)
if err != nil {
c.OnError(err)
return
}
select {
case <-ctx.Done():
return
default:
if cmd == nil {
continue
}
if c.OnStart != nil {
c.OnStart(cmd)
}
if err := cmd.Execute(ctx); err != nil {
if c.OnError != nil {
c.OnError(err)
}
if c.ExitOnError {
return
}
}
}
}
}
// Command is an external command that can be executed.
type Command struct {
Name string
Args []string
str string
}
// String returns the command in a human-readable format.
func (c *Command) String() string {
return c.str
}
// NewCommand creates a new command from the string.
func NewCommand(cmd string) (*Command, error) {
parsedCmd, err := shellquote.Split(cmd)
if err != nil {
return nil, err
}
if len(parsedCmd) == 0 {
return nil, errEmptyCmd
}
name := parsedCmd[0]
var args []string
if len(parsedCmd) > 1 {
args = parsedCmd[1:]
}
return &Command{
Name: name,
Args: args,
str: shellquote.Join(parsedCmd...),
}, nil
}
// Execute runs the commands and exits elegantly when the
// context is canceled.
//
// This doesn't use the exec.CommandContext because we just
// don't want to kill the parent process but all the child
// processes too.
func (c *Command) Execute(ctx context.Context) error {
stream := make(chan error)
cmd := exec.Command(c.Name, c.Args...) // nolint:gosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := cmd.Start(); err != nil {
return err
}
go func(ex *exec.Cmd, err chan<- error) {
err <- ex.Wait()
}(cmd, stream)
select {
case <-ctx.Done():
// Elegantly close the parent along-with the children.
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
return err
}
return nil
case err := <-stream:
return err
}
}