|
| 1 | +package asyncexec |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "os/exec" |
| 6 | +) |
| 7 | + |
| 8 | +// New instantiate a new Cmd object. |
| 9 | +func New(cmd *exec.Cmd, buff int) *Cmd { |
| 10 | + return &Cmd{ |
| 11 | + cmd: cmd, |
| 12 | + stopCh: make(chan struct{}), |
| 13 | + stdoutCh: make(chan []byte, buff), |
| 14 | + stderrCh: make(chan []byte, buff), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Cmd is a wrapper arround exec.Cmd. Mainly used to execute |
| 19 | +// command asynchronously with/or without output stream. |
| 20 | +type Cmd struct { |
| 21 | + cmd *exec.Cmd |
| 22 | + |
| 23 | + stopCh chan struct{} |
| 24 | + stdoutCh chan []byte |
| 25 | + stderrCh chan []byte |
| 26 | +} |
| 27 | + |
| 28 | +// Run runs the command. if streamOutput is true, it will spin |
| 29 | +// two goroutine responsible of streaming the stdout and stderr |
| 30 | +func (c *Cmd) Run() error { |
| 31 | + cmdStdoutReader, err := c.cmd.StdoutPipe() |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + stdoutScanner := bufio.NewScanner(cmdStdoutReader) |
| 36 | + |
| 37 | + cmdStderrReader, err := c.cmd.StderrPipe() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + stderrScanner := bufio.NewScanner(cmdStderrReader) |
| 42 | + |
| 43 | + // Goroutine for stdout |
| 44 | + go func() { |
| 45 | + defer close(c.stdoutCh) |
| 46 | + for stdoutScanner.Scan() { |
| 47 | + bytes := stdoutScanner.Bytes() |
| 48 | + c.stdoutCh <- bytes |
| 49 | + } |
| 50 | + }() |
| 51 | + |
| 52 | + // Goroutine for stderr |
| 53 | + go func() { |
| 54 | + defer close(c.stderrCh) |
| 55 | + for stderrScanner.Scan() { |
| 56 | + bytes := stderrScanner.Bytes() |
| 57 | + c.stderrCh <- bytes |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + err = c.cmd.Start() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + // listening for stop signal |
| 67 | + go func() { |
| 68 | + <-c.stopCh |
| 69 | + c.cmd.Process.Kill() |
| 70 | + }() |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// Exited returns true if the command exited, false otherwise. |
| 76 | +func (c *Cmd) Exited() bool { |
| 77 | + return c.cmd.ProcessState.Exited() |
| 78 | +} |
| 79 | + |
| 80 | +// ExitCode returns the command process exit code. |
| 81 | +func (c *Cmd) ExitCode() int { |
| 82 | + return c.cmd.ProcessState.ExitCode() |
| 83 | +} |
| 84 | + |
| 85 | +// StdoutStream returns a channel streaming the command Stdout. |
| 86 | +func (c *Cmd) StdoutStream() <-chan []byte { |
| 87 | + return c.stdoutCh |
| 88 | +} |
| 89 | + |
| 90 | +// StderrStream returns a channel streaming the command Stderr. |
| 91 | +func (c *Cmd) StderrStream() <-chan []byte { |
| 92 | + return c.stderrCh |
| 93 | +} |
| 94 | + |
| 95 | +// Wait blocks until the command exits |
| 96 | +func (c *Cmd) Wait() error { |
| 97 | + return c.cmd.Wait() |
| 98 | +} |
| 99 | + |
| 100 | +// Stop signals the Wrapper to kill the process running the command. |
| 101 | +func (c *Cmd) Stop() { |
| 102 | + c.stopCh <- struct{}{} |
| 103 | +} |
0 commit comments