-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
126 lines (115 loc) · 3.1 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package config
import (
"fmt"
"os"
"strings"
)
/*
EnvLoader implements a Loader type to parse settings from environment variables.
Environment variable names are generated by expanding the path for each setting
to a list of optional prefixes and ending with the setting's name:
opts := struct {
HTMLParser struct {
ElementIDs []string
Name string
}
}{}
results in paths
["HTMLParser", "ElementIDs"]
["HTMLParser", "Name"]
Each element is then replaced with the result of passing it to SplitName:
["HTML", "Parser", "Element", "IDs"]
["HTML", "Parser", "Name"]
Any dashes or underscores in each word are removed, the words capitalized, and
finally joined with underscores to produce the final names:
HTML_PARSER_ELEMENT_IDS
HTML_PARSER_NAME
The zero value is ready to use.
*/
type EnvLoader struct {
settings []Setting
}
// Name returns the name of the loader. It is always "env".
func (*EnvLoader) Name() string {
return "env"
}
/*
Init initializes the loader with the given settings.
Init must be called before Load.
*/
func (el *EnvLoader) Init(settings []Setting) {
names := make(map[string]struct{}, len(settings))
for i := range el.settings {
name := el.transformName(el.settings[i].Path)
if _, ok := names[name]; ok {
panic(fmt.Sprintf(
"duplicate environment variable name %s for %s",
name, el.settings[i].Path,
))
}
names[name] = struct{}{}
}
el.settings = settings
}
/*
Load attempts to parse the configured settings from environment variables.
The returned error, if non-nil, will be of type Errors. Each element will be
of type *ConversionError or *ValidationError.
*/
func (el *EnvLoader) Load() error {
var errs Errors
for i := range el.settings {
if val := os.Getenv(el.transformName(el.settings[i].Path)); val != "" {
if err := el.settings[i].Setter.Set(val); err != nil {
switch err := err.(type) {
case *ConversionError:
err.Path = el.settings[i].Path
case *ValidationError:
err.Path = el.settings[i].Path
}
errs.Append(err)
}
}
}
return errs.AsError()
}
/*
Usage returns a string with a list of environment variables names and their
descriptions.
*/
func (el *EnvLoader) Usage() string {
var b strings.Builder
b.WriteString("Environment Variables:\n")
for i := range el.settings {
b.WriteString(" ")
b.WriteString(el.transformName(el.settings[i].Path))
b.WriteString("=")
b.WriteString(FriendlyTypeName(el.settings[i].Setter.Get()))
usage := strings.Replace(
el.settings[i].Tag.Get("help"), "\n", "\n \t", -1,
)
z := isZeroValue(el.settings[i].Setter)
if usage != "" || !z {
b.WriteString("\n \t")
b.WriteString(usage)
if !z {
b.WriteString(" (default ")
b.WriteString(el.settings[i].Setter.String())
b.WriteString(")")
}
}
b.WriteString("\n")
}
return b.String()
}
func (el *EnvLoader) transformName(path *Path) string {
elements := path.Elements()
for i := range elements {
parts := SplitName(elements[i])
for i := range parts {
parts[i] = strings.Trim(strings.ToUpper(parts[i]), "-_")
}
elements[i] = strings.Join(parts, "_")
}
return strings.Join(elements, "_")
}