File tree 1 file changed +13
-0
lines changed
1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 16
16
package executils
17
17
18
18
import (
19
+ "bytes"
19
20
"context"
20
21
"io"
21
22
"os"
@@ -174,3 +175,15 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
174
175
}()
175
176
return p .Wait ()
176
177
}
178
+
179
+ // RunAndCaptureOutput starts the specified command and waits for it to complete. If the given context
180
+ // is canceled before the normal process termination, the process is killed. The standard output and
181
+ // standard error of the process are captured and returned at process termination.
182
+ func (p * Process ) RunAndCaptureOutput (ctx context.Context ) ([]byte , []byte , error ) {
183
+ stdout := & bytes.Buffer {}
184
+ stderr := & bytes.Buffer {}
185
+ p .RedirectStdoutTo (stdout )
186
+ p .RedirectStderrTo (stderr )
187
+ err := p .RunWithinContext (ctx )
188
+ return stdout .Bytes (), stderr .Bytes (), err
189
+ }
You can’t perform that action at this time.
0 commit comments