ADDED:
- env/DefEnv(), env/DefEnvBlank()
This commit is contained in:
brent saner 2024-09-06 12:50:23 -04:00
parent 5dc944cf21
commit 1a93d5d9f3
Signed by: bts
GPG Key ID: 8C004C2F93481F6B

View File

@ -17,6 +17,34 @@ import (
`r00t2.io/sysutils/paths` `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. // GetEnvMap returns a map of all environment variables. All values are strings.
func GetEnvMap() (envVars map[string]string) { func GetEnvMap() (envVars map[string]string) {