Documentation ¶
Overview ¶
Package environ is an environment variable manipulation library.
It couples the system's environment, represented as a []string of KEY[=VALUE] strings, into a key/value map and back.
Index ¶
- func Join(k, v string) string
- func Split(v string) (key, value string)
- type Env
- func (e Env) Clone() Env
- func (e Env) Get(k string) string
- func (e Env) Iter(cb func(k, v string) error) error
- func (e Env) Len() int
- func (e Env) Load(m map[string]string)
- func (e Env) Lookup(k string) (v string, ok bool)
- func (e Env) Map() map[string]string
- func (e Env) Remove(k string) bool
- func (e Env) RemoveMatch(fn func(k, v string) bool)
- func (e Env) Set(k, v string)
- func (e Env) SetEntry(entry string)
- func (e Env) SetInCtx(ctx context.Context) context.Context
- func (e Env) Sorted() []string
- func (e Env) String() string
- func (e Env) Update(other Env)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Env ¶
type Env struct {
// contains filtered or unexported fields
}
Env contains system environment variables. It preserves each environment variable verbatim (even if invalid).
Treat it as a map[...] (i.e. pass by value, but assume this value is actually a reference and the object can be mutated through it).
An empty value can be read or cloned, but should not be written to. If you want to get an empty modifiable Env use New(nil).
func FromCtx ¶
FromCtx returns a copy of the current Env in `ctx`.
This is guaranteed to return a non-nil Env (i.e. it's always safe for assignment/manipulation).
If no Env has been set with `SetInCtx`, this returns `System()`.
func New ¶
New instantiates a new Env instance from the supplied set of environment KEY=VALUE strings (e.g. as returned by os.Environ()).
func System ¶
func System() Env
System returns an Env instance instantiated with the current os.Environ values.
func (Env) Clone ¶
Clone creates a new Env instance that is identical to, but independent from, e.
The returned clone is always safe to modify.
func (Env) Get ¶
Get retrieves the value of the environment variable named by the key.
It returns the value, which will be empty if the variable is not present. To distinguish between an empty value and an unset value, use Lookup.
func (Env) Iter ¶
Iter iterates through all of the key/value pairs in Env and invokes the supplied callback, cb, for each element.
If the callback returns error, iteration will stop immediately.
func (Env) Load ¶
Load adds environment variables defined in a key/value map to an existing environment.
func (Env) Lookup ¶
Lookup retrieves the value of the environment variable named by the key.
If the variable is present in the environment the value (which may be empty) is returned and the boolean is true. Otherwise the returned value will be empty and the boolean will be false.
func (Env) Map ¶
Map returns a map of the key/value values in the environment.
This is a clone of the contents of e; manipulating this map will not change the values in e.
If env is either nil or empty, returns nil.
func (Env) Remove ¶
Remove removes a value from the environment, returning true if the value was present. If the value was missing, Remove returns false.
Remove is different from Set(k, "") in that Set persists the key with an empty value, while Remove removes it entirely.
func (Env) RemoveMatch ¶
RemoveMatch iterates over all keys and values in the environment, invoking the callback function, fn, for each key/value pair. If fn returns true, the key is removed from the environment.
func (Env) Set ¶
Set sets the supplied environment key and value.
Panics if the Env is zero-valued i.e. equals to Env{}. Use New(nil) to create a modifiable empty environment.
func (Env) SetEntry ¶
SetEntry sets the supplied environment to a "KEY[=VALUE]" entry.
Panics if the Env is zero-valued i.e. equals to Env{}. Use New(nil) to create a modifiable empty environment.
func (Env) SetInCtx ¶
SetInCtx installs a copy of the Env into the context, which can be retrieved with `FromCtx`.
func (Env) Update ¶
Update adds all key/value from other to the current environment. If there is a duplicate key, the value from other will overwrite the value from e.
Values from other will be sorted and added in alphabetic order. This means that if e is case insensitive and there are multiple keys in other that converge on the same case insensitive key, the one that is alphabetically highest will be added.
Panics if the Env is zero-valued i.e. equals to Env{}. Use New(nil) to create a modifiable empty environment.