env

package
v0.0.13-beta Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package env provides utilities for managing environment variables in Go. It offers functionality for loading, manipulating, and normalizing environment variables from various sources, such as the system environment or .env files. Additionally, it allows environment variable expansion, filtering, and conversion between different formats.

Key features include: - Loading environment variables from the OS or .env files. - Adding, merging, and normalizing environment variables. - Expanding environment variable values based on the current system environment. - Converting environment variables to and from slices of `key=value` strings.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEnvVarNotFound is returned when an expected environment variable is not found.
	ErrEnvVarNotFound = errors.New("environment variable not found")

	// ErrEnvMalformed is returned when an environment variable is malformed or contains invalid data.
	ErrEnvMalformed = errors.New("environment variable is malformed")
)

Functions

This section is empty.

Types

type Env

type Env map[string]string

Env represents a map of environment variables, where the keys and values are strings. It can be used to access and manage environment settings.

func FromDotEnv

func FromDotEnv(path string) (Env, error)

FromDotEnv loads environment variables from a .env file. Reads and parses the file at the given path using godotenv. Returns the variables as a normalized Env map or an error if the file cannot be read or parsed.

func FromEnv

func FromEnv() Env

FromEnv creates an Env from the current process environment. Retrieves all environment variables using os.Environ, normalizes their format, and returns them as an Env map.

func FromSlice

func FromSlice(slice ...string) (Env, error)

FromSlice creates an Env from a list of key-value strings. Each string should be in the format "key=value". Returns an error if any string is malformed. Normalizes the resulting environment.

func (*Env) Add

func (e *Env) Add(kv string) error

Add parses and adds a key-value pair to the environment. Takes a string in the format "key=value" and adds it to the Env map. Returns an error if the string doesn't contain exactly one '=' separator.

func (*Env) Delete added in v0.0.13

func (e *Env) Delete(key string)

Delete removes an environment variable by key.

func (*Env) Expand

func (e *Env) Expand()

Expand processes environment variable references in values. Replaces ${var} or $var patterns in each value with the corresponding environment variable value. Modifies the environment in place.

func (Env) Get

func (e Env) Get(key string) string

Get retrieves an environment variable's value by key. Returns an empty string if the key doesn't exist.

func (Env) GetAll

func (e Env) GetAll(predicate func(key, value string) bool) Env

GetAll filters environment variables using a predicate. Returns a new Env containing only the key-value pairs for which the predicate function returns true.

func (Env) GetAllMatching

func (e Env) GetAllMatching(pattern string) (Env, error)

GetAllMatching returns environment variables by regex pattern. Returns a new Env containing only variables whose keys match the specified regex pattern. Returns an error if the pattern is invalid.

func (Env) GetAllWithPrefix

func (e Env) GetAllWithPrefix(prefix string) Env

GetAllWithPrefix returns environment variables by prefix. Returns a new Env containing only variables whose keys start with the specified prefix.

func (Env) GetAllWithSuffix

func (e Env) GetAllWithSuffix(suffix string) Env

GetAllWithSuffix returns environment variables by suffix. Returns a new Env containing only variables whose keys end with the specified suffix.

func (Env) GetAllWithValue

func (e Env) GetAllWithValue(value string) Env

GetAllWithValue returns environment variables by value. Returns a new Env containing only variables that have the exact specified value.

func (Env) GetAny added in v0.0.8

func (e Env) GetAny(keys ...string) string

GetAny retrieves the first available environment value. Tries each key in order until a value is found, returning an empty string if none of the keys exist.

func (Env) GetOrDefault

func (e Env) GetOrDefault(key, defaultValue string) string

GetOrDefault retrieves an environment value with fallback. Returns the environment value if the key exists, otherwise returns the provided default value.

func (Env) Has added in v0.0.11

func (e Env) Has(key string) bool

Has checks if a key exists in the environment. Returns true if the key exists, false otherwise.

func (*Env) Merge

func (e *Env) Merge(envs ...Env)

Merge combines multiple environments into the current one. Copies values from the provided environments into this one, preserving existing values in case of key conflicts.

func (Env) Merged

func (e Env) Merged(envs ...Env) Env

Merged creates a new environment by combining multiple environments. Returns a new Env containing all values from this environment plus any non-conflicting values from the provided environments. Does not modify the original environment.

func (Env) MustGet added in v0.0.8

func (e Env) MustGet(key string) (string, error)

MustGet retrieves an environment variable's value by key. Returns an error if the key doesn't exist in the environment.

func (Env) Normalize

func (e Env) Normalize() Env

Normalize creates a new environment with uppercase keys. Returns a copy of the environment with all keys converted to uppercase, which ensures consistent behavior on case-insensitive systems like Windows.

func (Env) Normalized

func (e Env) Normalized() Env

Normalized returns a platform-appropriate normalized environment. On Windows, converts all keys to uppercase for case-insensitive compatibility. On other systems, returns the environment unchanged.

func (Env) ToEnv

func (e Env) ToEnv() error

ToEnv applies the environment to the current process. Sets each key-value pair as a process environment variable using os.Setenv. Returns an error if any variable cannot be set.

func (Env) ToSlice

func (e Env) ToSlice() []string

ToSlice converts the environment to a string slice. Returns a slice where each element is a "key=value" string representing an environment variable.

Jump to

Keyboard shortcuts

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