env

package module
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: BSD-3-Clause Imports: 8 Imported by: 5

README

Env Build GoDoc Go Report Card

Makes fetching and interpreting environment variables easy and safe.

Being able to provide default values when retrieving environment variables often makes program logic shorter and more readable.

Functions

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 value is used, if the environment variable value is empty or not set.

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 Int

func Int(envName string, defaultValue int) int

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

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 Has

func Has(s string) bool

Has return true if the given environment variable name is non-empty.

DurationSeconds

Takes a default int64 value, for the number of seconds, interprets the environment variable as the number of seconds and returns a time.Duration.

DurationMinutes

Takes a default int64 value, for the number of minutes, interprets the environment variable as the number of seconds and returns a time.Duration.

DurationHours

Takes a default int64 value, for the number of hours, interprets the environment variable as the number of seconds and returns a time.Duration.

Contains

Checks if the given environment variable contains the given string.

Is

Checks if the given environment variable is the given value, with leading and trailing spaces trimmed before comparing both values.

HomeDir

Returns the home directory of the current user, or /tmp if it is not available. /home/$LOGNAME or /home/$USER are returned if $HOME is not set.

ExpandUser

Replaces ~ or $HOME at the start of a string with the home directory of the current user.

File

Does the same as the Str function, but replaces a leading ~ or $HOME with the home directory of the current user.

Dir

Does the same as the Str function, but replaces a leading ~ or $HOME with the home directory of the current user.

Path

Returns the current $PATH as a slice of strings.

Other functions

There are also: Float64, Float32, Uint64, Uint32, Uint16, Uint8, Int64, Int32, Int16 and Int8 functions available.

Example

package main

import (
    "fmt"
    "github.com/xyproto/env"
)

func main() {
    fmt.Println(env.DurationSeconds("REQUEST_TIMEOUT", 1800))
}

Running the above program like this: REQUEST_TIMEOUT=1200 ./main, outputs:

20m0s

Cache

  • If Load() is called, then cache is enabled and all environment variables are read from the system.
  • The functions offered by this package will then read the values from the cache instead.
  • If variables are set with os.Setenv, then Load() can be used to read the environment variables from the system again.
  • Unload() can be called to stop using the cache and only rely on os.Setenv and os.Getenv again.
  • The Set and Unset functions can be used to both set values with os.Setenv and also modify the cache, so that an additional call to Load() is not needed.

General info

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 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 Contains added in v1.5.0

func Contains(envName string, value string) bool

Contains checks if the given environment variable contains the given string

func Dir added in v1.7.0

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

Dir does the same as File

func DurationHours added in v1.4.0

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 added in v1.4.0

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 added in v1.3.0

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 ExpandUser added in v1.6.0

func ExpandUser(path string) string

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

func File added in v1.7.0

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 added in v1.9.1

func Float32(envName string, defaultValue float32) float32

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

func Float64 added in v1.2.0

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 added in v1.6.0

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 added in v1.9.1

func Int16(envName string, defaultValue int16) int16

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

func Int32 added in v1.9.1

func Int32(envName string, defaultValue int32) int32

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

func Int64 added in v1.2.0

func Int64(envName string, defaultValue int64) int64

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

func Int8 added in v1.9.1

func Int8(envName string, defaultValue int8) int8

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

func Is added in v1.5.0

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 added in v1.9.0

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 Path added in v1.7.0

func Path() []string

Path returns the elements in the $PATH environment variable

func Set added in v1.9.0

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 added in v1.6.0

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 UInt16 added in v1.9.1

func UInt16(envName string, defaultValue uint16) uint16

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

func UInt32 added in v1.9.1

func UInt32(envName string, defaultValue uint32) uint32

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

func UInt64 added in v1.9.1

func UInt64(envName string, defaultValue uint64) uint64

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

func UInt8 added in v1.9.1

func UInt8(envName string, defaultValue uint8) uint8

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

func Unload added in v1.9.0

func Unload()

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

func Unset added in v1.9.0

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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