-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
215 lines (184 loc) · 5.37 KB
/
run.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
package go_exec
import (
"bufio"
"fmt"
"io"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/go-errors/errors"
"github.com/rs/zerolog/log"
)
type RunCmd struct {
cmd *exec.Cmd
stdout io.ReadCloser
stderr io.ReadCloser
}
type Pipe struct {
Cmd string
Args []string
}
// Run the supplied command.
func Run(command string, conditionals ...string) error {
return RunDir(command, ".", conditionals...)
}
// RunDir run the supplied command from the specified directory.
func RunDir(command string, dir string, conditionals ...string) error {
pipe := strings.Split(command, "|")
return RunStack(CreateRunStack(pipe, dir), conditionals...)
}
// RunArgs run the command with supplied args (not safe for args that require whitespace).
func RunArgs(command string, args []string, dir string, conditionals ...string) error {
pipe := strings.Split(fmt.Sprintf("%s %s", command, strings.Join(args, " ")), "|")
return RunStack(CreateRunStack(pipe, dir), conditionals...)
}
// CreateRunStackWithArgs create a run stack from the slice of Pipe structs, should automatically chain the stdout
// of the previous command into the stdin of the subsequent command.
func CreateRunStackWithArgs(pipe []Pipe, dir string) []*RunCmd {
stack := make([]*RunCmd, len(pipe))
if len(pipe) == 0 {
return stack
}
last := len(pipe) - 1
// build our commands
for i, c := range pipe {
cmd := exec.Command(c.Cmd, c.Args...) //nolint: gosec
cmd.Dir = dir
stderrPipe, _ := cmd.StderrPipe()
stack[i] = &RunCmd{cmd: cmd, stdout: nil, stderr: stderrPipe}
}
// now wire together with pipes
for i := range stack[:last] {
stack[i+1].cmd.Stdin, _ = stack[i].cmd.StdoutPipe()
}
// configure our last command in the chain
stack[last].stdout, _ = stack[last].cmd.StdoutPipe()
return stack
}
// CreateRunStack create a run stack from the slice of command strings.
func CreateRunStack(pipe []string, dir string) []*RunCmd {
stack := make([]Pipe, len(pipe))
for i, c := range pipe {
split := strings.Split(strings.Trim(c, " "), " ")
var parts []string
for _, v := range split {
if v != "" {
parts = append(parts, v)
}
}
stack[i] = Pipe{
Cmd: parts[0],
Args: parts[1:],
}
}
return CreateRunStackWithArgs(stack, dir)
}
// RunStack take the supplied stack of commands and run it if any conditionals are satisfied in the output.
func RunStack(stack []*RunCmd, conditionals ...string) (err error) {
isSuccessMessageInStdOut := make(chan bool)
isSuccessMessageInStdErr := make(chan bool)
var successDispiteErr = false
if len(stack) == 0 {
err := errors.New("no run stack defined")
log.Error().Err(err).Send()
return err
}
successDespiteErrWg := &sync.WaitGroup{}
successDespiteErrWg.Add(1)
go func(wg *sync.WaitGroup) {
select {
case stdout := <-isSuccessMessageInStdOut:
if stdout {
successDispiteErr = true
}
case stderr := <-isSuccessMessageInStdErr:
if stderr {
successDispiteErr = true
}
}
wg.Done()
}(successDespiteErrWg)
log.Debug().Msgf("Running: %s from %s", stack[0].cmd, stack[0].cmd.Dir)
if stack[0].cmd.Process == nil {
if err = stack[0].cmd.Start(); err != nil {
e := errors.Wrap(fmt.Errorf("%s, %w", stack[0].cmd.String(), err), 0)
log.Error().Err(e).Send()
return e
}
}
if stack[0].cmd.Process != nil {
check := GetConditionalCheck(conditionals...)
if stack[0].stderr != nil {
go HandleOutput(stack[0].stderr, check, isSuccessMessageInStdOut)
}
if stack[0].stdout != nil {
go HandleOutput(stack[0].stdout, check, isSuccessMessageInStdErr)
}
}
if len(stack) > 1 {
// start the next command in the chain to trigger
// the read of the incoming pipe
if err = stack[1].cmd.Start(); err != nil {
return errors.Wrap(fmt.Errorf("%s, %w", stack[1].cmd.String(), err), 0)
}
defer func() {
_ = stack[0].cmd.Stdout.(io.Closer).Close()
if err == nil {
// how handle the output and errors from the next command in the pipe
log.Debug().Msgf("\t| piping output to next command")
err = RunStack(stack[1:], conditionals...)
}
}()
}
result := stack[0].cmd.Wait()
successDespiteErrWg.Wait()
if successDispiteErr {
return nil
}
if result != nil {
e := errors.Wrap(fmt.Errorf("%s, %w", stack[0].cmd.String(), result), 0)
log.Error().Err(e).Send()
return e
}
return nil
}
// GetConditionalCheck return a function that can be used to check each line of output for content that indicates success.
func GetConditionalCheck(conditionals ...string) func(line string) bool {
var permissibleState bool
var permissibleConditions = make([]*regexp.Regexp, len(conditionals))
for i, conditional := range conditionals {
permissibleConditions[i] = regexp.MustCompile(conditional)
}
return func(line string) bool {
for _, pc := range permissibleConditions {
if pc.MatchString(line) {
permissibleState = true
}
}
return permissibleState
}
}
// HandleOutput take the supplied output and print it to screen checking for matches in the
// output to indicate if the output indicates success.
func HandleOutput(in io.ReadCloser, permitted func(string) bool, permissible chan<- bool) {
scanner := bufio.NewScanner(in)
scanner.Split(bufio.ScanLines)
var prev string
prev = ""
var p bool
for scanner.Scan() {
line := scanner.Text()
if ok := permitted(line); ok {
permissible <- true
p = true
}
if line != prev {
log.Debug().Msgf("\t| %s", line)
}
prev = line
}
if !p {
permissible <- false
}
}