exec

package
v0.4.1-rc3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package exec provides a client and interface for executing external commands. It abstracts the underlying os/exec calls, allowing for easier testing and consistent command execution logic throughout the application.

The primary components are:

  • CommandExecutor: An interface defining methods to run commands and capture output.
  • OSCommandExecutor: The default implementation of CommandExecutor using os/exec.
  • ExecutorClient: A client that uses a CommandExecutor to provide higher-level methods for command execution.

Usage:

// In your application setup (e.g., cmd/root.go or per command)
osExecutor := exec.NewOSCommandExecutor(someLogger) // Pass an *slog.Logger
execClient := exec.NewClient(osExecutor)

// Later, to run a command:
err := execClient.Execute(ctx, "/tmp", "ls", "-l")
if err != nil {
	// handle error
}

// To capture output:
stdout, stderr, err := execClient.CaptureOutput(ctx, ".", "go", "version")
if err != nil {
	// handle error
}
fmt.Printf("Go version: %s", stdout)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandExecutor

type CommandExecutor interface {
	// Execute runs a command, connecting stdio to the parent process's stdio.
	// dir: the working directory for the command.
	// commandName: the name or path of the command to run.
	// args: arguments for the command.
	// Returns an error if execution fails.
	Execute(ctx context.Context, dir string, commandName string, args ...string) error

	// CaptureOutput runs a command, capturing its stdout and stderr.
	// dir: the working directory for the command.
	// commandName: the name or path of the command to run.
	// args: arguments for the command.
	// Returns stdout, stderr, and any error (including *exec.PkgExitError).
	CaptureOutput(
		ctx context.Context,
		dir string,
		commandName string,
		args ...string,
	) (stdout, stderr string, err error)

	// CommandExists checks if a command is available in the PATH or at the specified path.
	CommandExists(commandName string) bool

	// Logger returns the logger associated with this executor.
	Logger() *slog.Logger
}

CommandExecutor defines the interface for running external commands. Implementations of this interface handle the actual execution logic.

func NewOSCommandExecutor

func NewOSCommandExecutor(logger *slog.Logger) CommandExecutor

NewOSCommandExecutor creates a new OSCommandExecutor. If logger is nil, a discard logger will be used.

type ExecutorClient

type ExecutorClient struct {
	// contains filtered or unexported fields
}

ExecutorClient provides a high-level interface for running external commands. It uses an underlying CommandExecutor for the actual execution.

func NewClient

func NewClient(executor CommandExecutor) *ExecutorClient

NewClient creates a new ExecutorClient with the given CommandExecutor.

func (*ExecutorClient) CaptureOutput

func (c *ExecutorClient) CaptureOutput(
	ctx context.Context,
	dir string,
	commandName string,
	args ...string,
) (string, string, error)

CaptureOutput runs a command and captures its stdout and stderr. See CommandExecutor.CaptureOutput.

func (*ExecutorClient) CommandExists

func (c *ExecutorClient) CommandExists(commandName string) bool

CommandExists checks if a command is available. See CommandExecutor.CommandExists.

func (*ExecutorClient) Execute

func (c *ExecutorClient) Execute(
	ctx context.Context,
	dir string,
	commandName string,
	args ...string,
) error

Execute runs a command, typically piping stdio. See CommandExecutor.Execute.

func (*ExecutorClient) Logger

func (c *ExecutorClient) Logger() *slog.Logger

Logger returns the logger from the underlying executor.

func (*ExecutorClient) UnderlyingExecutor added in v0.1.1

func (c *ExecutorClient) UnderlyingExecutor() CommandExecutor

UnderlyingExecutor returns the CommandExecutor used by this client. This allows passing the raw executor to other components if needed.

type OSCommandExecutor

type OSCommandExecutor struct {
	// contains filtered or unexported fields
}

OSCommandExecutor is the default implementation of CommandExecutor using the os/exec package.

func (*OSCommandExecutor) CaptureOutput

func (e *OSCommandExecutor) CaptureOutput(
	ctx context.Context,
	dir string,
	commandName string,
	args ...string,
) (string, string, error)

CaptureOutput runs a command and captures its output.

func (*OSCommandExecutor) CommandExists

func (e *OSCommandExecutor) CommandExists(commandName string) bool

CommandExists checks if a command exists in the path.

func (*OSCommandExecutor) Execute

func (e *OSCommandExecutor) Execute(
	ctx context.Context,
	dir string,
	commandName string,
	args ...string,
) error

Execute runs a command, piping stdio.

func (*OSCommandExecutor) Logger

func (e *OSCommandExecutor) Logger() *slog.Logger

Logger returns the logger associated with this executor.

Jump to

Keyboard shortcuts

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