| 9 | "strings" |
| 10 | "sync" |
| 11 | "time" |
| 12 | ) |
| 13 |
| 14 | // SafeBuffer is a thread-safe wrapper around bytes.Buffer. |
| 15 | // It is safe to call Write and String concurrently. |
| 16 | type SafeBuffer struct { |
| 17 | mu sync.Mutex |
| 18 | buf bytes.Buffer |
| 19 | } |
| 20 |
| 21 | func (b *SafeBuffer) Write(p []byte) (int, error) { |
| 22 | b.mu.Lock() |
| 23 | defer b.mu.Unlock() |
| 24 | return b.buf.Write(p) |
| 25 | } |
| 26 |
| 27 | func (b *SafeBuffer) String() string { |
| 28 | b.mu.Lock() |
| 29 | defer b.mu.Unlock() |
| 30 | return b.buf.String() |
| 31 | } |
| 32 |
| 33 | type Process struct { |
| 34 | cmd *exec.Cmd |
| 35 | cancel context.CancelFunc |
| 36 | errorOutput *SafeBuffer |
| 37 | stdOutput *SafeBuffer |
| 38 |
| 39 | done chan struct{} |
| 40 | closeOne sync.Once |
| 41 | waitErr error |
| 42 |
| 43 | started bool |
| 44 | stopped bool |
| 45 | } |
| 46 |
| 47 | func NewWithEnvs(path string, params []string, envs []string) *Process { |
| 48 | ctx, cancel := context.WithCancel(context.Background()) |
| 49 | cmd := exec.CommandContext(ctx, path, params...) |
| 50 | cmd.Env = envs |
| 51 | p := &Process{ |
| 52 | cmd: cmd, |
| 53 | cancel: cancel, |
| 54 | done: make(chan struct{}), |
| 55 | } |
| 56 | p.errorOutput = &SafeBuffer{} |
| 57 | p.stdOutput = &SafeBuffer{} |
| 58 | cmd.Stderr = p.errorOutput |
| 59 | cmd.Stdout = p.stdOutput |
| 60 | return p |
| 61 | } |
| 62 |
| 63 | func (p *Process) Start() error { |
| 64 | if p.started { |
| 65 | return errors.New("process already started") |
| 66 | } |
| 67 | p.started = true |
| 68 |
| 69 | err := p.cmd.Start() |
| 70 | if err != nil { |
| 71 | p.waitErr = err |
| 72 | p.closeDone() |
| 73 | return err |
| 74 | } |
| 75 | go func() { |
| 76 | p.waitErr = p.cmd.Wait() |
| 77 | p.closeDone() |
| 78 | }() |
| 79 | return nil |
| 80 | } |
| 81 |
| 82 | func (p *Process) closeDone() { |
| 83 | p.closeOne.Do(func() { close(p.done) }) |
| 84 | } |
| 85 |
| 86 | // Done returns a channel that is closed when the process exits. |
| 87 | func (p *Process) Done() <-chan struct{} { |
| 88 | return p.done |
| 89 | } |
| 90 |
| 91 | func (p *Process) Stop() error { |
| 92 | if p.stopped || !p.started { |
| 93 | return nil |
| 94 | } |
| 95 | defer func() { |
| 96 | p.stopped = true |
| 97 | }() |
| 98 | p.cancel() |
| 99 | <-p.done |
| 100 | return p.waitErr |
| 101 | } |
| 102 |
| 103 | func (p *Process) ErrorOutput() string { |
| 104 | return p.errorOutput.String() |
| 105 | } |
| 106 |
| 107 | func (p *Process) StdOutput() string { |
| 108 | return p.stdOutput.String() |
| 109 | } |
| 110 |
| 111 | func (p *Process) Output() string { |
| 112 | return p.stdOutput.String() + p.errorOutput.String() |
| 113 | } |
| 114 |
| 115 | // CountOutput returns how many times pattern appears in the current accumulated output. |
| 116 | func (p *Process) CountOutput(pattern string) int { |
| 117 | return strings.Count(p.Output(), pattern) |
| 118 | } |
| 119 |
| 120 | // WaitForOutput polls the combined process output until the pattern is found |
| 121 | // count time(s) or the timeout is reached. It also returns early if the process exits. |
| 122 | func (p *Process) WaitForOutput(pattern string, count int, timeout time.Duration) error { |
| 123 | deadline := time.Now().Add(timeout) |
| 124 | for time.Now().Before(deadline) { |
| 125 | output := p.Output() |
| 126 | if strings.Count(output, pattern) >= count { |
| 127 | return nil |
| 128 | } |
| 129 | select { |
| 130 | case <-p.Done(): |
| 131 | // Process exited, check one last time. |
| 132 | output = p.Output() |
| 133 | if strings.Count(output, pattern) >= count { |
| 134 | return nil |
| 135 | } |
| 136 | return fmt.Errorf("process exited before %d occurrence(s) of %q found", count, pattern) |
| 137 | case <-time.After(25 * time.Millisecond): |
| 138 | } |
| 139 | } |
| 140 | return fmt.Errorf("timeout waiting for %d occurrence(s) of %q", count, pattern) |
| 141 | } |