exec

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package exec runs external commands. It wraps exec.Cmd package with support for allocating a pseudo-terminal.

Index

Constants

View Source
const DefaultGracefulShutdownDelay = 30 * time.Second

DefaultGracefulShutdownDelay is the default time to wait for a process to exit gracefully after sending an interrupt signal before escalating to SIGKILL.

Variables

View Source
var ErrPTYRequiresOSBackend = errors.New("PTY allocation requires an OS-backed vexec.Exec")

ErrPTYRequiresOSBackend is returned when a Cmd is started with PTY allocation requested but the underlying vexec.Exec is not OS-backed.

Functions

func PrepareConsole

func PrepareConsole(_ log.Logger) bool

PrepareConsole is run at the start of the application to set up the console. On Unix, terminals handle ANSI escape sequences natively, so this is a no-op. Returns true to indicate ANSI support is available.

func PrepareStdinForPrompt added in v1.0.0

func PrepareStdinForPrompt(_ log.Logger)

PrepareStdinForPrompt is a no-op on Unix.

Types

type Cmd

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

Cmd wraps a vexec.Cmd with signal forwarding and optional PTY support. The Cmd may be backed by a real OS process or by an in-memory vexec backend (used in tests and fuzzers to prevent fork of external binaries).

func Command

func Command(ctx context.Context, e vexec.Exec, name string, args ...string) *Cmd

Command returns a `Cmd` configured to execute the named program with the given arguments via the provided vexec.Exec. PTY allocation requires an OS-backed Exec; non-OS backends are accepted but `WithUsePTY(true)` will fail at Start with ErrPTYRequiresOSBackend.

func (*Cmd) Configure

func (cmd *Cmd) Configure(opts ...Option)

Configure sets options to the `Cmd`.

func (*Cmd) Dir added in v1.0.4

func (cmd *Cmd) Dir() string

Dir returns the working directory previously set via SetDir.

func (*Cmd) ForwardSignal

func (cmd *Cmd) ForwardSignal(ctx context.Context, l log.Logger, sig os.Signal)

ForwardSignal forwards a given `sig` with a delay if cmd.forwardSignalDelay is greater than 0, and if the same signal is received again, it is forwarded immediately.

func (*Cmd) RegisterGracefullyShutdown

func (cmd *Cmd) RegisterGracefullyShutdown(ctx context.Context, l log.Logger) func()

RegisterGracefullyShutdown registers a graceful shutdown for the command in two ways:

  1. If the context cancel contains a cause with a signal, this means that Terragrunt received the signal from the OS, since our executed command may also receive the same signal, we need to give the command time to gracefully shutting down, to avoid the command receiving this signal twice. Thus we will send the signal to the executed command with a delay or immediately if Terragrunt receives this same signal again.
  2. If the context does not contain any causes, this means that there was some failure and we need to terminate all executed commands, in this situation we are sure that commands did not receive any signal, so we send them an interrupt signal immediately.

func (*Cmd) Run added in v1.0.4

func (cmd *Cmd) Run(l log.Logger) error

Run starts the command and waits for it to complete.

func (*Cmd) SendSignal

func (cmd *Cmd) SendSignal(l log.Logger, sig os.Signal)

SendSignal sends the given `sig` to the executed command. Errors are logged rather than returned; ErrProcessNotStarted is silently ignored because callers may race against process startup.

func (*Cmd) SetDir added in v1.0.4

func (cmd *Cmd) SetDir(dir string)

SetDir sets the command's working directory.

func (*Cmd) SetEnv added in v1.0.4

func (cmd *Cmd) SetEnv(env []string)

SetEnv sets the command's environment in `KEY=value` form.

func (*Cmd) SetStderr added in v1.0.4

func (cmd *Cmd) SetStderr(w io.Writer)

SetStderr sets the command's standard error.

func (*Cmd) SetStdin added in v1.0.4

func (cmd *Cmd) SetStdin(r io.Reader)

SetStdin sets the command's standard input.

func (*Cmd) SetStdout added in v1.0.4

func (cmd *Cmd) SetStdout(w io.Writer)

SetStdout sets the command's standard output.

func (*Cmd) Start

func (cmd *Cmd) Start(l log.Logger) error

Start starts the command but does not wait for it to complete. When PTY allocation is requested, the underlying backend must be OS-backed.

func (*Cmd) Wait added in v1.0.4

func (cmd *Cmd) Wait() error

Wait waits for the command to exit and returns its error.

type ConsoleState added in v1.0.0

type ConsoleState struct{}

ConsoleState is a no-op on Unix. On Windows it saves/restores console modes that subprocesses may modify.

func SaveConsoleState added in v1.0.0

func SaveConsoleState() ConsoleState

SaveConsoleState is a no-op on Unix.

func (ConsoleState) Restore added in v1.0.0

func (ConsoleState) Restore()

Restore is a no-op on Unix.

type Option

type Option func(*Cmd)

Option is type for passing options to the Cmd.

func WithEnv

func WithEnv(env map[string]string) Option

WithEnv sets envs to the Cmd.

func WithForwardSignalDelay

func WithForwardSignalDelay(delay time.Duration) Option

WithForwardSignalDelay sets forwarding signal delay to the Cmd.

func WithGracefulShutdownDelay added in v0.99.2

func WithGracefulShutdownDelay(delay time.Duration) Option

WithGracefulShutdownDelay sets the time to wait for a process to exit gracefully after sending an interrupt signal before escalating to SIGKILL. This allows processes like Terraform to clean up child processes (e.g., provider plugins).

func WithUsePTY

func WithUsePTY(state bool) Option

WithUsePTY enables a pty for the Cmd.

Jump to

Keyboard shortcuts

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