Documentation
¶
Overview ¶
Package cmdexec provides a testable abstraction for running external commands.
Index ¶
- func Env(ctx context.Context) []string
- func InProcessGroup(ctx context.Context) bool
- func LineParser(ctx context.Context) func(progress.Stream, string)
- func Started(ctx context.Context) func(pid int)
- func StderrMerged(ctx context.Context) bool
- func StdoutIsData(ctx context.Context) bool
- func WithEnv(ctx context.Context, env []string) context.Context
- func WithLineParser(ctx context.Context, parse func(progress.Stream, string)) context.Context
- func WithProcessGroup(ctx context.Context) context.Context
- func WithStarted(ctx context.Context, started func(pid int)) context.Context
- func WithStderrMerged(ctx context.Context) context.Context
- func WithStdoutAsData(ctx context.Context) context.Context
- 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 InProcessGroup ¶ added in v0.13.0
InProcessGroup reports whether ctx is one WithProcessGroup returned.
func LineParser ¶ added in v0.13.0
LineParser returns the function WithLineParser put in ctx, or nil.
func StderrMerged ¶ added in v0.13.0
StderrMerged reports whether ctx is one WithStderrMerged returned.
func StdoutIsData ¶ added in v0.12.0
StdoutIsData reports whether ctx is one WithStdoutAsData returned.
func WithEnv ¶ added in v0.13.0
WithEnv returns ctx for a command that OSExecutor runs with env, in the form of os.Environ, as its whole environment instead of the process's own. An empty env is the process's own, as without WithEnv.
func WithLineParser ¶ added in v0.13.0
WithLineParser returns ctx for a command whose output a caller follows line by line as it is written, such as the status lines of docker pull that tell how far an image has got: OSExecutor hands each line of its standard error, and of its standard output unless that is data (WithStdoutAsData), to parse as it ends, without its line break, with the stream it came from. parse is called from the goroutine that copies the stream, so the calls for the two streams may run at the same time, and must not block; a line that has not ended when the output does is none it is given. The output is captured as without a parser.
func WithProcessGroup ¶ added in v0.13.0
WithProcessGroup returns ctx for a command that OSExecutor runs in a process group of its own and, when ctx ends, kills with every process it started that is still in that group, such as a docker CLI. Killing the command alone would leave them running, holding its output open, so the command would be reported done only when they end.
func WithStarted ¶ added in v0.13.0
WithStarted returns ctx for a command whose process ID its caller needs while it runs, such as to signal it: OSExecutor calls started with the ID once the command has started, from the goroutine that runs it, before it waits for the command to end. It is not called for a command that cannot be started.
func WithStderrMerged ¶ added in v0.13.0
WithStderrMerged returns ctx for a long-lived command (Start) whose standard error is read with its standard output, as one stream: docker logs, for one, writes what the container wrote to its standard error to its own.
func WithStdoutAsData ¶ added in v0.12.0
WithStdoutAsData returns ctx for a command whose standard output is data its caller reads, such as an ID, a name or JSON, rather than lines that say what the command is doing: under a progress span that shows lines, OSExecutor then shows the lines of its standard error alone.
func WrapExitError ¶ added in v0.10.0
WrapExitError returns a bare *exec.ExitError as an *ExitError carrying stderr, and any other error unchanged. Executors call it on the result of a finished command; test doubles call it so that their errors have the same shape as the real ones.
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 stderr and
// wraps the *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.
Start(ctx context.Context, name string, args ...string) (*Process, error)
}
Executor runs external commands and captures their output.
type ExitError ¶ added in v0.10.0
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.
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 WithStderrMerged. The command is killed when ctx is cancelled.