shell

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: 26 Imported by: 0

Documentation

Overview

Package shell provides functions to run shell commands and Terraform commands.

Index

Constants

View Source
const SignalForwardingDelay = time.Second * 15

SignalForwardingDelay is the time to wait before forwarding the signal to the subcommand.

The signal can be sent to the main process (only `terragrunt`) as well as the process group (`terragrunt` and `terraform`), for example: kill -INT <pid> # sends SIGINT only to the main process kill -INT -<pid> # sends SIGINT to the process group Since we cannot know how the signal is sent, we should give `tofu`/`terraform` time to gracefully exit if it receives the signal directly from the shell, to avoid sending the second interrupt signal to `tofu`/`terraform`.

Variables

This section is empty.

Functions

func ExplainError

func ExplainError(err error) string

ExplainError will try to explain the error to the user, if we know how to do so.

func GitLastReleaseTag

func GitLastReleaseTag(ctx context.Context, l log.Logger, env map[string]string, workingDir string, gitRepo *url.URL) (string, error)

GitLastReleaseTag fetches git repository last release tag.

func GitRepoTags

func GitRepoTags(ctx context.Context, l log.Logger, env map[string]string, workingDir string, gitRepo *url.URL) ([]string, error)

GitRepoTags fetches git repository tags from passed url.

func GitTopLevelDir

func GitTopLevelDir(ctx context.Context, l log.Logger, env map[string]string, path string) (string, error)

GitTopLevelDir returns the git repository root that contains path, memoized in a run-scoped cache. A `.git` scan between path and any cached ancestor keeps the answer correct when a nested repository sits below an already-cached outer root. Concurrent misses for the same repo collapse to a single fork via the cache's resolve lock and a re-check after acquiring it.

func LastReleaseTag

func LastReleaseTag(tags []string) string

LastReleaseTag returns last release tag from passed tags slice.

func PromptUserForInput

func PromptUserForInput(ctx context.Context, l log.Logger, prompt string, nonInteractive bool, errWriter io.Writer) (string, error)

PromptUserForInput prompts the user for text in the CLI. Returns the text entered by the user.

func PromptUserForYesNo

func PromptUserForYesNo(ctx context.Context, l log.Logger, prompt string, nonInteractive bool, errWriter io.Writer) (bool, error)

PromptUserForYesNo prompts the user for a yes/no response and return true if they entered yes.

func RunCommand

func RunCommand(ctx context.Context, l log.Logger, e vexec.Exec, runOpts *ShellOptions, command string, args ...string) error

RunCommand runs the given shell command using the provided vexec.Exec. Pass vexec.NewMemExec from tests and fuzzers to intercept subprocess invocations so external binaries like tofu/terraform are never forked.

func RunCommandWithOutput

func RunCommandWithOutput(
	ctx context.Context,
	l log.Logger,
	e vexec.Exec,
	runOpts *ShellOptions,
	workingDir string,
	suppressStdout bool,
	needsPTY bool,
	command string,
	args ...string,
) (*util.CmdOutput, error)

RunCommandWithOutput runs the specified shell command using the provided vexec.Exec and captures stdout/stderr in addition to streaming.

Connect the command's stdin, stdout, and stderr to the currently running app. The command can be executed in a custom working directory by using the parameter `workingDir`. Terragrunt working directory will be assumed if empty string.

Types

type NestedGitScanDepthExceededError added in v1.0.4

type NestedGitScanDepthExceededError struct {
	Path     string
	Root     string
	MaxDepth int
}

NestedGitScanDepthExceededError is returned when the nested-repo guard's walk exceeds maxNestedGitScanDepth. Surfacing this as a typed error keeps callers from mistaking an aborted scan for a clean one.

func (*NestedGitScanDepthExceededError) Error added in v1.0.4

type RunCommandOptions added in v1.0.5

type RunCommandOptions struct {
	Output         *util.CmdOutput
	CommandDir     string
	Command        string
	Args           []string
	SuppressStdout bool
	NeedsPTY       bool
}

RunCommandOptions groups the per-invocation parameters for runCommand, keeping the function signature short and call sites readable.

type ShellOptions added in v1.0.0

type ShellOptions struct {
	Writers       writer.Writers
	EngineOptions *engine.EngineOptions
	EngineConfig  *engine.EngineConfig
	Telemetry     *telemetry.Options
	Env           map[string]string

	RootWorkingDir  string
	WorkingDir      string
	TFPath          string
	Experiments     experiment.Experiments
	Headless        bool
	ForwardTFStdout bool
}

ShellOptions contains the configuration needed to run shell commands.

func NewShellOptions added in v1.0.2

func NewShellOptions() *ShellOptions

NewShellOptions creates ShellOptions with sensible defaults:

  • Writers default to os.Stdout / os.Stderr.
  • Telemetry is always non-nil; TRACEPARENT is read from the environment when set.

Use the With* methods to override any of these.

func (*ShellOptions) NoEngine added in v1.0.0

func (o *ShellOptions) NoEngine() bool

NoEngine returns true if the user explicitly disabled the engine via --no-engine. Returns false when EngineOptions is nil (default: don't disable), letting the other guards (EngineConfig != nil, experiment enabled) decide whether to run.

func (*ShellOptions) SetTraceParent added in v1.0.2

func (o *ShellOptions) SetTraceParent(tp string) *ShellOptions

SetTraceParent explicitly overrides the TRACEPARENT value used for trace context propagation.

func (*ShellOptions) WithEngine added in v1.0.2

func (o *ShellOptions) WithEngine(cfg *engine.EngineConfig, opts *engine.EngineOptions) *ShellOptions

WithEngine sets the engine configuration and options.

func (*ShellOptions) WithEnv added in v1.0.2

func (o *ShellOptions) WithEnv(env map[string]string) *ShellOptions

WithEnv sets the environment variables for command execution.

func (*ShellOptions) WithExperiments added in v1.0.2

func (o *ShellOptions) WithExperiments(exp experiment.Experiments) *ShellOptions

WithExperiments sets the active experiments.

func (*ShellOptions) WithForwardTFStdout added in v1.0.2

func (o *ShellOptions) WithForwardTFStdout(f bool) *ShellOptions

WithForwardTFStdout sets the flag to forward TF stdout.

func (*ShellOptions) WithHeadless added in v1.0.2

func (o *ShellOptions) WithHeadless(h bool) *ShellOptions

WithHeadless sets the headless mode flag.

func (*ShellOptions) WithRootWorkingDir added in v1.0.2

func (o *ShellOptions) WithRootWorkingDir(dir string) *ShellOptions

WithRootWorkingDir sets the root working directory used in error messages.

func (*ShellOptions) WithTFPath added in v1.0.2

func (o *ShellOptions) WithTFPath(path string) *ShellOptions

WithTFPath sets the path to the Terraform/OpenTofu binary.

func (*ShellOptions) WithTelemetry added in v1.0.2

func (o *ShellOptions) WithTelemetry(t *telemetry.Options) *ShellOptions

WithTelemetry sets the full telemetry options, replacing the defaults from the constructor.

func (*ShellOptions) WithWorkingDir added in v1.0.2

func (o *ShellOptions) WithWorkingDir(dir string) *ShellOptions

WithWorkingDir sets the working directory for command execution.

func (*ShellOptions) WithWriters added in v1.0.2

func (o *ShellOptions) WithWriters(w writer.Writers) *ShellOptions

WithWriters sets the stdout/stderr writers.

Jump to

Keyboard shortcuts

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