Commit 6e1b5211 authored by ale's avatar ale

Fix error handling in shell pipelines

parent 54c6eca8
...@@ -120,10 +120,21 @@ func (s *Shell) RunStdoutPipe(ctx context.Context, arg string, fn func(io.Reader ...@@ -120,10 +120,21 @@ func (s *Shell) RunStdoutPipe(ctx context.Context, arg string, fn func(io.Reader
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return err return err
} }
if err := fn(stdout); err != nil {
return err // The error from fn takes precedence over the command exit
// status. We still need to call cmd.Wait() in any case.
ferr := fn(stdout)
if ferr != nil {
// By calling stdout.Close() early we're hoping that
// the child process gets a SIGPIPE.
stdout.Close()
}
werr := cmd.Wait()
if ferr != nil {
return ferr
} }
return cmd.Wait() return werr
} }
// RunStdinPipe runs a command with a function connected to its // RunStdinPipe runs a command with a function connected to its
...@@ -138,10 +149,17 @@ func (s *Shell) RunStdinPipe(ctx context.Context, arg string, fn func(io.Writer) ...@@ -138,10 +149,17 @@ func (s *Shell) RunStdinPipe(ctx context.Context, arg string, fn func(io.Writer)
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return err return err
} }
if err := fn(stdin); err != nil {
return err // The error from fn takes precedence over the command exit
// status. We still need to call cmd.Wait() in any case.
ferr := fn(stdin)
stdin.Close() // nolint
werr := cmd.Wait()
if ferr != nil {
return ferr
} }
return cmd.Wait() return werr
} }
// Run a command. // Run a command.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment