exec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 10 Imported by: 1 module, 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 runs subprocesses. Every subprocess git-stack starts goes through a Runner so that output capture, TTY hand-off, cancellation and debug logging are handled in exactly one place.

Capture mode is the default: stdout and stderr are buffered and stdin is whatever the caller supplies (never the parent's stdin). Passthrough mode attaches the real terminal and is only available to runners built with WithTTY; the MCP server never constructs such a runner.

Index

Constants

This section is empty.

Variables

View Source
var ErrTTYUnavailable = errors.New("exec: passthrough mode requested but no terminal is attached")

ErrTTYUnavailable is returned when Passthrough mode is requested from a runner that has no terminal attached.

Functions

func BuildEnv

func BuildEnv(base, extra, unset []string) []string

BuildEnv returns base with the keys in unset removed and extra appended. Later entries win when git or the OS reads the environment, so extra effectively overrides base.

Types

type Cmd

type Cmd struct {
	Name string
	Args []string
	// Dir is the working directory; empty means the current directory.
	Dir string
	// Env holds KEY=VALUE pairs appended to the parent environment.
	Env []string
	// Unset lists environment keys removed before Env is applied.
	Unset []string
	// Stdin is fed to the process in Capture mode. Nil means no input.
	Stdin io.Reader
	// Stream, when set in Capture mode, also receives the process's stderr as
	// it is produced so progress from a long backend command can be relayed
	// live. Stderr is still captured in the Result. Ignored in Passthrough.
	Stream io.Writer
	Mode   Mode
}

Cmd describes a subprocess to run.

func (Cmd) String

func (c Cmd) String() string

String renders the command line for logs and error messages.

type ExitError

type ExitError struct {
	Cmd    Cmd
	Result Result
}

ExitError reports a non-zero exit status.

func (*ExitError) Error

func (e *ExitError) Error() string

type Mode

type Mode int

Mode selects how a subprocess's standard streams are wired.

const (
	// Capture buffers stdout and stderr (default).
	Capture Mode = iota
	// Passthrough attaches the runner's TTY to stdin, stdout and stderr.
	Passthrough
)

func (Mode) String

func (m Mode) String() string

type NotFoundError

type NotFoundError struct {
	Name string
}

NotFoundError reports that the executable is not on PATH.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type Option

type Option func(*runner)

Option configures a Runner.

func WithDebug

func WithDebug(w io.Writer) Option

WithDebug logs every command (name, arguments, directory, mode, exit code and duration) to w. Stdin contents are never logged.

func WithTTY

func WithTTY(t TTY) Option

WithTTY enables Passthrough mode using the given terminal files.

type Result

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

Result is the outcome of a finished subprocess.

func (Result) Err

func (r Result) Err() string

Err returns stderr with surrounding whitespace trimmed.

func (Result) Out

func (r Result) Out() string

Out returns stdout with surrounding whitespace trimmed.

type Runner

type Runner interface {
	// Run executes c and waits for it. A non-zero exit status is returned as
	// *ExitError (the Result is still populated). A missing executable is
	// returned as *NotFoundError. Context cancellation is returned as the
	// context's error.
	Run(ctx context.Context, c Cmd) (Result, error)
}

Runner runs subprocesses.

func New

func New(opts ...Option) Runner

New returns a Runner backed by os/exec.

type TTY

type TTY struct {
	In, Out, Err *os.File
}

TTY holds the terminal files used for Passthrough mode.

Directories

Path Synopsis
Package exectest provides a scripted exec.Runner for unit tests.
Package exectest provides a scripted exec.Runner for unit tests.

Jump to

Keyboard shortcuts

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