-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
70 lines (60 loc) · 1.69 KB
/
config.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
60
61
62
63
64
65
66
67
68
69
70
package env
import (
"os"
)
type Config struct {
prefix string
separator string
}
// Separator returns the separator used to join the components
// of an environment variable. By defualt the separator is "_".
func (c *Config) Separator() string {
v := c.separator
if v == "" {
return "_"
}
return v
}
// SetPrefix sets the prefix used to format environment variable names
func (c *Config) SetSeparator(separator string) *Config {
c.separator = separator
return c
}
func (c *Config) Prefix() string {
v := c.prefix
if v == "" {
return "MYAPP"
}
return v
}
func (c *Config) SetPrefix(prefix string) *Config {
c.prefix = prefix
return c
}
// VarName formats the given name into an environment variable name
// using the rules defined by the Config.
func (c *Config) VarName(name string) string {
return c.Prefix() + c.Separator() + name
}
// Lookup is the same as `os.LookupEnv`, except it formats the
// environment variable named using `VarName()` before passing
// it to `os.LookupEnv`
func (c *Config) Lookup(name string) (string, bool) {
return os.LookupEnv(c.VarName(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 (c *Config) Value(name, defaultValue string) string {
v, ok := c.Lookup(name)
if ok {
return v
}
return 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 (c *Config) SetValue(name, value string) error {
return os.Setenv(c.VarName(name), value)
}