reexec

package
v1.216.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package reexec provides shared helpers for re-executing Atmos (either self-replacing the current process or spawning a child) with consistent handling of the --chdir flag and ATMOS_CHDIR environment variable.

Re-exec call sites (version switching, profile fallback, CLI aliases) all need to strip --chdir from argv and ATMOS_CHDIR from env before handing control to the new process — otherwise a relative chdir applied by the parent would be re-applied by the child relative to the already-changed cwd, producing an incorrect or non-existent path.

Index

Constants

View Source
const AtmosChdirEnvVar = "ATMOS_CHDIR"

AtmosChdirEnvVar is the environment-variable form of the --chdir flag.

View Source
const DepthEnvVar = "ATMOS_REEXEC_DEPTH"

DepthEnvVar holds the re-exec depth counter. A value of 0 (or unset) means the process was not re-exec'd by another Atmos process. A value of 1 or higher means the current process is running inside a re-exec'd child; any code path that would normally trigger another re-exec must short-circuit instead of looping.

Variables

This section is empty.

Functions

func CurrentDepth

func CurrentDepth() int

CurrentDepth reads the current depth from the process environment. It is shorthand for Depth(os.Getenv(DepthEnvVar)) and is the preferred entry point for callers that do not use dependency-injected env lookup.

func Depth

func Depth(value string) int

Depth parses a DepthEnvVar value. Empty or non-numeric input returns 0 so callers can treat "missing" and "invalid" identically (both mean "not inside a re-exec").

func FilterChdirEnv

func FilterChdirEnv(environ []string) []string

FilterChdirEnv returns a copy of environ with ATMOS_CHDIR removed. When the original contained an ATMOS_CHDIR entry, an explicit empty override (`ATMOS_CHDIR=`) is appended so the child cannot inherit the parent's value via environment merging.

func NextEnv

func NextEnv(environ []string) []string

NextEnv returns a copy of environ with ATMOS_REEXEC_DEPTH incremented by one. If the input contains no depth entry, the returned slice has one appended set to "1". If it contains multiple, the last wins and the returned slice contains exactly one. Use this when building the env slice for a child process about to be re-exec'd.

func StripChdirArgs

func StripChdirArgs(args []string) []string

StripChdirArgs returns a copy of args with --chdir / -C flags and their values removed. It handles all five accepted forms: `--chdir VAL`, `--chdir=VAL`, `-C VAL`, `-C=VAL`, and `-CVAL` (concatenated).

The POSIX `--` end-of-flags separator is respected: once encountered, the separator and every token that follows are forwarded verbatim so positional arguments intended for downstream tools (terraform, helmfile, packer, custom commands) are never mistaken for atmos flags.

Use this before re-executing Atmos so a chdir that was already applied by the parent is not re-applied by the child against the already-changed working directory.

Types

type ExecFunc

type ExecFunc func(argv0 string, argv []string, envv []string) error

ExecFunc matches the signature of syscall.Exec and is used to replace the current process image on Unix or spawn-and-exit on Windows. It is declared as a type so callers can accept a function value for dependency injection in tests.

var Exec ExecFunc = syscall.Exec

Exec replaces the current process image with the given program, argv, and envv (Unix execve semantics). Declared as a variable so tests can override it to avoid actually replacing the test process.

Jump to

Keyboard shortcuts

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