-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
59 lines (51 loc) · 1.62 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package env provides a thin wrapper around accessing environment
// variables.
package env
import (
"sync"
)
var muGlobal sync.RWMutex
var global Config
// Prefix returns the prefix used to format environment variable names
func Prefix() string {
muGlobal.RLock()
defer muGlobal.RUnlock()
return global.Prefix()
}
// SetPrefix sets the prefix used to format environment variable names
func SetPrefix(prefix string) *Config {
muGlobal.Lock()
defer muGlobal.Unlock()
return global.SetPrefix(prefix)
}
// VarName formats the given name into an environment variable name
// using the rules defined by the global Config.
func VarName(name string) string {
muGlobal.RLock()
defer muGlobal.RUnlock()
return global.VarName(name)
}
// Lookup is the same as `os.LookupEnv`, except it formats the
// environment variable named using `VarName()` before passing
// it to `os.LookupEnv`
func Lookup(name string) (string, bool) {
muGlobal.RLock()
defer muGlobal.RUnlock()
return global.Lookup(name)
}
// Value returns the value of the environment variable named `name`,
// except it retrieves the value using `Lookup()`. Also, if the
// value of the environment variable is empty, it returns `defaultValue`
func Value(name, defaultValue string) string {
muGlobal.RLock()
defer muGlobal.RUnlock()
return global.Value(name, defaultValue)
}
// SetValue sets the value of the environment variable named `name`
// to `value`, except it formats the environment variable name using
// `VarName()` before passing it to `os.Setenv`
func SetValue(name, value string) error {
muGlobal.RLock()
defer muGlobal.RUnlock()
return global.SetValue(name, value)
}