Skip to content

Commit

Permalink
add exec.RunWithStdoutReader
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed May 2, 2019
1 parent 2779e4b commit bd26a8d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,29 @@ func RunLoggingOutputOnFail(cmd Cmd) error {
}
return err
}

// RunWithStdoutReader runs cmd with stdout piped to readerFunc
func RunWithStdoutReader(cmd Cmd, readerFunc func(io.Reader) error) error {
pr, pw, err := os.Pipe()
if err != nil {
return err
}
defer pw.Close()
defer pr.Close()
cmd.SetStdout(pw)

errChan := make(chan error, 1)
go func() {
errChan <- readerFunc(pr)
}()

err = cmd.Run()
err2 := <-errChan
if err != nil {
return err
}
if err2 != nil {
return err2
}
return nil
}

0 comments on commit bd26a8d

Please sign in to comment.