@@ -11,6 +11,7 @@ import (
11
11
"sync"
12
12
)
13
13
14
+ // Process is intended to be used like exec.Cmd where possible.
14
15
type Process struct {
15
16
// CmdString takes a string and parses it into the relevant cmds
16
17
CmdString string
@@ -118,6 +119,8 @@ func (p *Process) findCmds() {
118
119
p .addCmd (cmd )
119
120
}
120
121
122
+ // lineReader takes will read a line in the io.Reader and write to the
123
+ // Process output buffer and use any OutputHandler that exists.
121
124
func (p * Process ) lineReader (wg * sync.WaitGroup , r io.Reader ) {
122
125
defer wg .Done ()
123
126
@@ -152,12 +155,16 @@ func (p *Process) lineReader(wg *sync.WaitGroup, r io.Reader) {
152
155
}
153
156
}
154
157
158
+ // checkErr shortens the creation of the pipes by bailing out with a
159
+ // log.Fatal.
155
160
func checkErr (msg string , err error ) {
156
161
if err != nil {
157
162
log .Fatalf ("%s: %s" , msg , err )
158
163
}
159
164
}
160
165
166
+ // setupOutputHandler configures the last cmd in the list of cmds to
167
+ // use the output handler defined.
161
168
func (p * Process ) setupOutputHandler (cmd * exec.Cmd ) error {
162
169
stdout , err := cmd .StdoutPipe ()
163
170
checkErr ("error creating stdout pipe" , err )
@@ -222,37 +229,7 @@ func (p *Process) wait() error {
222
229
return nil
223
230
}
224
231
225
- func (p * Process ) call (index int , wait bool ) error {
226
- // This hasn't already been started so start it
227
- if p .Cmds [index ].Process == nil {
228
- if err := p .Cmds [index ].Start (); err != nil {
229
- return err
230
- }
231
- }
232
-
233
- // See if we have more cmds to run and start them by recursively calling Call
234
- if len (p .Cmds [index :]) > 1 {
235
- err := p .Cmds [index + 1 ].Start ()
236
- if err != nil {
237
- return err
238
- }
239
-
240
- defer func () {
241
- if err == nil {
242
- p .Pipes [index ].Close ()
243
- err = p .call (index + 1 , wait )
244
- }
245
- }()
246
- }
247
-
248
- last := len (p .Cmds ) - 1
249
- if index == last && wait == true {
250
- return nil
251
- }
252
-
253
- return p .Cmds [index ].Wait ()
254
- }
255
-
232
+ // Run executes the cmds and returns the output as a string and any error.
256
233
func (p * Process ) Run () (string , error ) {
257
234
p .findCmds ()
258
235
p .setupPipes ()
@@ -268,16 +245,11 @@ func (p *Process) Run() (string, error) {
268
245
return p .outBuffer .String (), nil
269
246
}
270
247
248
+ // Start will start the list of cmds.
271
249
func (p * Process ) Start () error {
272
250
p .findCmds ()
273
251
p .setupPipes ()
274
252
275
- // if err := p.call(0, true); err != nil {
276
- // fmt.Printf("error calling command: %q\n", err)
277
- // fmt.Println(string(p.errBuffer.Bytes()))
278
- // return err
279
- // }
280
-
281
253
if err := p .start (); err != nil {
282
254
fmt .Printf ("error calling command: %q\n " , err )
283
255
fmt .Println (string (p .errBuffer .Bytes ()))
@@ -299,6 +271,7 @@ func (p *Process) Start() error {
299
271
return nil
300
272
}
301
273
274
+ // Wait will block, waiting for the commands to finish.
302
275
func (p * Process ) Wait () error {
303
276
if p .outputWait != nil {
304
277
p .outputWait .Wait ()
@@ -308,6 +281,7 @@ func (p *Process) Wait() error {
308
281
return p .Cmds [last ].Wait ()
309
282
}
310
283
284
+ // Output returns the buffered output as a string.
311
285
func (p * Process ) Output () string {
312
286
return p .outBuffer .String ()
313
287
}
0 commit comments