@@ -10,7 +10,7 @@ import (
1010 "time"
1111)
1212
13- //Command represents a single command which can be executed
13+ // Command represents a single command which can be executed
1414type Command struct {
1515 Command string
1616 Env []string
@@ -22,8 +22,9 @@ type Command struct {
2222 executed bool
2323 exitCode int
2424 // stderr and stdout retrieve the output after the command was executed
25- stderr bytes.Buffer
26- stdout bytes.Buffer
25+ stderr bytes.Buffer
26+ stdout bytes.Buffer
27+ combined bytes.Buffer
2728}
2829
2930// NewCommand creates a new command
@@ -68,8 +69,28 @@ func NewCommand(cmd string, options ...func(*Command)) *Command {
6869// c.Execute()
6970//
7071func WithStandardStreams (c * Command ) {
71- c .StdoutWriter = os .Stdout
72- c .StderrWriter = os .Stderr
72+ c .StdoutWriter = NewMultiplexedWriter (os .Stdout , & c .stdout , & c .combined )
73+ c .StderrWriter = NewMultiplexedWriter (os .Stderr , & c .stdout , & c .combined )
74+ }
75+
76+ // WithCustomStdout allows to add custom writers to stdout
77+ func WithCustomStdout (writers ... io.Writer ) func (c * Command ) {
78+ return func (c * Command ) {
79+ writers = append (writers , & c .stdout , & c .combined )
80+ c .StdoutWriter = NewMultiplexedWriter (writers ... )
81+
82+ c .StderrWriter = NewMultiplexedWriter (& c .stderr , & c .combined )
83+ }
84+ }
85+
86+ // WithCustomStderr allows to add custom writers to stderr
87+ func WithCustomStderr (writers ... io.Writer ) func (c * Command ) {
88+ return func (c * Command ) {
89+ writers = append (writers , & c .stderr , & c .combined )
90+ c .StderrWriter = NewMultiplexedWriter (writers ... )
91+
92+ c .StdoutWriter = NewMultiplexedWriter (& c .stdout , & c .combined )
93+ }
7394}
7495
7596// WithTimeout sets the timeout of the command
@@ -107,18 +128,24 @@ func (c *Command) AddEnv(key string, value string) {
107128 c .Env = append (c .Env , fmt .Sprintf ("%s=%s" , key , value ))
108129}
109130
110- //Stdout returns the output to stdout
131+ // Stdout returns the output to stdout
111132func (c * Command ) Stdout () string {
112133 c .isExecuted ("Stdout" )
113134 return c .stdout .String ()
114135}
115136
116- //Stderr returns the output to stderr
137+ // Stderr returns the output to stderr
117138func (c * Command ) Stderr () string {
118139 c .isExecuted ("Stderr" )
119140 return c .stderr .String ()
120141}
121142
143+ // Combined returns the combined output of stderr and stdout according to their timeline
144+ func (c * Command ) Combined () string {
145+ c .isExecuted ("Combined" )
146+ return c .combined .String ()
147+ }
148+
122149//ExitCode returns the exit code of the command
123150func (c * Command ) ExitCode () int {
124151 c .isExecuted ("ExitCode" )
0 commit comments