-
Notifications
You must be signed in to change notification settings - Fork 90
/
process.go
49 lines (39 loc) · 915 Bytes
/
process.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
package applier
import (
"io/ioutil"
"os/exec"
"sync"
"github.com/pkg/errors"
)
func Run(cmd *exec.Cmd) ([]byte, []byte, error) {
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to create stdout reader")
}
defer stdoutReader.Close()
stderrReader, err := cmd.StderrPipe()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to create stderr reader")
}
defer stderrReader.Close()
err = cmd.Start()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to start commmand")
}
var stdout, stderr []byte
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
stdout, _ = ioutil.ReadAll(stdoutReader)
}()
wg.Add(1)
go func() {
defer wg.Done()
stderr, _ = ioutil.ReadAll(stderrReader)
}()
// cmd.Wait() must be called after all readers have completed
wg.Wait()
err = cmd.Wait()
return stdout, stderr, err
}