Skip to content

Commit

Permalink
Merge pull request #258 from ericfialkowski/helpers
Browse files Browse the repository at this point in the history
Added WithStdout and WithStderr helpers
  • Loading branch information
dnephin committed Mar 17, 2023
2 parents 88af834 + ee9f52a commit f83b3c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion icmd/command.go
Expand Up @@ -195,6 +195,7 @@ type Cmd struct {
Timeout time.Duration
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Dir string
Env []string
ExtraFiles []*os.File
Expand Down Expand Up @@ -252,7 +253,11 @@ func buildCmd(cmd Cmd) *Result {
} else {
execCmd.Stdout = outBuffer
}
execCmd.Stderr = errBuffer
if cmd.Stderr != nil {
execCmd.Stderr = io.MultiWriter(errBuffer, cmd.Stderr)
} else {
execCmd.Stderr = errBuffer
}
execCmd.ExtraFiles = cmd.ExtraFiles

return &Result{
Expand Down
14 changes: 14 additions & 0 deletions icmd/ops.go
Expand Up @@ -38,6 +38,20 @@ func WithStdin(r io.Reader) CmdOp {
}
}

// WithStdout sets the standard output of the command to the specified writer
func WithStdout(w io.Writer) CmdOp {
return func(c *Cmd) {
c.Stdout = w
}
}

// WithStderr sets the standard error of the command to the specified writer
func WithStderr(w io.Writer) CmdOp {
return func(c *Cmd) {
c.Stderr = w
}
}

// WithExtraFile adds a file descriptor to the command
func WithExtraFile(f *os.File) CmdOp {
return func(c *Cmd) {
Expand Down

0 comments on commit f83b3c8

Please sign in to comment.