Documentation
¶
Overview ¶
Package exec provides interfaces and implementations for command execution. This abstraction allows for dependency injection and testing of steps that execute external commands.
Index ¶
- type CommandCall
- type CommandExecutor
- func (e *CommandExecutor) RunBash(ctx context.Context, dir string, command string) ([]byte, error)
- func (e *CommandExecutor) RunBinary(ctx context.Context, dir string, binary string, args []string) ([]byte, error)
- func (e *CommandExecutor) RunShell(ctx context.Context, dir string, command string) ([]byte, error)
- type CommandResponse
- type Commander
- type MockCommander
- func (m *MockCommander) CallCount() int
- func (m *MockCommander) GetCall(n int) *CommandCall
- func (m *MockCommander) LastCall() *CommandCall
- func (m *MockCommander) Reset()
- func (m *MockCommander) Run(ctx context.Context, dir string, command string, args ...string) ([]byte, error)
- func (m *MockCommander) SetResponse(command string, args []string, output []byte, err error)
- func (m *MockCommander) WasCalled(command string, args ...string) bool
- type RealCommander
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CommandCall ¶
type CommandCall struct {
// Dir is the working directory where the command was executed.
Dir string
// Command is the executable that was called.
Command string
// Args contains all arguments passed to the command.
Args []string
}
CommandCall records details of a single command execution.
type CommandExecutor ¶
type CommandExecutor struct {
// contains filtered or unexported fields
}
CommandExecutor provides a higher-level interface for common execution patterns. It wraps a Commander and provides convenience methods.
func NewCommandExecutor ¶
func NewCommandExecutor(commander Commander) *CommandExecutor
NewCommandExecutor creates a new CommandExecutor with the given Commander. If commander is nil, a RealCommander is used.
func (*CommandExecutor) RunBash ¶
RunBash executes a command through bash -c. This is useful for complex commands that require bash features.
type CommandResponse ¶
type CommandResponse struct {
// Output is the byte slice to return as the command output.
Output []byte
// Err is the error to return (nil for successful execution).
Err error
}
CommandResponse defines the response for a specific command.
type Commander ¶
type Commander interface {
// Run executes a command in the specified directory with the given arguments.
// Returns the combined stdout and stderr output, and any execution error.
Run(ctx context.Context, dir string, command string, args ...string) ([]byte, error)
}
Commander defines the interface for executing commands. Implementations can provide real command execution or mock behavior for testing.
type MockCommander ¶
type MockCommander struct {
// Responses maps command keys to their preset responses.
// The key is formatted as: "command arg1 arg2 ..."
Responses map[string]CommandResponse
// Calls records all commands that were executed.
// Each entry contains the full details of a command invocation.
Calls []CommandCall
}
MockCommander is a test double that records command calls and returns preset responses. Use this in tests to verify commands are executed correctly without actually running them.
func NewMockCommander ¶
func NewMockCommander() *MockCommander
NewMockCommander creates a new MockCommander with empty responses and calls.
func (*MockCommander) CallCount ¶
func (m *MockCommander) CallCount() int
CallCount returns the number of commands that have been executed.
func (*MockCommander) GetCall ¶
func (m *MockCommander) GetCall(n int) *CommandCall
GetCall returns the nth command call (0-indexed). Returns nil if n is out of range.
func (*MockCommander) LastCall ¶
func (m *MockCommander) LastCall() *CommandCall
LastCall returns the most recent command call. Returns nil if no commands have been executed.
func (*MockCommander) Reset ¶
func (m *MockCommander) Reset()
Reset clears all recorded calls and responses.
func (*MockCommander) Run ¶
func (m *MockCommander) Run(ctx context.Context, dir string, command string, args ...string) ([]byte, error)
Run records the command call and returns the preset response if one exists. The command key is constructed as "command arg1 arg2 ...". If no response is found for the key, it returns nil, nil.
func (*MockCommander) SetResponse ¶
func (m *MockCommander) SetResponse(command string, args []string, output []byte, err error)
SetResponse configures a preset response for a specific command. The command key is automatically built from the command and args.
type RealCommander ¶
type RealCommander struct{}
RealCommander executes commands using the real operating system. This is the production implementation that actually runs commands.