cmdexec

package
v0.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: GPL-3.0, LGPL-3.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cmdexec provides a testable abstraction for running external commands.

Index

Constants

View Source
const TerminalWidth = 100

TerminalWidth is the width of the pseudo-terminal of WithTerminal, in columns: wide enough for docker pull to write its byte counts and the time left, too narrow for it to draw a bar as well.

Variables

This section is empty.

Functions

func Env added in v0.13.0

func Env(ctx context.Context) []string

Env returns the environment WithEnv put in ctx, or nil.

func InProcessGroup added in v0.13.0

func InProcessGroup(ctx context.Context) bool

InProcessGroup reports whether ctx is one WithProcessGroup returned.

func LineParser added in v0.13.0

func LineParser(ctx context.Context) func(progress.Stream, string)

LineParser returns the function WithLineParser put in ctx, or nil.

func Started added in v0.13.0

func Started(ctx context.Context) func(pid int)

Started returns the function WithStarted put in ctx, or nil.

func StderrMerged added in v0.13.0

func StderrMerged(ctx context.Context) bool

StderrMerged reports whether ctx is one WithStderrMerged returned.

func StdoutIsData added in v0.12.0

func StdoutIsData(ctx context.Context) bool

StdoutIsData reports whether ctx is one WithStdoutAsData returned.

func Terminal added in v0.14.0

func Terminal(ctx context.Context) bool

Terminal reports whether WithTerminal put a terminal in ctx.

func TerminalOpened added in v0.15.0

func TerminalOpened(ctx context.Context) func()

TerminalOpened returns the function WithTerminalOpened put in ctx, or nil.

func WithEnv added in v0.13.0

func WithEnv(ctx context.Context, env []string) context.Context

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

func WithLineParser(ctx context.Context, parse func(progress.Stream, string)) context.Context

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

func WithProcessGroup(ctx context.Context) context.Context

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

func WithStarted(ctx context.Context, started func(pid int)) context.Context

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

func WithStderrMerged(ctx context.Context) context.Context

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

func WithStdoutAsData(ctx context.Context) context.Context

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 WithTerminal added in v0.14.0

func WithTerminal(ctx context.Context) context.Context

WithTerminal returns ctx for a command that writes more to a terminal than to a pipe, such as docker pull, which writes the bytes each layer has downloaded only to a terminal: OSExecutor gives its standard output a pseudo-terminal of TerminalWidth columns, and turns what the command writes there into plain lines (NewTerminalLines) before it captures them or hands them to a line parser. Where it cannot open one, the command writes to a pipe, as without WithTerminal. Standard error stays a pipe.

func WithTerminalOpened added in v0.15.0

func WithTerminalOpened(ctx context.Context, opened func()) context.Context

WithTerminalOpened returns ctx for a command run WithTerminal whose caller needs to know that it got the terminal, as a parser of what docker pull writes does, which reads the bytes only where docker writes them: OSExecutor calls opened once it has opened the terminal, before the command starts, and not at all where it writes to a pipe instead.

func WrapExitError added in v0.10.0

func WrapExitError(err error, stderr string) error

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

type ExitError struct {
	Err    *exec.ExitError
	Stderr string
}

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

func (e *ExitError) Error() string

Error returns the exit status followed by the command's stderr, if any.

func (*ExitError) ExitCode added in v0.10.0

func (e *ExitError) ExitCode() int

ExitCode returns the command's exit code.

func (*ExitError) Unwrap added in v0.10.0

func (e *ExitError) Unwrap() error

Unwrap returns the underlying *exec.ExitError.

type LogFunc

type LogFunc func(ctx context.Context, cmd string)

LogFunc is called before each command execution with the context and the full command string (name + args joined by spaces).

type LoggingExecutor

type LoggingExecutor struct {
	Inner Executor
	Log   LogFunc
}

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.

func (*LoggingExecutor) RunWithStdin

func (l *LoggingExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)

RunWithStdin implements Executor.

func (*LoggingExecutor) Start added in v0.9.0

func (l *LoggingExecutor) Start(ctx context.Context, name string, args ...string) (*Process, error)

Start implements Executor.

type OSExecutor

type OSExecutor struct{}

OSExecutor runs commands using os/exec.

func (*OSExecutor) Run

func (e *OSExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)

Run implements Executor.

func (*OSExecutor) RunWithStdin

func (e *OSExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)

RunWithStdin implements Executor.

func (*OSExecutor) Start added in v0.9.0

func (e *OSExecutor) Start(ctx context.Context, name string, args ...string) (*Process, error)

Start implements Executor.

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

func Start(ctx context.Context, name string, args ...string) (*Process, error)

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.

func (*Process) Close added in v0.9.0

func (p *Process) Close() error

Close kills the process (if still running) and waits for it to exit. If the Process was created without a command (e.g. in tests), Close just closes Stdout. The "signal: killed" error from the intentional Kill is suppressed so normal shutdown returns nil.

type TerminalLines added in v0.14.0

type TerminalLines struct {
	// contains filtered or unexported fields
}

TerminalLines writes what a command wrote to a terminal as plain lines, each ended by "\n", without the escape sequences that move the cursor or erase. A carriage return, a line feed and a move of the cursor up or down end a line, as a program that redraws lines in place, such as the progress of docker pull, writes one line before each of them; a line of blanks only is left out, and the blanks around a line are cut. Other control characters are left out. It is not safe for concurrent use.

func NewTerminalLines added in v0.14.0

func NewTerminalLines(w io.Writer) *TerminalLines

NewTerminalLines returns a TerminalLines that writes its lines to w.

func (*TerminalLines) Flush added in v0.14.0

func (t *TerminalLines) Flush() error

Flush writes the line that has not ended, if any.

func (*TerminalLines) Write added in v0.14.0

func (t *TerminalLines) Write(p []byte) (int, error)

Write cuts p into lines, and writes those that ended to w at once. It returns len(p) unless w fails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL