command

package
v2.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package command provides an abstraction over exec.Command for testability.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunInJob added in v2.11.0

func RunInJob(cmd *exec.Cmd) error

RunInJob runs the command in a new process group with resource limits.

  • Process group ensures the entire tree is killed on timeout/cleanup
  • Pdeathsig kills the child if the parent dies unexpectedly
  • prlimit64 sets RLIMIT_AS (memory) and RLIMIT_CPU on the child
  • cmd.Cancel kills the process group (not just the top-level PID)

Types

type Executor

type Executor interface {
	// Run executes a command and waits for it to complete.
	// Returns an error if the command fails to start or exits with non-zero status.
	Run(ctx context.Context, name string, args ...string) error

	// Output runs a command and returns its standard output.
	// Returns the output bytes and an error if the command fails.
	Output(ctx context.Context, name string, args ...string) ([]byte, error)

	// Start starts a command without waiting for it to complete (fire-and-forget).
	// Returns an error if the command fails to start.
	Start(ctx context.Context, name string, args ...string) error

	// StartWithOptions starts a command with platform-specific options.
	// Returns an error if the command fails to start.
	StartWithOptions(ctx context.Context, opts StartOptions, name string, args ...string) error
}

Executor provides an abstraction over exec.Command for testability. This allows commands to be mocked in tests without executing real system commands.

type RealExecutor

type RealExecutor struct{}

RealExecutor uses actual exec.Command to execute system commands. This is the production implementation used in normal operation.

func (*RealExecutor) Output

func (*RealExecutor) Output(ctx context.Context, name string, args ...string) ([]byte, error)

Output runs a command and returns its standard output.

func (*RealExecutor) Run

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

Run executes a system command using exec.CommandContext.

func (*RealExecutor) Start

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

Start starts a command without waiting for it to complete.

func (*RealExecutor) StartWithOptions

func (*RealExecutor) StartWithOptions(
	ctx context.Context,
	_ StartOptions,
	name string,
	args ...string,
) error

StartWithOptions starts a command with platform-specific options on Unix. HideWindow option is ignored on non-Windows platforms.

type StartOptions

type StartOptions struct {
	// HideWindow prevents a console window from appearing (Windows-only).
	// On non-Windows platforms, this field is ignored.
	HideWindow bool
}

StartOptions configures command startup behavior.

Jump to

Keyboard shortcuts

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