executor

package
v1.0.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTimeout = 5 * time.Second

DefaultTimeout is the recommended timeout for version detection commands. This matches the spec requirement (FR-006) of 5 seconds per binary.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandExecutor

type CommandExecutor interface {
	// ExecuteWithTimeout executes a command with a timeout.
	//
	// Parameters:
	//   - ctx: Context for cancellation (timeout should be set via context.WithTimeout)
	//   - name: Command name or path (e.g., "stabled", "/path/to/binary")
	//   - args: Command arguments (e.g., "version", "--format", "json")
	//
	// Returns:
	//   - Combined stdout and stderr output as bytes
	//   - Error if command fails, times out, or cannot be executed
	//
	// Timeout Behavior:
	//   - Command is killed if context deadline is exceeded
	//   - Returns context.DeadlineExceeded error on timeout
	//
	// Example:
	//   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	//   defer cancel()
	//   output, err := executor.ExecuteWithTimeout(ctx, "stabled", "version")
	ExecuteWithTimeout(ctx context.Context, name string, args ...string) ([]byte, error)
}

CommandExecutor abstracts command execution for testing. This interface allows the infrastructure layer to execute system commands while remaining testable through mocking.

Design Decision: Minimal interface focused on our use case (version detection) rather than exposing full exec.Cmd capabilities.

Security Note: Caller is responsible for validating and sanitizing command arguments to prevent command injection vulnerabilities.

type OSCommandExecutor

type OSCommandExecutor struct{}

OSCommandExecutor implements CommandExecutor using os/exec package. This is the production adapter that executes real system commands.

Usage in production:

executor := executor.NewOSCommandExecutor()
output, err := executor.ExecuteWithTimeout(ctx, "stabled", "version")

Usage in tests:

executor := &MockCommandExecutor{...}  // Test implementation

func NewOSCommandExecutor

func NewOSCommandExecutor() *OSCommandExecutor

NewOSCommandExecutor creates a new command executor using the real OS exec package. This is the default implementation used in production code.

func (*OSCommandExecutor) ExecuteWithTimeout

func (e *OSCommandExecutor) ExecuteWithTimeout(ctx context.Context, name string, args ...string) ([]byte, error)

ExecuteWithTimeout executes a command using exec.CommandContext.

Implementation Notes:

  • Uses CommandContext for automatic cancellation on timeout
  • Returns combined stdout and stderr via CombinedOutput()
  • Command is automatically killed when context is cancelled/times out

Error Handling:

  • Returns exec.ExitError if command exits with non-zero status
  • Returns context.DeadlineExceeded if timeout occurs
  • Returns exec.Error if command cannot be found/executed

Jump to

Keyboard shortcuts

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