Skip to content

Commit

Permalink
Wait for cmd reads to complete before calling Wait()
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeZ committed Jun 4, 2020
1 parent eddf378 commit e58130c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"time"

"github.com/ararog/timeago"
Expand Down Expand Up @@ -129,14 +130,18 @@ func Command(name string, args ...string) (string, string, error) {
return "", "", err
}

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
stdout, errStdout = copyAndCapture(os.Stdout, stdoutIn)
}()

go func() {
stderr, errStderr = copyAndCapture(os.Stderr, stderrIn)
}()
stderr, errStderr = copyAndCapture(os.Stderr, stderrIn)

// call testCmd.Wait() only after reads from all pipes have completed
wg.Wait()
err = testCmd.Wait()
if err != nil {
return string(stdout), string(stderr), err
Expand Down

0 comments on commit e58130c

Please sign in to comment.