env

package module
v2.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: BSD-3-Clause Imports: 10 Imported by: 15

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBool

func AsBool(s string) bool

AsBool can be used to interpret a string value as either true or false. Examples of true values are "yes" and "1".

func AsBoolSimple

func AsBoolSimple(s string) bool

AsBoolSimple can be used to interpret a string value as either true or false. Only "1" is true, anything else is false.

func Bool

func Bool(envName string) bool

Bool returns the bool value of the given environment variable name. Returns false if it is not declared or empty.

func BoolSimple

func BoolSimple(envName string) bool

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 Contains

func Contains(envName string, value string) bool

Contains checks if the given environment variable contains the given string

func Dir

func Dir(name string, optionalDefault ...string) string

Dir does the same as File

func DurationHours

func DurationHours(envName string, defaultValue int64) time.Duration

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

func DurationMinutes(envName string, defaultValue int64) time.Duration

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

func DurationSeconds(envName string, defaultValue int64) time.Duration

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

func EtcEnvironment(envVarName string) (string, error)

EtcEnvironment tries to find the value of the given variable name in /etc/environment. The variable name could be ie. "JAVA_HOME".

func ExpandUser

func ExpandUser(path string) string

ExpandUser replaces a leading ~ or $HOME with the path to the home directory of the current user

func False added in v2.2.0

func False(s string) bool

False checks if the given string is likely to be interpreted as a "false" value

func File

func File(name string, optionalDefault ...string) string

File does the same as Str, but expands a leading "~" or "$HOME" string to the home directory of the current user.

func Float32

func Float32(envName string, defaultValue float32) float32

Float32 returns the number stored in the environment variable, or the provided default value.

func Float64

func Float64(envName string, defaultValue float64) float64

Float64 returns the number stored in the environment variable, or the provided default value.

func Has

func Has(envName string) bool

Has returns true if the given environment variable name is set and not empty.

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

func Int(envName string, defaultValue int) int

Int returns the number stored in the environment variable, or the provided default value.

func Int16

func Int16(envName string, defaultValue int16) int16

Int16 returns the number stored in the environment variable, or the provided default value.

func Int32

func Int32(envName string, defaultValue int32) int32

Int32 returns the number stored in the environment variable, or the provided default value.

func Int64

func Int64(envName string, defaultValue int64) int64

Int64 returns the number stored in the environment variable, or the provided default value.

func Int8

func Int8(envName string, defaultValue int8) int8

Int8 returns the number stored in the environment variable, or the provided default value.

func Is

func Is(envName, value string) bool

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 Path

func Path() []string

Path returns the elements in the $PATH environment variable

func Set

func Set(name, value string) error

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

func Str(name string, optionalDefault ...string) string

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

func StrAlt(name1, name2 string, optionalDefault ...string) string

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

func True(s string) bool

True checks if the given string is likely to be interpreted as a "true" value

func UInt16

func UInt16(envName string, defaultValue uint16) uint16

UInt16 returns the number stored in the environment variable, or the provided default value.

func UInt32

func UInt32(envName string, defaultValue uint32) uint32

UInt32 returns the number stored in the environment variable, or the provided default value.

func UInt64

func UInt64(envName string, defaultValue uint64) uint64

UInt64 returns the number stored in the environment variable, or the provided default value.

func UInt8

func UInt8(envName string, defaultValue uint8) uint8

UInt8 returns the number stored in the environment variable, or the provided default value.

func Unload

func Unload()

Unload clears the cache and configures env to not use the cache.

func Unset

func Unset(name string) error

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.

func XSession added in v2.2.3

func XSession() bool

XSession returns true if DISPLAY is set. X being available is not neccessarily in opposition to Wayland running.

Types

This section is empty.

Jump to

Keyboard shortcuts

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