forked from davecheney/gcvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subcommand.go
55 lines (46 loc) · 865 Bytes
/
subcommand.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
50
51
52
53
54
55
package main
import (
"io"
"log"
"os"
"os/exec"
"sync"
)
type SubCommand struct {
cmd *exec.Cmd
PipeRead io.ReadCloser
pipeWrite io.WriteCloser
err error
errMtx sync.Mutex
}
func NewSubCommand(args []string) *SubCommand {
pipeRead, pipeWrite, err := os.Pipe()
if err != nil {
log.Fatal(err)
}
env := append(os.Environ(), "GODEBUG=gctrace=1")
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = env
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = pipeWrite
return &SubCommand{
cmd: cmd,
PipeRead: pipeRead,
pipeWrite: pipeWrite,
}
}
func (s *SubCommand) Run() {
s.setErr(s.cmd.Run())
s.pipeWrite.Close()
}
func (s *SubCommand) Err() error {
s.errMtx.Lock()
defer s.errMtx.Unlock()
return s.err
}
func (s *SubCommand) setErr(err error) {
s.errMtx.Lock()
defer s.errMtx.Unlock()
s.err = err
}