runner

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 15 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 runner is the only code that starts a process.

Index

Constants

View Source
const Grace = 60 * time.Second

Grace is how long a streamed run has to end after the interrupt its ended context sends, before its process group is killed: long enough for OpenTofu to finish the call it's in and write its state.

Variables

View Source
var ErrFollowFailed = errors.New("a run whose output couldn't be followed")

ErrFollowFailed is returned, wrapped, for a run streamed through a file whose output couldn't be followed as it was written, or handed over: what its caller read of it may be short. It sits beside the run's own error, never inside an *ExitError's.

View Source
var ErrIncompleteDetach = errors.New("a detached process needs a folder to run in and a file for its stderr")

ErrIncompleteDetach is returned, wrapped, for a Detached with no folder to run in or no file for its stderr.

View Source
var ErrNoSuchProcess = errors.New("no process a run's can have")

ErrNoSuchProcess is returned, wrapped, for a pid no process of a run's can have: none above 1, which is launchd's, and where 0 and below would signal a group, or every process.

View Source
var ErrNotFound = errors.New("not found on the PATH")

ErrNotFound is returned, wrapped, when the binary isn't on the PATH.

View Source
var ErrNotStarted = errors.New("not started")

ErrNotStarted is returned, wrapped, for a run that never started: its context already ended, its binary not found, not one it may run, a folder it can't run in, or arguments past the system's limit. Nothing ran.

View Source
var ErrUnfollowable = errors.New("a file a run can't stream through")

ErrUnfollowable is returned, wrapped, for a file a run can't stream through (Followable), and beside ErrNotStarted for a run given one, or one it can't watch: nothing ran.

Functions

func BesideExit

func BesideExit(err error) []string

BesideExit are the errors joined in err, a line apiece, each in its own words, but a run's *ExitError where it exited by itself, which what the child said of its failure explains. A signal's end is kept, in its own words, since nothing the child said explains it.

func Followable

func Followable(f *os.File) error

Followable is nil where a run can stream through f: a regular file, open to write and to append, that its name still opens. Else it's why not, an ErrUnfollowable.

func Interrupt

func Interrupt(pid int) error

Interrupt sends the process pid one interrupt, as a cancel of a run whose own process has gone sends its Terragrunt (decision 52): the process alone, never its group, so OpenTofu hears it only as Terragrunt passes it on. A process already gone is no error: nothing's left to interrupt.

Types

type Command

type Command struct {
	// Dir is where it runs; "" for terrace's own directory. Where it's set,
	// the run's PWD names it, as a shell starting there would: a tool may
	// read its folder from PWD rather than asking the system.
	Dir string
	// Env is the whole environment the run gets, NAME=value each, and
	// nothing else: nil is an empty one. internal/childenv builds it.
	Env  []string
	Name string
	Args []string
}

Command is one run of a binary.

func (Command) String

func (c Command) String() string

String is the command line, as a seeded fake reads it: the name and its arguments, space-separated.

type Detached

type Detached struct {
	// Command is what runs. Its Dir is needed, so the process never keeps
	// terrace's own folder, which may be a worktree removed while it runs.
	Command Command
	// Stderr is the file the process writes its errors to, so a crash
	// leaves its words. It's needed.
	Stderr *os.File
	// Files are handed to the process after stderr, as fds 3, 4 and on, in
	// order.
	Files []*os.File
}

Detached is a process to start detached.

type Ending

type Ending struct {
	// Signals are those sent to the run as its context ended, in order:
	// none, an interrupt, or an interrupt and then a kill.
	Signals []syscall.Signal
	// Left are those sent to what the process cmd started left of its group
	// once it had exited, as a local-exec's child, or OpenTofu where
	// Terragrunt died: none where it left nothing, else a terminate, then
	// a kill where anything's still there after its grace.
	Left []syscall.Signal
}

Ending is how a streamed run ended.

func (Ending) Killed

func (e Ending) Killed() bool

Killed reports whether the run was killed once its grace was over, with no chance to say how it ended.

func (Ending) Signalled

func (e Ending) Signalled() bool

Signalled reports whether the run was sent a signal: a run that was is never a success, whatever its status, since a tool may exit 0 after one.

type Exec

type Exec struct{}

Exec runs real processes. Each gets exactly its command's environment, PWD aside, and never the terminal: stdin is empty, both output streams are captured, and it runs in a session of its own, with no controlling terminal, so a child that opens /dev/tty to prompt fails at once rather than stopping until it's killed. The binary is found on terrace's own PATH.

func (Exec) Detach

func (x Exec) Detach(ctx context.Context, d Detached) (Started, error)

Detach starts d's process to outlive terrace, as a run's own process does (spec 4.1.5, 4.8.7), and returns once it has started, never waiting for it to end. It runs in a session of its own, so no hangup or signal sent to terrace's terminal or group reaches it, with stdin and stdout on the null device, stderr on d.Stderr, exactly its command's environment, PWD aside, and no fd of terrace's but d.Files: the caller closes its own copies of those once Detach returns. Should it end while terrace still runs, it's reaped, so it leaves no zombie. A start that fails is ErrNotStarted, and nothing ran.

func (Exec) Run

func (x Exec) Run(ctx context.Context, cmd Command) (Result, error)

Run runs cmd and returns what it printed. A run that exits non-zero is an *ExitError, with what it printed kept in the Result. It runs as Stream runs it, in a process group of its own, but with interruptGrace for Grace.

func (Exec) Stream

func (x Exec) Stream(ctx context.Context, cmd Command, out Output) (Ending, error)

Stream runs cmd in a session and process group of its own, writing its output to out as it comes, through pipes, and returns once the run has ended and its output is written. When ctx ends, the process cmd started gets an interrupt, which it passes on to its own children, as Terragrunt does, and its whole group a kill if it hasn't ended within Grace; the run's error then holds ctx's. Once that process has exited, what it left of its group is ended: killed on a cancelled run, else terminated, then killed if anything's still there after Grace. A run that exits non-zero is an *ExitError, and one that never started ErrNotStarted.

A pipe dies with its reader: should terrace die first, the child dies at its next write, and so does a child of its own writing to it. A run that must outlive terrace streams through a file (StreamFile).

func (Exec) StreamFile

func (x Exec) StreamFile(ctx context.Context, cmd Command, out FileOutput) (Ending, error)

StreamFile runs cmd as Stream does, but for its output, which the child writes to out.File, and which is read as the file grows, from where it ends as StreamFile is called until what's left of the run's group has ended, then to the file's end. A File it can't stream through (Followable), or can't watch, is ErrUnfollowable and ErrNotStarted: nothing ran. Output it couldn't follow or hand over is ErrFollowFailed, beside the run's own error.

type ExitError

type ExitError struct {
	// Code is the run's exit status: -1 where a signal ended it.
	Code int
	Err  error
}

ExitError is a run that ended with a status other than 0, or that a signal ended.

func (*ExitError) Error

func (e *ExitError) Error() string

Error is the exit status, as os/exec words it.

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap gives the run's own error, where there is one.

type FileOutput

type FileOutput struct {
	// File is a regular file, open to write and to append, the child
	// writes both its streams to, in the order it writes them. A file,
	// unlike a pipe, never loses its reader, so the child runs on whatever
	// happens to terrace. The run reads it by its name, which must still
	// name it, from where it ends when StreamFile is called, until the
	// run's group has ended, then to its end: a process that leaves the
	// group yet keeps the file, as a hook's daemon in a session of its own,
	// writes outside what's read once the group has ended. The caller
	// closes it once the run has ended.
	File *os.File
	// Out gets what the child writes to File, in the order written, as it's
	// written, though up to a few milliseconds behind where a pipe would
	// hand it over; nil drops it. It's written from a goroutine of the
	// run's own, which nothing else writes it on, alongside OnStart. A
	// writer that blocks holds up the run's end, but never the child. Its
	// first error stops it being written, and is the run's, an
	// ErrFollowFailed.
	Out io.Writer
	// OnStart and OnInterrupt are as Output's.
	OnStart     func(Started)
	OnInterrupt func()
}

FileOutput is where a streamed run's output goes through a file: the child writes both its streams to File, which the run reads as it grows, handing what it reads to Out.

type Output

type Output struct {
	Stdout, Stderr io.Writer
	// OnStart, where it's set, gets the run's process ids as soon as it
	// has started: what ends the run's group should terrace die before it
	// does. It's called on the goroutine Stream runs on, alongside the
	// writes of the run's output, and must not block: the run is already
	// going, and its interrupt doesn't wait on it.
	OnStart func(Started)
	// OnInterrupt, where it's set, is called as ctx's end has the run
	// interrupted, just before its interrupt is sent: what records that
	// it's sent, so that nothing sends it a second (decision 52). It's
	// called on a goroutine of the run's own, and the interrupt waits on
	// it.
	OnInterrupt func()
}

Output is where a streamed run's output goes as the child writes it. One writer given as both takes both streams through one pipe, in the order the child wrote them. Each writer is written from os/exec's own goroutine, which nothing else reads the child's output on: a writer that blocks holds the child up.

type Result

type Result struct {
	Stdout, Stderr []byte
}

Result is what a run printed.

type Started

type Started struct {
	Pid, Group int
}

Started is a started run's process ids: its leader's, and the group's it leads.

Directories

Path Synopsis
Package runnertest is a fake runner.Runner, seeded by command line, for the tests of each binary's adapter.
Package runnertest is a fake runner.Runner, seeded by command line, for the tests of each binary's adapter.

Jump to

Keyboard shortcuts

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