Documentation
¶
Overview ¶
Package cmdexec provides a testable abstraction for running external commands.
Index ¶
- func WrapExitError(err error, stderr string) error
- type Executor
- type ExitError
- type LogFunc
- type LoggingExecutor
- func (l *LoggingExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)
- func (l *LoggingExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)
- func (l *LoggingExecutor) Start(ctx context.Context, name string, args ...string) (*Process, error)
- type OSExecutor
- func (e *OSExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)
- func (e *OSExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)
- func (e *OSExecutor) Start(ctx context.Context, name string, args ...string) (*Process, error)
- type Process
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WrapExitError ¶ added in v0.10.0
WrapExitError returns a bare *exec.ExitError as an *ExitError carrying its exit code and stderr, and any other error unchanged. Executors call it on the result of a finished command.
Types ¶
type Executor ¶
type Executor interface {
// Run executes the named command with the given arguments and returns
// the captured stdout and stderr. If the command exits with a non-zero
// status, the returned error is an *ExitError that carries its exit
// code and stderr (NewExitError, or WrapExitError of an
// *exec.ExitError).
Run(ctx context.Context, name string, args ...string) (stdout string, stderr string, err error)
// RunWithStdin is like Run but pipes the given reader to the command's stdin.
RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (stdout string, stderr string, err error)
// Start starts a long-lived command and returns a Process for reading
// its stdout incrementally. The command is killed when ctx is cancelled.
// The caller must call Process.Close to release resources. An Executor
// that runs no local process returns a Process with only Stdout set,
// whose Close ends the command and returns an *ExitError for one that
// failed.
Start(ctx context.Context, name string, args ...string) (*Process, error)
}
Executor runs external commands and captures their output.
sind's own packages give OSExecutor options in the context of a call, such as a pseudo-terminal for docker pull, the lines of a command's output for a progress display, or the environment of a command that the CLI runs. Those that a library call sets change only what is shown or kept of the output, so an Executor of a library caller may ignore the context apart from its end.
type ExitError ¶ added in v0.10.0
type ExitError struct {
// Code is the command's exit code, -1 for a command that a signal
// ended.
Code int
// Stderr is what the command wrote to its standard error.
Stderr string
// Err is the error the command ended with, such as the *exec.ExitError
// of OSExecutor, or nil.
Err error
}
ExitError is the error of a command that ran and exited with a non-zero status. It keeps what the command wrote to stderr, which for docker is the only place that says what went wrong: "exit status 1" alone cannot tell a missing volume from one that is still in use.
It carries its own exit code, so that an Executor that does not run a local process, or a test double, can return one with NewExitError.
func NewExitError ¶ added in v0.18.0
NewExitError returns the error of a command that exited with code and wrote stderr.
func (*ExitError) Error ¶ added in v0.10.0
Error returns the exit status followed by the command's stderr, if any.
type LogFunc ¶
LogFunc is called before each command execution with the context and the full command string (name + args joined by spaces).
type LoggingExecutor ¶
LoggingExecutor wraps another Executor and calls a LogFunc before each command invocation.
func (*LoggingExecutor) Run ¶
func (l *LoggingExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)
Run implements Executor.
type OSExecutor ¶
type OSExecutor struct{}
OSExecutor runs commands using os/exec.
type Process ¶ added in v0.9.0
type Process struct {
Stdout io.ReadCloser
// contains filtered or unexported fields
}
Process represents a running long-lived command whose stdout can be read incrementally. The caller must call Close to release resources.
func Start ¶ added in v0.9.0
Start starts a command and returns a Process for reading its stdout, and its stderr too under execopt.WithStderrMerged. The command is killed when ctx is cancelled. Outside execopt.WithStderrMerged, the Process keeps the first and the last 2 KiB of the command's stderr for the *ExitError of Close, which says why a stream that failed ended.
The command's stdin is a pipe that stays open, without data, until the command has ended or Close kills it, so that a docker exec -i whose command ends with its stdin (docker.TiedExecArgs) runs as long as the docker CLI.
As OSExecutor.Run does, Start runs the command with the environment of execopt.WithEnv, and in a process group of its own under execopt.WithProcessGroup, which the end of ctx and Close kill whole.
A command that never reads its stdin, such as docker events, does not see that pipe close when this process dies. Under execopt.WithDiesWithParent the kernel kills it then (Linux).
func (*Process) Close ¶ added in v0.9.0
Close kills the process (if still running), waits for it to exit and closes Stdout, in either mode of Start. If the Process was created without a command, as a test double or an Executor that runs no local process creates it, Close just closes Stdout and returns its error, so that Stdout's Close can end the command and report how it exited. The "signal: killed" error from the intentional Kill is suppressed so normal shutdown returns nil; a command that exited with a non-zero status by itself, as a stream that failed, returns an *ExitError with what Start kept of its stderr. Later calls return the result of the first.