execx

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 14 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 execx runs external tools such as terragrunt, terraform, tofu and mise. It handles timeouts, one interrupt and then a kill on cancellation, an environment allowlist, and debug logs with secrets redacted.

Index

Constants

View Source
const DefaultTimeout = 30 * time.Minute

DefaultTimeout is the per-command timeout when neither WithTimeout nor Cmd.Timeout sets one.

View Source
const DefaultWaitDelay = 10 * time.Second

DefaultWaitDelay is how long a child gets between the interrupt and the kill.

View Source
const Redacted = "***"

Redacted replaces secret values in logged command lines.

Variables

View Source
var ErrNotFound = errors.New("executable not found")

ErrNotFound is returned when the executable isn't on the child's PATH or isn't executable.

View Source
var ErrTimeout = errors.New("command timed out")

ErrTimeout is returned when a command runs longer than its timeout.

Functions

func DefaultEnvAllow

func DefaultEnvAllow() []string

DefaultEnvAllow lists the environment variables passed to child processes. Entries ending in "*" are prefixes. Names match case-insensitively on Windows, where PATH is often spelled Path.

func Redact

func Redact(args []string) []string

Redact returns a copy of args that is safe to log: values of secret-looking flags (--token x, --token=x) and KEY=VALUE pairs with a secret-looking key (-var 'db_password=x') are replaced.

Types

type Cmd

type Cmd struct {
	Name    string // looked up on the child's PATH unless it contains a path separator
	Args    []string
	Dir     string
	Env     []string // extra KEY=VALUE pairs, passed regardless of the allowlist; only keys are logged
	Stdin   io.Reader
	Stdout  io.Writer     // nil captures into Result.Stdout
	Stderr  io.Writer     // also receives stderr, which is captured either way
	Timeout time.Duration // overrides the Runner default; negative disables it
}

Cmd describes one invocation.

type ExitError

type ExitError struct {
	Name   string
	Code   int
	Stderr string
}

ExitError reports a non-zero exit. Stderr holds the trailing captured stderr.

func (*ExitError) Error

func (e *ExitError) Error() string

type Option

type Option func(*Runner)

Option configures a Runner.

func WithEnvAllow

func WithEnvAllow(allow []string) Option

WithEnvAllow replaces the environment allowlist.

func WithEnviron

func WithEnviron(f func() []string) Option

WithEnviron sets the parent environment the allowlist is applied to (default os.Environ).

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the default per-command timeout.

func WithWaitDelay

func WithWaitDelay(d time.Duration) Option

WithWaitDelay sets how long a child gets between the interrupt and the kill.

type Result

type Result struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
	Duration time.Duration
}

Result holds a finished command's output.

type Runner

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

Runner starts commands. The zero value is not usable; use NewRunner.

func NewRunner

func NewRunner(logger *slog.Logger, opts ...Option) *Runner

NewRunner returns a Runner that logs through logger.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, c Cmd) (*Result, error)

Run starts the command and waits for it. On cancellation or timeout the child gets one interrupt, so terraform can release its state lock, and is killed after the wait delay. A non-zero exit is returned as *ExitError together with the Result.

Jump to

Keyboard shortcuts

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