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
- func Args() []string
- func CurrentDepth() int
- func Depth(value string) int
- func FilterChdirEnv(environ []string) []string
- func NextEnv(environ []string) []string
- func ScriptArgs() int
- func SetOriginalArgs(args []string, scriptArgs int) (restore func())
- func StripBeforeScript(args []string, scriptArgs int, strip func([]string) []string) []string
- func StripChdirArgs(args []string) []string
- type ExecFunc
Constants ¶
const AtmosChdirEnvVar = "ATMOS_CHDIR"
AtmosChdirEnvVar is the environment-variable form of the --chdir flag.
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 Args ¶
func Args() []string
Args returns the argv to forward to a re-executed Atmos: the original command line when SetOriginalArgs recorded one, otherwise os.Args.
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 ¶
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 ¶
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 ¶
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 ScriptArgs ¶
func ScriptArgs() int
ScriptArgs returns how many trailing arguments of Args belong to a standalone script (its path and its arguments), or 0 when the command line does not run a script.
func SetOriginalArgs ¶
SetOriginalArgs records the argv the user actually typed when the process has rewritten os.Args for its own parsing (a standalone script run isolates the script path and its arguments). The scriptArgs count says how many trailing arguments (the script path and its arguments) belong to the script, so re-exec helpers leave them untouched. Re-exec call sites read Args so the new process receives the whole command line. It returns a function that restores the previous value.
func StripBeforeScript ¶
StripBeforeScript applies strip to args except the last scriptArgs entries, so flags that belong to a standalone script (for example a script's own --chdir) are forwarded unchanged. A count of zero, or one that does not fit args, means there is no script and strip applies to all of args.
func StripChdirArgs ¶
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.