exc

package
v0.14.11 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package exc provides a thin, ergonomic wrapper around the standard library's 'os/exec' package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	ID        string
	Labels    dt.OrderedSet[string]
	Name      string
	Args      []string
	Env       dt.OrderedMap[string, string]
	Directory string
	Input     io.Reader
	Output    io.Writer
	Error     io.Writer
}

Command describes an external process. All fields are public and may be set directly, or through the fluent builder methods. Always set, at least, 'Name' before calling 'Run', 'Exec, or 'Start'.

The methods all return the same *Command pointer, so calls can be chained: new(Command).WithName("echo").WithArgs("hello").

func (*Command) Clone

func (cmd *Command) Clone() *Command

Clone returns a new Command with all fields copied. The Args slice is cloned so mutations to one do not affect the other. Input, Output, and Error share the same underlying io.Reader/Writer values as the original, since those cannot be meaningfully copied.

func (*Command) Exec

func (cmd *Command) Exec(ctx context.Context) (io.Reader, error)

Exec runs the command and returns its stdout as a reader. Both stdout and stderr are buffered internally. If the process fails, Exec returns a nil reader and an *Error containing the captured stderr, stdout, and the underlying exec error. On success the returned reader contains stdout.

func (*Command) Format

func (cmd *Command) Format(f fmt.State, verb rune)

Format implements fmt.Formatter. For %v and %+v the exc.Command<[+ID][+LABELS] command>. %q encloses with brackets.)

func (*Command) HasLabel

func (cmd *Command) HasLabel(label string) bool

HasLabel reports whether label is in the command's label set.

func (*Command) ResentEnv

func (cmd *Command) ResentEnv() *Command

ResentEnv clears all explicitly set environment variables and returns the receiver. After this call the process inherits the parent environment.

func (*Command) ResetArgs

func (cmd *Command) ResetArgs() *Command

ResetArgs clears the argument list and returns the receiver.

func (*Command) ResetIO

func (cmd *Command) ResetIO() *Command

ResetIO clears stdin, stdout, and stderr and returns the receiver.

func (*Command) ResetLabels

func (cmd *Command) ResetLabels() *Command

ResetLabels removes all labels from the command and returns the receiver.

func (*Command) ResetStdError

func (cmd *Command) ResetStdError() *Command

ResetStdError clears the stderr writer and returns the receiver.

func (*Command) ResetStdInput

func (cmd *Command) ResetStdInput() *Command

ResetStdInput clears the stdin reader and returns the receiver.

func (*Command) ResetStdOutput

func (cmd *Command) ResetStdOutput() *Command

ResetStdOutput clears the stdout writer and returns the receiver.

func (*Command) Resolve

func (cmd *Command) Resolve(ctx context.Context) *exec.Cmd

Resolve builds an *exec.Cmd from the Command fields without running it. Useful when you need direct access to the underlying exec.Cmd, for example to set additional fields before calling Start or Run yourself.

func (*Command) Run

func (cmd *Command) Run(ctx context.Context) error

Run executes the command, waits for it to finish, and returns any error. stdout and stderr are routed to cmd.Output and cmd.Error respectively, or discarded if those fields are nil. Use Exec to capture output as a reader, or Start for non-blocking execution.

func (*Command) SSH

func (cmd *Command) SSH(host string, args ...string) *Command

SSH sets the command to ssh and passes host followed by args directly to the ssh binary. host may include a user prefix (user@host). args may contain ssh options, a remote command, or both, in the order ssh expects them.

func (*Command) SetArgs

func (cmd *Command) SetArgs(a []string) *Command

SetArgs replaces the argument list with a and returns the receiver.

func (*Command) SetEnvVar

func (cmd *Command) SetEnvVar(k, v string) *Command

SetEnvVar adds or replaces an environment variable in the command's explicit environment and returns the receiver. Variables set here are passed to the process instead of inheriting the parent environment.

func (*Command) Shell

func (cmd *Command) Shell(shell string, block string) *Command

Shell runs code using the specified shell (or path to shell binary,) and runs the 'block' accordingly (via the '-c' option). Usable with bash/zsh/dash/sh/nu/fish/python/python3.

func (*Command) Start

func (cmd *Command) Start(ctx context.Context) fnx.Worker

Start launches the command asynchronously and returns a fnx.Worker that blocks until the process exits or the worker's context is cancelled. If the command fails to start, a worker that immediately returns the start error is returned. Cancelling the context passed to Start terminates the process; cancelling the context passed to the returned worker unblocks the caller without killing the process.

func (*Command) UnsetEnvVar

func (cmd *Command) UnsetEnvVar(k string) *Command

UnsetEnvVar removes an environment variable from the command's explicit environment and returns the receiver.

func (*Command) WithArgs

func (cmd *Command) WithArgs(a ...string) *Command

WithArgs replaces the argument list with the provided values and returns the receiver.

func (*Command) WithDirectory

func (cmd *Command) WithDirectory(d string) *Command

WithDirectory sets the working directory for the process and returns the receiver.

func (*Command) WithID

func (cmd *Command) WithID(id string) *Command

WithID sets the user-supplied identifier for the command and returns the receiver. The ID is not passed to the underlying process; it is used only for logging and formatting.

func (*Command) WithLabel

func (cmd *Command) WithLabel(label string) *Command

WithLabel adds label to the command's label set and returns the receiver.

func (*Command) WithName

func (cmd *Command) WithName(n string) *Command

WithName sets the executable name or path and returns the receiver.

func (*Command) WithStdError

func (cmd *Command) WithStdError(w io.Writer) *Command

WithStdError sets the writer that receives the process's stderr and returns the receiver. Pass nil to discard stderr.

func (*Command) WithStdInput

func (cmd *Command) WithStdInput(r io.Reader) *Command

WithStdInput sets the reader that is connected to the process's stdin and returns the receiver. Pass nil to use no stdin.

func (*Command) WithStdOutput

func (cmd *Command) WithStdOutput(w io.Writer) *Command

WithStdOutput sets the writer that receives the process's stdout and returns the receiver. Pass nil to discard stdout.

func (*Command) Worker

func (cmd *Command) Worker() fnx.Worker

Worker provides access to the command as an fnx.Worker function.

type Error

type Error struct {
	StdError  *bytes.Buffer
	StdOutput *bytes.Buffer
	Name      string
	Err       error
}

Error holds the diagnostic information from a failed Exec call.

func ResolveError

func ResolveError(err error) (*Error, bool)

ResolveError extracts an *Error from err if one appears anywhere in the error chain. Returns the *Error and true on success, or nil and false if err is nil or contains no *Error.

func (*Error) As

func (e *Error) As(other any) bool

As finds the first value in the underlying error's chain that matches target and sets target to that value, delegating to errors.As. This lets errors.As(extrErr, &target) extract typed errors from inside Err.

func (*Error) Error

func (e *Error) Error() string

Error returns the string summary of the error including the name, the underlying error, and the captured stderr output.

func (*Error) Is

func (e *Error) Is(other error) bool

Is reports whether the underlying error matches other, delegating to errors.Is. This lets errors.Is(extrErr, target) traverse the wrapped chain inside Err without callers needing to unwrap manually.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error so that errors.Is and errors.As can traverse it. Returns nil when no underlying error is set.

Jump to

Keyboard shortcuts

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