|
30 | 30 | package utils
|
31 | 31 |
|
32 | 32 | import (
|
| 33 | + "bufio" |
33 | 34 | "bytes"
|
34 | 35 | "crypto/md5"
|
35 | 36 | "encoding/hex"
|
@@ -291,33 +292,62 @@ func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int)
|
291 | 292 | ctx.GetLogger().UnformattedFprintln(os.Stdout, PrintableCommand(command.Args))
|
292 | 293 | }
|
293 | 294 |
|
| 295 | + var stdoutCopy io.ReadCloser |
| 296 | + var stderrCopy io.ReadCloser |
| 297 | + |
294 | 298 | if stdout == Capture {
|
295 | 299 | buffer := &bytes.Buffer{}
|
296 | 300 | command.Stdout = buffer
|
297 | 301 | } else if stdout == Show || stdout == ShowIfVerbose && ctx.Verbose {
|
298 |
| - command.Stdout = os.Stdout |
| 302 | + stdoutCopy, _ = command.StdoutPipe() |
299 | 303 | }
|
300 | 304 |
|
301 | 305 | if stderr == Capture {
|
302 | 306 | buffer := &bytes.Buffer{}
|
303 | 307 | command.Stderr = buffer
|
304 | 308 | } else if stderr == Show || stderr == ShowIfVerbose && ctx.Verbose {
|
305 |
| - command.Stderr = os.Stderr |
| 309 | + stderrCopy, _ = command.StderrPipe() |
306 | 310 | }
|
307 | 311 |
|
308 | 312 | err := command.Start()
|
309 | 313 | if err != nil {
|
310 | 314 | return nil, nil, i18n.WrapError(err)
|
311 | 315 | }
|
312 | 316 |
|
313 |
| - err = command.Wait() |
314 |
| - |
315 | 317 | var outbytes, errbytes []byte
|
316 |
| - if buf, ok := command.Stdout.(*bytes.Buffer); ok { |
317 |
| - outbytes = buf.Bytes() |
| 318 | + |
| 319 | + if stdoutCopy != nil && stderrCopy != nil { |
| 320 | + stdOut := bufio.NewScanner(stdoutCopy) |
| 321 | + stdErr := bufio.NewScanner(stderrCopy) |
| 322 | + stdOut.Split(bufio.ScanLines) |
| 323 | + stdErr.Split(bufio.ScanLines) |
| 324 | + |
| 325 | + go func() { |
| 326 | + for stdOut.Scan() { |
| 327 | + txt := stdOut.Text() + "\n" |
| 328 | + fmt.Fprint(os.Stderr, txt) |
| 329 | + outbytes = append(outbytes, txt...) |
| 330 | + } |
| 331 | + }() |
| 332 | + |
| 333 | + go func() { |
| 334 | + for stdErr.Scan() { |
| 335 | + txt := stdErr.Text() + "\n" |
| 336 | + fmt.Fprint(os.Stderr, txt) |
| 337 | + errbytes = append(errbytes, txt...) |
| 338 | + } |
| 339 | + }() |
318 | 340 | }
|
319 |
| - if buf, ok := command.Stderr.(*bytes.Buffer); ok { |
320 |
| - errbytes = buf.Bytes() |
| 341 | + |
| 342 | + err = command.Wait() |
| 343 | + |
| 344 | + if stdoutCopy == nil || stderrCopy == nil { |
| 345 | + if buf, ok := command.Stdout.(*bytes.Buffer); ok { |
| 346 | + outbytes = buf.Bytes() |
| 347 | + } |
| 348 | + if buf, ok := command.Stderr.(*bytes.Buffer); ok { |
| 349 | + errbytes = buf.Bytes() |
| 350 | + } |
321 | 351 | }
|
322 | 352 |
|
323 | 353 | return outbytes, errbytes, i18n.WrapError(err)
|
|
0 commit comments