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 ¶
- Variables
- type Env
- func (e *Env) Add(kv string) error
- func (e *Env) Expand()
- func (e Env) Get(key string) (string, error)
- func (e Env) GetAll(predicate func(key, value string) bool) Env
- func (e Env) GetAllMatching(pattern string) (Env, error)
- func (e Env) GetAllWithPrefix(prefix string) Env
- func (e Env) GetAllWithSuffix(suffix string) Env
- func (e Env) GetAllWithValue(value string) Env
- func (e Env) GetAny(keys ...string) string
- func (e Env) GetOrDefault(key, defaultValue string) string
- func (e *Env) Merge(envs ...Env)
- func (e Env) Merged(envs ...Env) Env
- func (e Env) MustGet(key string) (string, error)
- func (e Env) Normalize() Env
- func (e Env) Normalized() Env
- func (e Env) ToEnv() error
- func (e Env) ToSlice() []string
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
FromDotEnv loads environment variables from a .env file specified by the path. It returns an error if there is an issue reading the file or processing its contents.
func FromEnv ¶
func FromEnv() Env
FromEnv returns the current environment variables as an Env. It uses os.Environ to fetch all the environment variables and normalizes them before returning.
func FromSlice ¶
FromSlice constructs an Env from a slice of `key=value` strings. It returns an error if any string in the slice is malformed.
func (*Env) Add ¶
Add splits a `key=value` string and adds it to the Env map. It returns an error if the input is not properly formatted, expecting exactly one '=' separator.
func (*Env) Expand ¶
func (e *Env) Expand()
Expand expands environment variables in the Env values using os.ExpandEnv. Each value in the Env is processed, replacing any occurrences of ${var} or $var with the corresponding value from the environment.
func (Env) Get ¶
Get retrieves the value associated with the given key or returns an error if the key is not found.
func (Env) GetAll ¶
GetAll returns a new Env containing all key-value pairs that satisfy the given predicate function.
func (Env) GetAllMatching ¶
GetAllMatching returns all environment variables with keys matching the given regex pattern. It returns an error if the provided regex pattern is invalid.
func (Env) GetAllWithPrefix ¶
GetAllWithPrefix returns all environment variables with keys starting with the given prefix.
func (Env) GetAllWithSuffix ¶
GetAllWithSuffix returns all environment variables with keys ending with the given suffix.
func (Env) GetAllWithValue ¶
GetAllWithValue returns all environment variables with the exact given value.
func (Env) GetAny ¶ added in v0.0.8
GetAny retrieves the value for the first key found in the given list of keys, from left to right.
func (Env) GetOrDefault ¶
GetOrDefault retrieves the value for the given key, or returns the provided defaultValue if the key is not found.
func (*Env) Merge ¶
Merge merges another Env into the current Env, without overwriting existing keys in the current Env. If a key in the other Env already exists in the current Env, it is not updated.
func (Env) Merged ¶
Merged returns a new Env by merging the given Env into the current Env, without overwriting existing keys in the original Env. This method does not mutate the original Env.
func (Env) MustGet ¶ added in v0.0.8
MustGet retrieves the value associated with the given key or returns an error if the key is not found.
func (Env) Normalize ¶
Normalize returns a copy of the Env with all keys converted to uppercase. This is primarily useful for ensuring consistent key handling on case-insensitive systems like Windows.
func (Env) Normalized ¶
Normalized returns a copy of the Env with all keys normalized to uppercase on Windows. On other operating systems, it returns the Env unchanged.