envvar

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: BSD-3-Clause Imports: 3 Imported by: 28

Documentation

Overview

Package envvar implements utilities for processing environment variables. There are three representations of environment variables:

  1. []"key=value" # hard to get and set, used by standard Go packages
  2. map[key]value # simple to get and set, nicest syntax
  3. *envvar.Vars # simple to get and set, also tracks deltas

The slice form (1) is used by standard Go packages, presumably since it's similar to the underlying OS representation. The map form (2) is convenient to use, and has native Go map syntax. The Vars form (3) is also convenient to use, and tracks deltas when mutations are performed.

This package provides utilities to easily use and convert between the three representations.

Empty keys are invalid and silently skipped in operations over all representations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendUniqueToken

func AppendUniqueToken(value, separator, token string) string

AppendUniqueToken appends token to value, which is separated by separator, and removes all empty and duplicate tokens. Returns a string where token only occurs once, and is last.

func CopyMap

func CopyMap(from map[string]string) map[string]string

CopyMap returns a copy of from, with empty keys dropped.

func CopySlice

func CopySlice(from []string) []string

CopySlice returns a copy of from, with empty keys dropped, and ordered by key. If the same key appears more than once the last one "wins"; the value is set based on the last slice element containing that key.

func FilterToken

func FilterToken(tokens []string, target string) []string

FilterToken returns a new slice containing tokens that are not empty or match the target, and in the same relative order as the original slice.

func JoinKeyValue

func JoinKeyValue(key, value string) string

JoinKeyValue joins key and value into a single string "key=value".

func JoinTokens

func JoinTokens(tokens []string, separator string) string

JoinTokens is like strings.Join(tokens, separator), but also filters out empty tokens.

func MapToSlice

func MapToSlice(from map[string]string) []string

MapToSlice converts from the map to the slice representation. The returned slice is in sorted order.

func MergeMaps

func MergeMaps(maps ...map[string]string) map[string]string

MergeMaps merges together maps, and returns a new map with the merged result. If the same key appears in more than one input map, the last one "wins"; the value is set based on the last map containing that key.

As a result of its semantics, MergeMaps called with a single map returns a copy of the map, with empty keys dropped.

func MergeSlices

func MergeSlices(slices ...[]string) []string

MergeSlices merges together slices, and returns a new slice with the merged result. If the same key appears more than once in a single input slice, or in more than one input slice, the last one "wins"; the value is set based on the last slice element in the last slice containing that key.

As a result of its semantics, MergeSlices called with a single slice returns a copy of the slice, with empty keys dropped.

func PrependUniqueToken

func PrependUniqueToken(value, separator, token string) string

PrependUniqueToken prepends token to value, which is separated by separator, removing all empty and duplicate tokens. Returns a string where token only occurs once, and is first.

func SliceToMap

func SliceToMap(from []string) map[string]string

SliceToMap converts from the slice to the map representation. If the same key appears more than once, the last one "wins"; the value is set based on the last slice element containing that key.

func SortByKey

func SortByKey(vars []string)

SortByKey sorts vars into ascending key order, where vars is expected to be in the []"key=value" slice representation.

func SplitKeyValue

func SplitKeyValue(kv string) (string, string)

SplitKeyValue splits kv into its key and value components. The format of kv is "key=value"; the split is performed on the first '=' character.

func SplitTokens

func SplitTokens(value, separator string) []string

SplitTokens is like strings.Split(value, separator), but also filters out empty tokens. Thus SplitTokens("", ":") returns a nil slice, unlike strings.SplitTokens which returns a slice with a single empty string.

func UniqueTokens

func UniqueTokens(tokens []string) []string

UniqueTokens returns a new slice containing tokens that are not empty or duplicated, and in the same relative order as the original slice.

Types

type Vars

type Vars struct {
	// contains filtered or unexported fields
}

Vars is a mutable set of environment variables that tracks deltas.

Vars are initialized with a base environment, and may be mutated with calls to Set and SetTokens. The resulting environment is retrieved with calls to ToMap and ToSlice.

Mutations are tracked separately from the base environment; call Deltas to retrieve only the environment variables that have been changed.

The zero Vars has an empty environment, and supports all methods.

func VarsFromMap

func VarsFromMap(base map[string]string) *Vars

VarsFromMap returns a new Vars initialized from the given base map.

func VarsFromOS

func VarsFromOS() *Vars

VarsFromOS returns a new Vars initialized from os.Environ.

func VarsFromSlice

func VarsFromSlice(base []string) *Vars

VarsFromSlice returns a new Vars initialized from the given base slice.

func (*Vars) Base

func (x *Vars) Base() map[string]string

Base returns a copy of the original base environment.

Mutating the returned map does not affect x.

func (*Vars) Contains

func (x *Vars) Contains(key string) bool

Contains returns true iff the key exists in the current set of variables.

func (*Vars) Delete

func (x *Vars) Delete(keys ...string)

Delete removes the given keys. Subsequent calls to Contains on each key will return false.

func (*Vars) Deltas

func (x *Vars) Deltas() map[string]*string

Deltas returns the set of variables that have been mutated after initialization.

If the last mutation for key K was Set or SetTokens, map[K] contains a non-nil pointer to the last value that was set. If the last mutation for key K was Delete, map[K] contains a nil pointer.

Mutating the returned map does not affect x.

func (*Vars) Get

func (x *Vars) Get(key string) string

Get returns the value associated with key. Returns "" if the key doesn't exist, or if the key has an empty value. Use Contains to test for existence.

func (*Vars) GetTokens

func (x *Vars) GetTokens(key, separator string) []string

GetTokens is a convenience that calls SplitTokens(x.Get(key), separator).

func (*Vars) Set

func (x *Vars) Set(key, value string)

Set assigns key to the given value.

func (*Vars) SetTokens

func (x *Vars) SetTokens(key string, tokens []string, separator string)

SetTokens is a convenience that calls x.Set(key, JoinTokens(tokens, separator)).

func (*Vars) ToMap

func (x *Vars) ToMap() map[string]string

ToMap returns the map representation of the current set of variables.

Mutating the returned map does not affect x.

func (*Vars) ToSlice

func (x *Vars) ToSlice() []string

ToSlice returns the slice representation of the current set of variables.

Mutating the returned slice does not affect x.

func (*Vars) UpdateOS

func (x *Vars) UpdateOS() error

UpdateOS updates the OS with the current set of variables. All variables are visited in sorted order, and os.Setenv is called for each variable.

Returns the first error encountered, if any.

Jump to

Keyboard shortcuts

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