run

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff added in v0.1.0

func Backoff(interval time.Duration) func() time.Duration

func Cancel

func Cancel()

Cancel cancels any currently executing scripts, which used the current context

func Command

func Command(cmd string, opts ...Option) (string, error)

Command runs a command, waits until completion, and returns stdout. The first argument is the path to the binary and DOES NOT shell-split. When not captured, stderr is output to os.Stderr and returned as part of the error text.

func Context added in v0.1.0

func Context() context.Context

Context returns the current context being used for executing scripts

func HandleSignals added in v0.1.0

func HandleSignals()

HandleSignals sets up a top-level signal handler that cancels the run context on the first interrupt and force-exits on the second.

func PeriodicStackTraces added in v0.1.0

func PeriodicStackTraces(interval func() time.Duration)

func SetContext added in v0.1.0

func SetContext(ctx context.Context)

SetContext sets the context to be used for executing scripts

Types

type Option

type Option func(context.Context, *exec.Cmd) error

Option is a functional option that modifies command execution behavior. Options are applied in order before the command runs, allowing customization of arguments, environment, I/O streams, and error handling.

func Args

func Args(args ...string) Option

Args appends args to the command

func Env

func Env(key, val string) Option

Env adds an environment variable to the command's environment. Note that the command inherits the current process environment by default (minus GO* and CGO_* variables which are filtered to avoid conflicts).

func InDir added in v0.1.0

func InDir(dir string) Option

InDir executes the command in the specified directory. This is equivalent to running "cd dir && cmd" but without spawning a shell. The directory change only affects this command execution.

func LDFlags added in v0.1.0

func LDFlags(flags ...string) Option

LDFlags adds Go linker flags via the -ldflags argument. If -ldflags is already present in the command arguments, the new flags are appended to the existing value rather than replacing it. This allows multiple LDFlags() calls to accumulate.

Example:

Run(`go build ./cmd/app`,
    run.LDFlags("-s", "-w"),
    run.LDFlags("-X main.version=1.0.0"),
)

func NoFail added in v0.1.0

func NoFail() Option

NoFail prevents the command from panicking on failure. Instead of panicking, the error is logged at Debug level and an empty string is returned. Use this when command failure is expected or acceptable.

Example:

version := Run(`git describe --tags`, run.NoFail())
if version == "" {
    version = "dev"
}

func Options added in v0.1.0

func Options(options ...Option) Option

Options combines multiple Options into a single Option. This is useful for creating reusable option sets or conditionally including groups of options.

Example:

buildOpts := run.Options(run.Env("CGO_ENABLED", "0"), run.Quiet())
Run(`go build ./...`, buildOpts)

func Quiet

func Quiet() Option

Quiet suppresses command output at Info level. The command line is logged at Debug level instead, and stderr is discarded unless config.Debug is enabled. Useful for commands whose output is only needed programmatically.

func Stderr

func Stderr(w io.Writer) Option

Stderr redirects the command's stderr to the provided writer. By default, stderr is sent to os.Stderr. Use io.Discard to suppress error output.

func Stdin

func Stdin(in io.Reader) Option

Stdin provides input to the command from the given reader. By default, stdin is not connected (nil). Use this for commands that read from standard input.

func Stdout

func Stdout(w io.Writer) Option

Stdout redirects the command's stdout to the provided writer. By default, stdout is captured and returned as the result string. Use this to stream output to a file, buffer, or os.Stdout for real-time display.

func Write

func Write(path string) Option

Write redirects stdout to a file, creating the file if it doesn't exist and truncating it if it does. The file is opened before returning the Option, so errors during file creation will panic immediately.

Jump to

Keyboard shortcuts

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