diff --git a/envs/funcs.go b/envs/funcs.go index 6676ade..0d3d2a2 100644 --- a/envs/funcs.go +++ b/envs/funcs.go @@ -17,6 +17,34 @@ import ( `r00t2.io/sysutils/paths` ) +/* + DefEnv operates like Python's .get() method on dicts (maps); + if the environment variable specified by key does not exist/is not specified, + then the value specified by fallback will be returned instead + otherwise key's value is returned. +*/ +func DefEnv(key, fallback string) (value string) { + + var exists bool + + if value, exists = os.LookupEnv(key); !exists { + value = fallback + } + + return +} + +// DefEnvBlank is like DefEnv but will ADDITIONALLY/ALSO apply fallback if key is *defined/exists but is an empty string*. +func DefEnvBlank(key, fallback string) (value string) { + + value = DefEnv(key, fallback) + if value == "" { + value = fallback + } + + return +} + // GetEnvMap returns a map of all environment variables. All values are strings. func GetEnvMap() (envVars map[string]string) {