Skip to content

Commit 91770d8

Browse files
committed
Added executils.RunAndCaptureOutput
1 parent 818b0e7 commit 91770d8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: executils/process.go

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package executils
1717

1818
import (
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+
}

0 commit comments

Comments
 (0)