Documentation ¶
Overview ¶
Package run provides a wrapper around os/exec with method chaining for modifying behaviour.
Index ¶
- Variables
- func ContextWithRunInfo(ctx context.Context, value *RunInfo) context.Context
- type MockFn
- type RunInfo
- func (r *RunInfo) CombinedOutput() ([]byte, error)
- func (r *RunInfo) Ctx(ctx context.Context) *RunInfo
- func (r *RunInfo) Dir(dir string) *RunInfo
- func (r *RunInfo) DiscardErr() *RunInfo
- func (r *RunInfo) Env(env ...string) *RunInfo
- func (r *RunInfo) In(input []byte) *RunInfo
- func (r *RunInfo) Log() *RunInfo
- func (r *RunInfo) Mock(fn MockFn) *RunInfo
- func (r *RunInfo) PrintErr() *RunInfodeprecated
- func (r *RunInfo) Run(w ...io.Writer) error
- func (r *RunInfo) STDOutOutput() ([]byte, error)
- func (r *RunInfo) SaveErr() *RunInfo
- func (r *RunInfo) Stdin() *RunInfo
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type RunInfo ¶
type RunInfo struct { Cmd []string // exposed for mocking purposes only Stdout io.Writer // exposed for mocking purposes only Stderr io.Writer // exposed for mocking purposes only // contains filtered or unexported fields }
func CMDCtx ¶ added in v0.8.0
CMD - Pulls RunInfo from context if it exists and if not it initializes a new one. Useful when loading a RunInfo from context to ease testing.
func (*RunInfo) CombinedOutput ¶
CombinedOutput - Runs given CMD and returns STDOut and STDErr combined.
func (*RunInfo) Ctx ¶ added in v0.5.0
Ctx - specifies the context of the command to allow for timeouts.
func (*RunInfo) DiscardErr ¶ added in v0.7.0
DiscardErr - Don't print command error to stderr by default.
func (*RunInfo) Run ¶
Run - wrapper around os/exec CMD.Run()
Run starts the specified command and waits for it to complete.
The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.
If the command starts but does not complete successfully, the error is of type *ExitError. Other error types may be returned for other situations.
Examples:
Run() // Output goes to os.Stdout and os.Stderr Run(out) // Sets the command's os.Stdout and os.Stderr to out. Run(out, outErr) // Sets the command's os.Stdout to out and os.Stderr to outErr.
func (*RunInfo) STDOutOutput ¶
STDOutOutput - Runs given CMD and returns STDOut only.
Stderr output is discarded unless a call to SaveErr() or PrintErr() was made.
func (*RunInfo) SaveErr ¶ added in v0.6.0
SaveErr - If the command starts but does not complete successfully, the error is of type *ExitError. In this case, save the error output into *ExitError.Stderr for retrieval.
Retrieval can be done as shown below:
err := run.CMD("./command", "arg").SaveErr().Run() // or .STDOutOutput() or .CombinedOutput() if err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { errOutput := exitErr.Stderr