env

package
v1.205.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

SupportedFormats lists all supported environment variable output formats.

Functions

func ApplyGlobalEnvToSlice

func ApplyGlobalEnvToSlice(envSlice []string, globalEnv map[string]string) []string

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

func ConvertEnvVars(envVarsMap map[string]any) []string

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

func ConvertMapStringToAny(env map[string]string) map[string]any

ConvertMapStringToAny converts a map[string]string to map[string]any. Returns nil if the input map is nil.

func ConvertMapToSlice

func ConvertMapToSlice(envMap map[string]string) []string

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

func EnsureBinaryInPath(envSlice []string, binaryPath string) []string

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

func EnvironToMap() map[string]string

EnvironToMap converts all the environment variables in the environment into a map of strings.

func EnvironToMapFiltered

func EnvironToMapFiltered(excludeKeys []string, excludePrefixes []string) map[string]string

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

func FormatData(data map[string]any, format Format, opts ...Option) (string, error)

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

func FormatValue(key string, value any, format Format, opts ...Option) (string, error)

FormatValue formats a single key-value pair in the specified format.

func GetPathFromEnvironment

func GetPathFromEnvironment(envSlice []string) string

GetPathFromEnvironment extracts the PATH value from an environment slice.

func MergeGlobalEnv

func MergeGlobalEnv(baseEnv []string, globalEnv map[string]string) []string

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

func MergeSystemEnv(componentEnvList []string) []string

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

func MergeSystemEnvSimple(newEnvList []string) []string

MergeSystemEnvSimple merges system environment variables with new env without TF_CLI_ARGS_* special handling. Priority order: system env < new env list.

func MergeSystemEnvSimpleWithGlobal

func MergeSystemEnvSimpleWithGlobal(newEnvList []string, globalEnv map[string]string) []string

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

func MergeSystemEnvWithGlobal(componentEnvList []string, globalEnv map[string]string) []string

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

func PrependToPath(currentPath, newDir string) string

PrependToPath adds a directory to the beginning of the PATH environment variable. Returns the new PATH value.

func UpdateEnvVar

func UpdateEnvVar(envSlice []string, key, value string) []string

UpdateEnvVar updates or adds an environment variable in an environment slice. Returns a new environment slice with the variable updated.

func UpdateEnvironmentPath

func UpdateEnvironmentPath(envSlice []string, newDir string) []string

UpdateEnvironmentPath updates the PATH in an environment slice. Returns a new environment slice with the updated PATH.

func ValueToString added in v1.205.0

func ValueToString(value any) string

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

func WriteToFile(path string, content string) error

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

func NewBuilder(initial ...[]string) *Builder

NewBuilder creates a new Builder with an optional initial environment slice. If no initial slice is provided, starts with an empty slice.

func (*Builder) Build

func (b *Builder) Build() []string

Build returns the constructed environment slice.

func (*Builder) WithEnv

func (b *Builder) WithEnv(keyValue string) *Builder

WithEnv adds an environment variable in KEY=value format.

func (*Builder) WithEnvMap

func (b *Builder) WithEnvMap(envMap map[string]string) *Builder

WithEnvMap adds all environment variables from a map.

func (*Builder) WithEnvVar

func (b *Builder) WithEnvVar(key, value string) *Builder

WithEnvVar adds an environment variable with the given key and value.

func (*Builder) WithEnvVarf

func (b *Builder) WithEnvVarf(key, format string, args ...any) *Builder

WithEnvVarf adds an environment variable using a format string for the 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

func ParseFormat(s string) (Format, error)

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

func WithExport(export bool) Option

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

func WithFlatten(separator string) Option

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.

Jump to

Keyboard shortcuts

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