Documentation ¶
Overview ¶
Package env provides convenience functions for retrieving data from environment variables
Package env provides convenience functions for retrieving data from environment variables
Index ¶
- func AsBool(s string) bool
- func AsBoolSimple(s string) bool
- func Bool(envName string) bool
- func BoolSimple(envName string) bool
- func Contains(envName string, value string) bool
- func Dir(name string, optionalDefault ...string) string
- func DurationHours(envName string, defaultValue int64) time.Duration
- func DurationMinutes(envName string, defaultValue int64) time.Duration
- func DurationSeconds(envName string, defaultValue int64) time.Duration
- func Environ() []string
- func EtcEnvironment(envVarName string) (string, error)
- func ExpandUser(path string) string
- func False(s string) bool
- func File(name string, optionalDefault ...string) string
- func Float32(envName string, defaultValue float32) float32
- func Float64(envName string, defaultValue float64) float64
- func Has(envName string) bool
- func HomeDir() string
- func Int(envName string, defaultValue int) int
- func Int16(envName string, defaultValue int16) int16
- func Int32(envName string, defaultValue int32) int32
- func Int64(envName string, defaultValue int64) int64
- func Int8(envName string, defaultValue int8) int8
- func Is(envName, value string) bool
- func Load()
- func OnlyXSession() bool
- func Path() []string
- func Set(name, value string) error
- func Str(name string, optionalDefault ...string) string
- func StrAlt(name1, name2 string, optionalDefault ...string) string
- func True(s string) bool
- func UInt16(envName string, defaultValue uint16) uint16
- func UInt32(envName string, defaultValue uint32) uint32
- func UInt64(envName string, defaultValue uint64) uint64
- func UInt8(envName string, defaultValue uint8) uint8
- func Unload()
- func Unset(name string) error
- func WaylandSession() bool
- func XOrWaylandSession() bool
- func XSession() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsBool ¶
AsBool can be used to interpret a string value as either true or false. Examples of true values are "yes" and "1".
func AsBoolSimple ¶
AsBoolSimple can be used to interpret a string value as either true or false. Only "1" is true, anything else is false.
func Bool ¶
Bool returns the bool value of the given environment variable name. Returns false if it is not declared or empty.
func BoolSimple ¶
BoolSimple returns the bool value of the given environment variable name. Returns false if it is not declared or empty. Only "1" is true.
func DurationHours ¶
DurationHours interprets the environment variable value as hours and returns a time.Duration. The given default number is interpreted as the number of hours.
func DurationMinutes ¶
DurationMinutes interprets the environment variable value as minutes and returns a time.Duration. The given default number is interpreted as the number of minutes.
func DurationSeconds ¶
DurationSeconds interprets the environment variable value as seconds and returns a time.Duration. The given default number is interpreted as the number of seconds.
func Environ ¶ added in v2.2.2
func Environ() []string
Environ returns either the cached environment, or os.Environ()
func EtcEnvironment ¶ added in v2.1.0
EtcEnvironment tries to find the value of the given variable name in /etc/environment. The variable name could be ie. "JAVA_HOME".
func ExpandUser ¶
ExpandUser replaces a leading ~ or $HOME with the path to the home directory of the current user
func False ¶ added in v2.2.0
False checks if the given string is likely to be interpreted as a "false" value
func File ¶
File does the same as Str, but expands a leading "~" or "$HOME" string to the home directory of the current user.
func Float32 ¶
Float32 returns the number stored in the environment variable, or the provided default value.
func Float64 ¶
Float64 returns the number stored in the environment variable, or the provided default value.
func HomeDir ¶
func HomeDir() string
HomeDir returns the path to the home directory of the user, if available. If not available, $LOGNAME or $USER are used to construct a path starting with /home/. If $LOGNAME and $USER are not available, just "/tmp" is returned. The returned string is what the home directory should have been named, if it would have existed. No checks are made for if the directory exists.
func Int ¶
Int returns the number stored in the environment variable, or the provided default value.
func Int16 ¶
Int16 returns the number stored in the environment variable, or the provided default value.
func Int32 ¶
Int32 returns the number stored in the environment variable, or the provided default value.
func Int64 ¶
Int64 returns the number stored in the environment variable, or the provided default value.
func Int8 ¶
Int8 returns the number stored in the environment variable, or the provided default value.
func Is ¶
Is returns true if the given environment variable is the given string value. The whitespace of both values are trimmed before the comparison.
func Load ¶
func Load()
Load reads all environment variables into the environment map. It also instructs env to use the cache. If a program uses os.Setenv, then Load() should be called after that, in order to read the new values. This function can be used both as an "init and enable cache" function and as a "reload" function.
func OnlyXSession ¶ added in v2.2.4
func OnlyXSession() bool
OnlyXSession returns true if DISPLAY is set and WaylandSession() is false.
func Set ¶
Set calls os.Setenv. If caching is enabled, the value in the environment map is also set and there is no need to call Load() to re-read the environment variables from the system.
func Str ¶
Str does the same as os.Getenv, but allows the user to provide a default value (optional). Only the first optional argument is used, the rest is discarded.
func StrAlt ¶
StrAlt will return the string value of the first given environment variable name, or, if that is not available, use the string value of the second given environment variable. If none are available, the optional default string is returned.
func True ¶ added in v2.2.0
True checks if the given string is likely to be interpreted as a "true" value
func UInt16 ¶
UInt16 returns the number stored in the environment variable, or the provided default value.
func UInt32 ¶
UInt32 returns the number stored in the environment variable, or the provided default value.
func UInt64 ¶
UInt64 returns the number stored in the environment variable, or the provided default value.
func UInt8 ¶
UInt8 returns the number stored in the environment variable, or the provided default value.
func Unset ¶
Unset will clear an environment variable by calling os.Setenv(name, ""). The cache entry will also be cleared if useCaching is true.
func WaylandSession ¶ added in v2.2.3
func WaylandSession() bool
WaylandSession returns true of XDG_SESSION_TYPE is "wayland" or if DESKTOP_SESSION contains "wayland".
func XOrWaylandSession ¶ added in v2.2.3
func XOrWaylandSession() bool
XOrWaylandSession returns true if DISPLAY is set or WaylandSession() returns true.
Types ¶
This section is empty.