env

package
v0.0.0-...-af98851 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

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 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

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

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

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

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

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

Get retrieves the value associated with the given key or returns an error if the key is not found.

func (Env) GetAll

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

GetAll returns a new Env containing all key-value pairs that satisfy the given predicate function.

func (Env) GetAllMatching

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

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

func (e Env) GetAllWithPrefix(prefix string) Env

GetAllWithPrefix returns all environment variables with keys starting with the given prefix.

func (Env) GetAllWithSuffix

func (e Env) GetAllWithSuffix(suffix string) Env

GetAllWithSuffix returns all environment variables with keys ending with the given suffix.

func (Env) GetAllWithValue

func (e Env) GetAllWithValue(value string) Env

GetAllWithValue returns all environment variables with the exact given value.

func (Env) GetOrDefault

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

GetOrDefault retrieves the value for the given key, or returns the provided defaultValue if the key is not found.

func (*Env) Merge

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

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

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

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

func (e Env) Normalize() Env

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

func (e Env) Normalized() Env

Normalized returns a copy of the Env with all keys normalized to uppercase on Windows. On other operating systems, it returns the Env unchanged.

func (Env) ToEnv

func (e Env) ToEnv() error

ToEnv sets all the key-value pairs in the Env as environment variables using os.Setenv. It returns an error if setting any environment variable fails.

func (Env) ToSlice

func (e Env) ToSlice() []string

ToSlice converts the Env to a slice of `key=value` strings. Each key-value pair in the Env is formatted as "key=value" and added to the slice.

Jump to

Keyboard shortcuts

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