exec

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

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

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

func (e *CommandExecutor) RunBash(ctx context.Context, dir string, command string) ([]byte, error)

RunBash executes a command through bash -c. This is useful for complex commands that require bash features.

func (*CommandExecutor) RunBinary

func (e *CommandExecutor) RunBinary(ctx context.Context, dir string, binary string, args []string) ([]byte, error)

RunBinary executes a binary command with arguments. The binary can contain spaces (e.g., "php artisan") and will be properly split.

func (*CommandExecutor) RunShell

func (e *CommandExecutor) RunShell(ctx context.Context, dir string, command string) ([]byte, error)

RunShell executes a command through sh -c. This is more portable than bash but has fewer 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.

func (*MockCommander) WasCalled

func (m *MockCommander) WasCalled(command string, args ...string) bool

WasCalled checks if a command with the given arguments was ever executed. The command key must match exactly.

type RealCommander

type RealCommander struct{}

RealCommander executes commands using the real operating system. This is the production implementation that actually runs commands.

func (*RealCommander) Run

func (c *RealCommander) Run(ctx context.Context, dir string, command string, args ...string) ([]byte, error)

Run executes the command using exec.CommandContext. The command is executed in the specified directory with the provided arguments.

Jump to

Keyboard shortcuts

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