grace

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKillFailed = errors.New("failed to kill process")

Functions

This section is empty.

Types

type Result added in v1.0.5

type Result struct {
	StdOut   bytes.Buffer
	StdErr   bytes.Buffer
	Buf      bytes.Buffer
	ExitCode int
	// contains filtered or unexported fields
}

func Spawn

func Spawn(ctx context.Context, cmd string, args []string, opts ...SpawnOption) (Result, error)

Spawn runs a command and captures its output. It creates a new process group, waits for the command to finish, and returns a Result. When no WithStdout or WithStderr option is provided, both streams are captured into a shared Result.Buf. If the context is cancelled, Spawn sends SIGTERM (followed by SIGKILL after KillDelay) to the entire process group.

Options can be provided to configure stdin, stdout, stderr redirection, environment variables, working directory, and kill timeout behaviour.

func (Result) Err added in v1.0.5

func (o Result) Err() error

Err returns the original *exec.ExitError if the command exited with a non-zero status, otherwise nil.

func (Result) Output added in v1.0.5

func (o Result) Output() *bytes.Buffer

Output returns a buffer containing the combined stdout and stderr output. When no WithStdout or WithStderr option was passed to Spawn, this returns the shared Result.Buf directly. Otherwise it concatenates StdOut and StdErr into a new buffer.

type SpawnOption added in v1.0.5

type SpawnOption interface {
	// contains filtered or unexported methods
}

SpawnOption configures the Spawn function.

func WithDir added in v1.0.5

func WithDir(dir string) SpawnOption

WithDir sets the working directory for the command.

func WithEnv added in v1.0.5

func WithEnv(env ...string) SpawnOption

WithEnv adds extra environment variables in KEY=VALUE format.

func WithKillDelay added in v1.0.5

func WithKillDelay(d time.Duration) SpawnOption

WithKillDelay sets the grace period between SIGTERM and SIGKILL when a command is cancelled. Defaults to 5 seconds.

func WithStderr added in v1.0.5

func WithStderr(w io.Writer) SpawnOption

WithStderr sets the standard error for the command. When set, Result.StdErr will not be populated and stdout is captured in Result.StdOut.

func WithStdin added in v1.0.5

func WithStdin(r io.Reader) SpawnOption

WithStdin sets the standard input for the command.

func WithStdout added in v1.0.5

func WithStdout(w io.Writer) SpawnOption

WithStdout sets the standard output for the command. When set, Result.StdOut will not be populated and stderr is captured in Result.StdErr.

Jump to

Keyboard shortcuts

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