Documentation ¶
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) GetOrDefault(key, defaultValue string) string
- func (e *Env) Merge(envs ...Env)
- func (e Env) Merged(envs ...Env) Env
- 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) 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) 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.