|
| 1 | +package pipe |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "sync/atomic" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "golang.org/x/sync/errgroup" |
| 14 | +) |
| 15 | + |
| 16 | +// commandStage is a pipeline `Stage` based on running an external |
| 17 | +// command and piping the data through its stdin and stdout. |
| 18 | +type commandStage struct { |
| 19 | + name string |
| 20 | + stdin io.Closer |
| 21 | + cmd *exec.Cmd |
| 22 | + done chan struct{} |
| 23 | + wg errgroup.Group |
| 24 | + stderr bytes.Buffer |
| 25 | + |
| 26 | + // If the context expired and we attempted to kill the command, |
| 27 | + // `ctx.Err()` is stored here. |
| 28 | + ctxErr atomic.Value |
| 29 | +} |
| 30 | + |
| 31 | +// Command returns a pipeline `Stage` based on the specified external |
| 32 | +// `command`, run with the given command-line `args`. Its stdin and |
| 33 | +// stdout are handled as usual, and its stderr is collected and |
| 34 | +// included in any `*exec.ExitError` that the command might emit. |
| 35 | +func Command(command string, args ...string) Stage { |
| 36 | + if len(command) == 0 { |
| 37 | + panic("attempt to create command with empty command") |
| 38 | + } |
| 39 | + |
| 40 | + cmd := exec.Command(command, args...) |
| 41 | + return CommandStage(command, cmd) |
| 42 | +} |
| 43 | + |
| 44 | +// Command returns a pipeline `Stage` with the name `name`, based on |
| 45 | +// the specified `cmd`. Its stdin and stdout are handled as usual, and |
| 46 | +// its stderr is collected and included in any `*exec.ExitError` that |
| 47 | +// the command might emit. |
| 48 | +func CommandStage(name string, cmd *exec.Cmd) Stage { |
| 49 | + return &commandStage{ |
| 50 | + name: name, |
| 51 | + cmd: cmd, |
| 52 | + done: make(chan struct{}), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (s *commandStage) Name() string { |
| 57 | + return s.name |
| 58 | +} |
| 59 | + |
| 60 | +func (s *commandStage) Start( |
| 61 | + ctx context.Context, env Env, stdin io.ReadCloser, |
| 62 | +) (io.ReadCloser, error) { |
| 63 | + if s.cmd.Dir == "" { |
| 64 | + s.cmd.Dir = env.Dir |
| 65 | + } |
| 66 | + |
| 67 | + if stdin != nil { |
| 68 | + s.cmd.Stdin = stdin |
| 69 | + // Also keep a copy so that we can close it when the command exits: |
| 70 | + s.stdin = stdin |
| 71 | + } |
| 72 | + |
| 73 | + stdout, err := s.cmd.StdoutPipe() |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + // If the caller hasn't arranged otherwise, read the command's |
| 79 | + // standard error into our `stderr` field: |
| 80 | + if s.cmd.Stderr == nil { |
| 81 | + // We can't just set `s.cmd.Stderr = &s.stderr`, because if we |
| 82 | + // do then `s.cmd.Wait()` doesn't wait to be sure that all |
| 83 | + // error output has been captured. By doing this ourselves, we |
| 84 | + // can be sure. |
| 85 | + p, err := s.cmd.StderrPipe() |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + s.wg.Go(func() error { |
| 90 | + _, err := io.Copy(&s.stderr, p) |
| 91 | + // We don't consider `ErrClosed` an error (FIXME: is this |
| 92 | + // correct?): |
| 93 | + if err != nil && !errors.Is(err, os.ErrClosed) { |
| 94 | + return err |
| 95 | + } |
| 96 | + return nil |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + // Put the command in its own process group, if possible: |
| 101 | + s.runInOwnProcessGroup() |
| 102 | + |
| 103 | + if err := s.cmd.Start(); err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + // Arrange for the process to be killed (gently) if the context |
| 108 | + // expires before the command exits normally: |
| 109 | + go func() { |
| 110 | + select { |
| 111 | + case <-ctx.Done(): |
| 112 | + s.kill(ctx.Err()) |
| 113 | + case <-s.done: |
| 114 | + // Process already done; no need to kill anything. |
| 115 | + } |
| 116 | + }() |
| 117 | + |
| 118 | + return stdout, nil |
| 119 | +} |
| 120 | + |
| 121 | +// filterCmdError interprets `err`, which was returned by `Cmd.Wait()` |
| 122 | +// (possibly `nil`), possibly modifying it or ignoring it. It returns |
| 123 | +// the error that should actually be returned to the caller (possibly |
| 124 | +// `nil`). |
| 125 | +func (s *commandStage) filterCmdError(err error) error { |
| 126 | + if err == nil { |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + eErr, ok := err.(*exec.ExitError) |
| 131 | + if !ok { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + ctxErr, ok := s.ctxErr.Load().(error) |
| 136 | + if ok { |
| 137 | + // If the process looks like it was killed by us, substitute |
| 138 | + // `ctxErr` for the process's own exit error. Note that this |
| 139 | + // doesn't do anything on Windows, where the `Signaled()` |
| 140 | + // method isn't implemented (it is hardcoded to return |
| 141 | + // `false`). |
| 142 | + ps, ok := eErr.ProcessState.Sys().(syscall.WaitStatus) |
| 143 | + if ok && ps.Signaled() && |
| 144 | + (ps.Signal() == syscall.SIGTERM || ps.Signal() == syscall.SIGKILL) { |
| 145 | + return ctxErr |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + eErr.Stderr = s.stderr.Bytes() |
| 150 | + return eErr |
| 151 | +} |
| 152 | + |
| 153 | +func (s *commandStage) Wait() error { |
| 154 | + defer close(s.done) |
| 155 | + |
| 156 | + // Make sure that any stderr is copied before `s.cmd.Wait()` |
| 157 | + // closes the read end of the pipe: |
| 158 | + wErr := s.wg.Wait() |
| 159 | + |
| 160 | + err := s.cmd.Wait() |
| 161 | + err = s.filterCmdError(err) |
| 162 | + |
| 163 | + if err == nil && wErr != nil { |
| 164 | + err = wErr |
| 165 | + } |
| 166 | + |
| 167 | + if s.stdin != nil { |
| 168 | + cErr := s.stdin.Close() |
| 169 | + if cErr != nil && err == nil { |
| 170 | + return cErr |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return err |
| 175 | +} |
0 commit comments