-
Notifications
You must be signed in to change notification settings - Fork 15
/
cmd.go
103 lines (86 loc) · 2.06 KB
/
cmd.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package asyncexec
import (
"bufio"
"os/exec"
)
// New instantiate a new Cmd object.
func New(cmd *exec.Cmd, buff int) *Cmd {
return &Cmd{
cmd: cmd,
stopCh: make(chan struct{}),
stdoutCh: make(chan []byte, buff),
stderrCh: make(chan []byte, buff),
}
}
// Cmd is a wrapper arround exec.Cmd. Mainly used to execute
// command asynchronously with/or without output stream.
type Cmd struct {
cmd *exec.Cmd
stopCh chan struct{}
stdoutCh chan []byte
stderrCh chan []byte
}
// Run runs the command. if streamOutput is true, it will spin
// two goroutine responsible of streaming the stdout and stderr
func (c *Cmd) Run() error {
cmdStdoutReader, err := c.cmd.StdoutPipe()
if err != nil {
return err
}
stdoutScanner := bufio.NewScanner(cmdStdoutReader)
cmdStderrReader, err := c.cmd.StderrPipe()
if err != nil {
return err
}
stderrScanner := bufio.NewScanner(cmdStderrReader)
// Goroutine for stdout
go func() {
defer close(c.stdoutCh)
for stdoutScanner.Scan() {
bytes := stdoutScanner.Bytes()
c.stdoutCh <- bytes
}
}()
// Goroutine for stderr
go func() {
defer close(c.stderrCh)
for stderrScanner.Scan() {
bytes := stderrScanner.Bytes()
c.stderrCh <- bytes
}
}()
err = c.cmd.Start()
if err != nil {
return err
}
// listening for stop signal
go func() {
<-c.stopCh
c.cmd.Process.Kill()
}()
return nil
}
// Exited returns true if the command exited, false otherwise.
func (c *Cmd) Exited() bool {
return c.cmd.ProcessState.Exited()
}
// ExitCode returns the command process exit code.
func (c *Cmd) ExitCode() int {
return c.cmd.ProcessState.ExitCode()
}
// StdoutStream returns a channel streaming the command Stdout.
func (c *Cmd) StdoutStream() <-chan []byte {
return c.stdoutCh
}
// StderrStream returns a channel streaming the command Stderr.
func (c *Cmd) StderrStream() <-chan []byte {
return c.stderrCh
}
// Wait blocks until the command exits
func (c *Cmd) Wait() error {
return c.cmd.Wait()
}
// Stop signals the Wrapper to kill the process running the command.
func (c *Cmd) Stop() {
c.stopCh <- struct{}{}
}