cmdexec

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: GPL-3.0, LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cmdexec provides a testable abstraction for running external commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 wraps 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)
}

Executor runs external commands and captures their output.

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.

type MockCall

type MockCall struct {
	Name  string
	Args  []string
	Stdin string // captured stdin content (empty if no stdin)
}

MockCall records a single invocation of MockExecutor.

type MockExecutor

type MockExecutor struct {
	// OnCall, if set, is called to produce a result for each invocation.
	// The args slice contains the subcommand and its arguments
	// (e.g. ["inspect", "sind-dns"]). Stdin is non-empty for RunWithStdin calls.
	OnCall func(args []string, stdin string) MockResult

	Calls []MockCall
	// contains filtered or unexported fields
}

MockExecutor records all calls and returns preconfigured results. It is safe for concurrent use.

Results are dispatched in two modes:

  • If OnCall is set, it is called for every invocation to produce a result.
  • Otherwise, results queued via AddResult are returned in FIFO order.

OnCall is useful when multiple goroutines share a MockExecutor and result dispatch must be based on command arguments rather than call order.

func (*MockExecutor) AddResult

func (m *MockExecutor) AddResult(stdout, stderr string, err error)

AddResult enqueues a result to be returned by the next Run or RunWithStdin call. Only used when OnCall is nil.

func (*MockExecutor) Run

func (m *MockExecutor) Run(_ context.Context, name string, args ...string) (string, string, error)

Run implements Executor.

func (*MockExecutor) RunWithStdin

func (m *MockExecutor) RunWithStdin(_ context.Context, stdin io.Reader, name string, args ...string) (string, string, error)

RunWithStdin implements Executor.

type MockResult

type MockResult struct {
	Stdout string
	Stderr string
	Err    error
}

MockResult holds the return values for a single MockExecutor call.

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.

type RecordedCall

type RecordedCall struct {
	Name   string
	Args   []string
	Stdout string
	Stderr string
	Err    error
}

RecordedCall captures one invocation of the executor.

type Recorder

type Recorder struct {
	*RecordingExecutor
	// contains filtered or unexported fields
}

Recorder holds a RecordingExecutor and the underlying mock (if any). In unit mode, mock is non-nil and AddResult configures responses. In integration mode, mock is nil and AddResult is a no-op.

func NewIntegrationRecorder

func NewIntegrationRecorder() *Recorder

NewIntegrationRecorder returns a Recorder backed by an OSExecutor.

func NewMockRecorder

func NewMockRecorder() *Recorder

NewMockRecorder returns a Recorder backed by a MockExecutor.

func (*Recorder) AddResult

func (r *Recorder) AddResult(stdout, stderr string, err error)

AddResult queues a mock response. No-op in integration mode.

func (*Recorder) IsIntegration

func (r *Recorder) IsIntegration() bool

IsIntegration returns true when running against real executors.

type RecordingExecutor

type RecordingExecutor struct {
	Inner Executor
	// contains filtered or unexported fields
}

RecordingExecutor wraps another Executor and records all calls with their results. Useful for observing actual CLI I/O during tests.

func (*RecordingExecutor) Calls

func (r *RecordingExecutor) Calls() []RecordedCall

Calls returns a copy of all recorded calls.

func (*RecordingExecutor) Dump

func (r *RecordingExecutor) Dump() string

Dump returns a human-readable log of all recorded calls.

func (*RecordingExecutor) Run

func (r *RecordingExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)

Run implements Executor.

func (*RecordingExecutor) RunWithStdin

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

RunWithStdin implements Executor.

Jump to

Keyboard shortcuts

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