run

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MPL-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package run provides facilities to run commands inside Terramate context and ordering.

Index

Constants

View Source
const (
	// ErrLoadingGlobals indicates that an error happened while loading globals.
	ErrLoadingGlobals errors.Kind = "loading globals to evaluate terramate.config.run.env configuration"

	// ErrEval indicates that an error happened while evaluating one of the
	// terramate.config.run.env attributes.
	ErrEval errors.Kind = "evaluating terramate.config.run.env attribute"

	// ErrInvalidEnvVarType indicates the env var attribute
	// has an invalid type.
	ErrInvalidEnvVarType errors.Kind = "invalid environment variable type"
)
View Source
const ErrNotFound errors.Kind = "executable file not found in $PATH"

ErrNotFound is the error resulting if a path search failed to find an executable file.

Variables

This section is empty.

Functions

func BuildDAG

func BuildDAG(
	d *dag.DAG[*config.Stack],
	root *config.Root,
	s *config.Stack,
	descendantsName string,
	getDescendants func(config.Stack) []string,
	ancestorsName string,
	getAncestors func(config.Stack) []string,
	visited dag.Visited,
) error

BuildDAG builds a run order DAG for the given stack.

func BuildDAGFromStacks added in v0.5.0

func BuildDAGFromStacks[S ~[]E, E any](root *config.Root, items S, getStack func(E) *config.Stack) (*dag.DAG[E], string, error)

BuildDAGFromStacks computes the final, reduced dag for the given list of stacks.

func Getenv added in v0.12.0

func Getenv(key string, environ []string) (val string, found bool)

Getenv returns the value of the environment variable named by the key, which is assumed to be case-insensitive, from the given environment. If the variable is not present in the environment, found is false.

func LookPath added in v0.4.2

func LookPath(file string, environ []string) (string, error)

LookPath searches for an executable named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. Otherwise, on success, the result is an absolute path.

In older versions of Go, LookPath could return a path relative to the current directory. As of Go 1.19, LookPath will instead return that path along with an error satisfying errors.Is(err, ErrDot). See the package documentation for more details.

func ResetVersionCache added in v0.14.7

func ResetVersionCache()

ResetVersionCache clears the shared version cache. Intended for tests.

func ResolveVersion added in v0.14.7

func ResolveVersion(ctx context.Context, environ []string, binary string) string

ResolveVersion returns the semantic version of the given binary by executing "<binary> --version", parsed with a generic semver regex. The shell-out is performed at most once per resolved binary path across the process.

func ResolveVersionFor added in v0.14.7

func ResolveVersionFor(ctx context.Context, environ []string, binary, resolvedPath string) string

ResolveVersionFor returns the semantic version for a given binary. If a test override is configured for the binary name it is returned immediately. If a non-empty resolvedPath is provided, it will be used to avoid any additional path lookups. Otherwise it falls back to ResolveVersion which may look up the binary in PATH.

func ResolveVersionForResolvedPath added in v0.14.7

func ResolveVersionForResolvedPath(ctx context.Context, resolvedPath string) string

ResolveVersionForResolvedPath returns the semantic version of the given binary at the provided resolved path by executing "<resolvedPath> --version". The shell-out is performed at most once per resolved binary path across the process.

func SeedVersionForResolvedPath added in v0.14.7

func SeedVersionForResolvedPath(resolvedPath, version string)

SeedVersionForResolvedPath sets the version for a given resolved binary path. Useful for tests to avoid shelling out.

func SetTestVersionOverride added in v0.14.7

func SetTestVersionOverride(binary, version string)

SetTestVersionOverride sets a version override by binary name for tests. When set, ResolveVersion will return this value without shelling out.

func Sort

func Sort[S ~[]E, E any](root *config.Root, items S, getStack func(E) *config.Stack) (string, error)

Sort computes the final execution order for the given list of stacks. In the case of multiple possible orders, it returns the lexicographic sorted path.

Types

type CLIRunner added in v0.14.7

type CLIRunner interface {
	// Name returns the CLI runner name (e.g. "terraform", "terragrunt", "tofu").
	Name() string
	// Version returns the cached semantic version string of the underlying CLI.
	// Must use the shared version cache to avoid repeated shell-outs.
	Version(ctx context.Context) string
	ShowCommand(ctx context.Context, planfile string, flags ...string) (*exec.Cmd, error)
}

CLIRunner abstracts common CLI operations used by terraform, terragrunt and tofu runners. Implementations should be thin wrappers around the actual binaries and must not mutate provided arguments. Methods that execute commands return a prepared *exec.Cmd ready to run.

type EnvVars

type EnvVars []string

EnvVars represents a set of environment variables to be used when running commands. Each string follows the same format used on os.Environ and can be used to set env on exec.Cmd.

func LoadEnv

func LoadEnv(root *config.Root, st *config.Stack) (EnvVars, error)

LoadEnv will load environment variables to be exported when running any command inside the given stack. The order of the env vars is guaranteed to be the same and is ordered lexicographically. All defined `terramate.config.run.env` definitions from the provided stack dir up to the root of the project are collected, and env definitions closer to the stack have precedence over parent definitions.

type OnceMap added in v0.11.6

type OnceMap[K ~string, V any] struct {
	// contains filtered or unexported fields
}

OnceMap contains map of key/value pairs that is safe for concurrent initialization.

func NewOnceMap added in v0.11.6

func NewOnceMap[K ~string, V any]() *OnceMap[K, V]

NewOnceMap returns a new empty OnceMap.

func (*OnceMap[K, V]) GetOrInit added in v0.11.6

func (m *OnceMap[K, V]) GetOrInit(k K, init func() (V, error)) (V, error)

GetOrInit obtains a value for the given key if the value exists in the map, or initializes it with the given init function. This function is safe to be called concurrently.

Directories

Path Synopsis
Package dag provides the Directed-Acyclic-Graph (DAG) primitives required by Terramate.
Package dag provides the Directed-Acyclic-Graph (DAG) primitives required by Terramate.
Package terraform provides utilities for executing terraform commands with configurable arguments.
Package terraform provides utilities for executing terraform commands with configurable arguments.
Package terragrunt provides utilities for executing terragrunt commands with configurable arguments.
Package terragrunt provides utilities for executing terragrunt commands with configurable arguments.
Package tofu provides utilities for executing OpenTofu commands with configurable arguments.
Package tofu provides utilities for executing OpenTofu commands with configurable arguments.

Jump to

Keyboard shortcuts

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