File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 1616package executils
1717
1818import (
19+ "bytes"
1920 "context"
2021 "io"
2122 "os"
@@ -174,3 +175,15 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
174175 }()
175176 return p .Wait ()
176177}
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