Documentation
¶
Overview ¶
Package env provides utilities for working with environment variables. It includes functions for converting, merging, updating, and managing environment variable slices used throughout atmos.
Package env provides unified environment variable formatting across multiple output formats. It supports bash, dotenv, env, and github formats with consistent escaping and heredoc handling.
Package env provides utilities for working with environment variables.
Index ¶
- Variables
- func ApplyGlobalEnvToSlice(envSlice []string, globalEnv map[string]string) []string
- func CommandEnvToMap(envs []schema.CommandEnv) map[string]string
- func ConvertEnvVars(envVarsMap map[string]any) []string
- func ConvertMapStringToAny(env map[string]string) map[string]any
- func ConvertMapToSlice(envMap map[string]string) []string
- func EnsureBinaryInPath(envSlice []string, binaryPath string) []string
- func EnvironToMap() map[string]string
- func EnvironToMapFiltered(excludeKeys []string, excludePrefixes []string) map[string]string
- func FormatData(data map[string]any, format Format, opts ...Option) (string, error)
- func FormatValue(key string, value any, format Format, opts ...Option) (string, error)
- func GetPathFromEnvironment(envSlice []string) string
- func MergeGlobalEnv(baseEnv []string, globalEnv map[string]string) []string
- func MergeSystemEnv(componentEnvList []string) []string
- func MergeSystemEnvSimple(newEnvList []string) []string
- func MergeSystemEnvSimpleWithGlobal(newEnvList []string, globalEnv map[string]string) []string
- func MergeSystemEnvWithGlobal(componentEnvList []string, globalEnv map[string]string) []string
- func PrependToPath(currentPath, newDir string) string
- func UpdateEnvVar(envSlice []string, key, value string) []string
- func UpdateEnvironmentPath(envSlice []string, newDir string) []string
- func ValueToString(value any) string
- func WriteToFile(path string, content string) error
- type Builder
- type Format
- type Option
Constants ¶
This section is empty.
Variables ¶
var SupportedFormats = []Format{FormatEnv, FormatDotenv, FormatBash, FormatGitHub}
SupportedFormats lists all supported environment variable output formats.
Functions ¶
func ApplyGlobalEnvToSlice ¶
ApplyGlobalEnvToSlice applies global env vars to an environment slice. For each key in globalEnv, if it doesn't exist in envSlice, it is added. If it exists, it is NOT overwritten (preserves higher-priority values). This is useful when global env needs to be applied as defaults.
func CommandEnvToMap ¶
func CommandEnvToMap(envs []schema.CommandEnv) map[string]string
CommandEnvToMap converts a slice of schema.CommandEnv to a map[string]string. Keys are taken from the Key field; later entries overwrite earlier ones on duplicate keys.
func ConvertEnvVars ¶
ConvertEnvVars converts ENV vars from a map to a list of strings in the format ["key1=val1", "key2=val2", "key3=val3" ...]. Variables with nil or "null" values are skipped.
func ConvertMapStringToAny ¶
ConvertMapStringToAny converts a map[string]string to map[string]any. Returns nil if the input map is nil.
func ConvertMapToSlice ¶
ConvertMapToSlice converts a map[string]string to []string{"KEY=value", ...}. The order of the resulting slice is not guaranteed due to map iteration.
func EnsureBinaryInPath ¶
EnsureBinaryInPath checks if a binary directory is in PATH and adds it if missing. Returns updated environment with the binary directory prepended to PATH.
func EnvironToMap ¶
EnvironToMap converts all the environment variables in the environment into a map of strings.
func EnvironToMapFiltered ¶
EnvironToMapFiltered converts environment variables to a map, excluding specified keys and prefixes. This is useful for terraform-exec which prohibits certain environment variables.
func FormatData ¶ added in v1.205.0
FormatData formats key-value data in the specified format. Complex values (maps, slices) are JSON-encoded. Keys are sorted alphabetically for consistent output.
func FormatValue ¶ added in v1.205.0
FormatValue formats a single key-value pair in the specified format.
func GetPathFromEnvironment ¶
GetPathFromEnvironment extracts the PATH value from an environment slice.
func MergeGlobalEnv ¶
MergeGlobalEnv merges atmos.yaml global env into an environment slice. Global env has lowest priority and is appended (can be overridden by command-specific env). The baseEnv slice typically comes from os.Environ().
func MergeSystemEnv ¶
MergeSystemEnv merges system environment variables with component-specific env. Priority order: system env < component env. Special handling is applied for TF_CLI_ARGS_* variables where new values are prepended to existing values.
func MergeSystemEnvSimple ¶
MergeSystemEnvSimple merges system environment variables with new env without TF_CLI_ARGS_* special handling. Priority order: system env < new env list.
func MergeSystemEnvSimpleWithGlobal ¶
MergeSystemEnvSimpleWithGlobal merges system environment variables with global env from atmos.yaml and new env without TF_CLI_ARGS_* special handling. Priority order: system env < global env < new env list.
func MergeSystemEnvWithGlobal ¶
MergeSystemEnvWithGlobal merges system environment variables with global env from atmos.yaml and component-specific env. Priority order: system env < global env < component env. Special handling is applied for TF_CLI_ARGS_* variables where new values are prepended to existing values.
func PrependToPath ¶
PrependToPath adds a directory to the beginning of the PATH environment variable. Returns the new PATH value.
func UpdateEnvVar ¶
UpdateEnvVar updates or adds an environment variable in an environment slice. Returns a new environment slice with the variable updated.
func UpdateEnvironmentPath ¶
UpdateEnvironmentPath updates the PATH in an environment slice. Returns a new environment slice with the updated PATH.
func ValueToString ¶ added in v1.205.0
ValueToString converts any value to its string representation. Complex types (maps, slices) are JSON-encoded. Boolean values are converted to "true" or "false". Numeric values are converted using their default string representation. Nil values return an empty string.
func WriteToFile ¶ added in v1.205.0
WriteToFile writes content to a file in append mode. Creates the file if it doesn't exist.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent API for constructing environment variable slices.
func NewBuilder ¶
NewBuilder creates a new Builder with an optional initial environment slice. If no initial slice is provided, starts with an empty slice.
func (*Builder) WithEnvMap ¶
WithEnvMap adds all environment variables from a map.
func (*Builder) WithEnvVar ¶
WithEnvVar adds an environment variable with the given key and value.
type Format ¶ added in v1.205.0
type Format string
Format represents an environment variable output format.
const ( // FormatEnv outputs key=value pairs without quoting. FormatEnv Format = "env" // FormatDotenv outputs key='value' pairs with single-quote escaping. FormatDotenv Format = "dotenv" // FormatBash outputs export key='value' statements with single-quote escaping. FormatBash Format = "bash" // FormatGitHub outputs key=value or heredoc syntax for multiline values. // Used for $GITHUB_OUTPUT and $GITHUB_ENV in GitHub Actions. FormatGitHub Format = "github" )
func ParseFormat ¶ added in v1.205.0
ParseFormat converts a format string to a Format type. Returns an error for unsupported format strings.
type Option ¶ added in v1.205.0
type Option func(*config)
Option configures formatting behavior.
func WithExport ¶ added in v1.205.0
WithExport controls whether bash format includes the 'export' prefix. Default is true (export KEY='value'). Set to false for KEY='value' without export. This option only affects FormatBash; other formats ignore it.
func WithFlatten ¶ added in v1.205.0
WithFlatten flattens nested maps using the specified separator. For example, {"a": {"b": "c"}} with separator "_" becomes {"a_b": "c"}.
func WithUppercase ¶ added in v1.205.0
func WithUppercase() Option
WithUppercase converts all keys to uppercase.