env

package
v0.0.0-...-f8a288c Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

env is a convenient package to parse environment variables in a 12factor application

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(name string, def ...bool) bool

Bool parses given environment variables as a boolean, or returns the default if the environment variable is empty/unset. If the value is empty or unset it will return the first value of def or false if none is given. Evaluates true if the value case-insensitive matches 1|t|true|y|yes.

Example
defer testenv.Clear().Restore()
name := "some_bool_environment_variable_that_is_not_set"

fmt.Println(Bool(name))
fmt.Println(Bool(name, true))
fmt.Println(Bool(name, true, false))
fmt.Println(Bool(name, false, true))

os.Setenv(name, "true")
fmt.Println(Bool(name))
fmt.Println(Bool(name, false))
os.Setenv(name, "false")
fmt.Println(Bool(name))
fmt.Println(Bool(name, true))
os.Setenv(name, "t")
fmt.Println(Bool(name))
fmt.Println(Bool(name, false))
os.Setenv(name, "f")
fmt.Println(Bool(name))
fmt.Println(Bool(name, true))
os.Setenv(name, "1")
fmt.Println(Bool(name))
fmt.Println(Bool(name, false))
os.Setenv(name, "0")
fmt.Println(Bool(name))
fmt.Println(Bool(name, true))
os.Setenv(name, "random-value")
fmt.Println(Bool(name))
fmt.Println(Bool(name, true))
Output:

false
true
true
false
true
true
false
false
true
true
false
false
true
true
false
false
false
false

func Duration

func Duration(name string, def ...time.Duration) time.Duration

Duration parses given environment variable as a time.Duration, or returns the default if the environment variable is empty/unset. Duration will panic if it fails to parse the value.

Example
defer testenv.Clear().Restore()

name := "some_duration_environment_variable_that_is_not_set"
fmt.Println(Duration(name))
fmt.Println(Duration(name, 15*time.Minute))
fmt.Println(Duration(name, 15*time.Minute, 2*time.Hour))
fmt.Println(Duration(name, 0, 2*time.Hour))
os.Setenv(name, "14m2s")
fmt.Println(Duration(name))
fmt.Println(Duration(name).Seconds())
fmt.Println(Duration(name, 30*time.Minute))
Output:

0s
15m0s
15m0s
0s
14m2s
842
14m2s

func Get

func Get(name string, def ...string) string

Get retrieves the value of the environment variable named by the key. If the value is empty or unset it will return the first value of def or "" if none is given

Example
defer testenv.Clear().Restore()

name := "some_environment_variable_that_is_not_set"
fmt.Println(Get(name))
fmt.Println(Get(name, "this is the default"))
fmt.Println(Get(name, "this is the default", "this one is ignored"))
fmt.Println(Get(name, "", "this one is ignored"))
os.Setenv(name, "this is the value set")
fmt.Println(Get(name))
fmt.Println(Get(name, "this is the default"))
Output:


this is the default
this is the default

this is the value set
this is the value set

func Int

func Int(name string, def ...int) int

Int parses given environment variable as an int, or returns the default if the environment variable is empty/unset. Int will panic if it fails to parse the value.

Example
defer testenv.Clear().Restore()

name := "some_int_environment_variable_that_is_not_set"
fmt.Println(Int(name))
fmt.Println(Int(name, 42))
fmt.Println(Int(name, 42, 21))
fmt.Println(Int(name, 0, 48))

os.Setenv(name, strconv.Itoa(9))
fmt.Println(Int(name))
fmt.Println(Int(name, 42))
Output:

0
42
42
0
9
9

func URL

func URL(name string, def ...string) *url.URL

URL parses given environment variable as a URL, or returns the default if the environment variable is empty/unset. URL will panic if it fails to parse the value.

Example
defer testenv.Clear().Restore()

name := "some_url_environment_variable_that_is_not_set"
fmt.Println(URL(name))
fmt.Println(URL(name, "https://packet.com"))
fmt.Println(URL(name, "https://packet.com", "https://www.equinix.com"))
fmt.Println(URL(name, "", "https://tinkerbell.org"))
os.Setenv(name, "https://tinkerbell.org")
fmt.Println(URL(name))
fmt.Println(URL(name).Host)
fmt.Println(URL(name, "https://www.equinix.com/"))
Output:


https://packet.com
https://packet.com

https://tinkerbell.org
tinkerbell.org
https://tinkerbell.org

Types

This section is empty.

Jump to

Keyboard shortcuts

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