Wait for cmd reads to complete before calling Wait()

pull/626/head
zeeZ 2020-06-04 14:28:27 +02:00
parent eddf378938
commit e58130cc6b
1 changed files with 8 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/ararog/timeago" "github.com/ararog/timeago"
@ -129,14 +130,18 @@ func Command(name string, args ...string) (string, string, error) {
return "", "", err return "", "", err
} }
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
stdout, errStdout = copyAndCapture(os.Stdout, stdoutIn) 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() err = testCmd.Wait()
if err != nil { if err != nil {
return string(stdout), string(stderr), err return string(stdout), string(stderr), err