xc

package module
v0.0.0-...-698d0b4 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 9 Imported by: 0

README

Package xc is os/exec, but with fewer features (and vowels).

The goal is to make common (to me!) exec tasks simple, with richer errors available.

If it is useful for your tasks, great! If not, that's fine. Use os/exec.

Inspired by https://github.com/earthboundkid/requests and by having to write a lot of os/exec recently.

Status: definitely experimental. Use with caution.

License: MIT

Documentation

Overview

Package xc is os/exec, but with fewer features (and vowels).

The goal is to make common (to me!) exec tasks simple, with richer error handling.

If it is useful for your tasks, great! If not, that's fine. Use os/exec.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder provides a fluent interface for configuring commands.

func Command

func Command(ctx context.Context, name string, args ...string) *Builder

Command creates a new Builder for the given command and arguments.

func (*Builder) AppendArgf

func (b *Builder) AppendArgf(format string, args ...any) *Builder

AppendArgf adds a single argument fmt.Sprintf-style.

func (*Builder) AppendArgs

func (b *Builder) AppendArgs(args ...string) *Builder

AppendArgs adds additional arguments to the command.

func (*Builder) AppendEnv

func (b *Builder) AppendEnv(env ...string) *Builder

AppendEnv adds environment variables to the command's environment. Each entry should be in the form "KEY=VALUE".

func (*Builder) AppendEnvKV

func (b *Builder) AppendEnvKV(kv ...string) *Builder

AppendEnvKV adds environment variables to the command's environment. AppendEnvKV takes a slice of alternating key-value pairs. If the length of the slice is odd, AppendEnvKV will panic.

func (*Builder) CombineOutput

func (b *Builder) CombineOutput() *Builder

CombineOutput combines stderr into stdout in the output. Doing this makes stderr unavailable in error messages. Calling CombineOutput after Stdout or Stderr will panic.

func (*Builder) Describe

func (b *Builder) Describe(description string) *Builder

Describe adds a description of the command to the error message.

func (*Builder) Describef

func (b *Builder) Describef(format string, args ...any) *Builder

Describef adds a fmt.Sprintf-style description of the command to the error message.

func (*Builder) Dir

func (b *Builder) Dir(dir string) *Builder

Dir sets the working directory for the command.

func (*Builder) Run

func (b *Builder) Run() *Result

Run runs the command. It always returns a non-nil Result. It does not block. Instead, most Result methods block.

func (*Builder) Stderr

func (b *Builder) Stderr(w io.Writer) *Builder

Stderr writes error output to the given writer. Using a custom stderr will cause many Result methods to become unavailable. Calling Stderr after CombineOutput will panic.

func (*Builder) Stdin

func (b *Builder) Stdin(r io.Reader) *Builder

Stdin sets the input that will be provided to the command.

func (*Builder) StdinBytes

func (b *Builder) StdinBytes(buf []byte) *Builder

StdinBytes sets a byte slice as the input that will be provided to the command.

func (*Builder) StdinString

func (b *Builder) StdinString(s string) *Builder

StdinString sets a string as the input that will be provided to the command.

func (*Builder) Stdout

func (b *Builder) Stdout(w io.Writer) *Builder

Stdout writes output to the given writer. Using a custom stdout will cause many Result methods to become unavailable. Calling Stdout after CombineOutput will panic.

type Error

type Error struct {
	// Frame contains the runtime.Frame of the code that called Run.
	Frame runtime.Frame
	// Command contains a string description of the command that was run.
	// It is meant for human consumption, not parsing.
	Command string
	// LaunchErr contains errors that occurred before the command started.
	LaunchErr error
	// ExecErr contains errors that occurred while the command was running.
	ExecErr error
	// APIError contains errors that occurred due to API misuse.
	APIError error
	// Stderr contains the buffered error output of the command.
	// It may be nil, e.g. if Builder.Stderr or Builder.CombineOutput was called.
	Stderr *bytes.Buffer
	// Description contains a description of the command's intent.
	Description string
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Result

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

A Result contains the output of a command. All Result methods that return errors always return an *Error.

func (*Result) AllowExitCodes

func (r *Result) AllowExitCodes(codes ...int) *Result

AllowExitCodes allows the command to exit "successfully", i.e. with no error, with the given exit codes.

func (*Result) Bytes

func (r *Result) Bytes() ([]byte, error)

Bytes returns the (standard) output of the command.

func (*Result) ExitCode

func (r *Result) ExitCode() int

ExitCode returns the exit code of the command. If the command did not start, or the exit code is not available, for example because the command was terminated, it returns -1.

func (*Result) Split

func (r *Result) Split(sep string) ([]string, error)

Split splits the output of the command by the given separator.

func (*Result) SplitBytes

func (r *Result) SplitBytes(sep string) ([][]byte, error)

SplitBytes splits the output of the command by the given separator.

func (*Result) String

func (r *Result) String() (string, error)

String returns the (standard) output of the command.

func (*Result) TrimSpace

func (r *Result) TrimSpace() *Result

TrimSpace requests that the output be trimmed of leading and trailing whitespace.

func (*Result) Wait

func (r *Result) Wait() error

Wait waits for the command to finish.

Jump to

Keyboard shortcuts

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