-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
38 lines (32 loc) · 668 Bytes
/
env.go
File metadata and controls
38 lines (32 loc) · 668 Bytes
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
package cli
import (
"os"
"strings"
)
// Env loads option values from environment variables
// with the given prefix. The prefix and option keys
// are converted to uppercase.
func Env(prefix string) Provider {
return &env{prefix}
}
// env loads option values from environment variables.
type env struct {
Prefix string
}
func (e *env) Provide(l *Loader) error {
for _, key := range l.Keys() {
var prefixed []string
if e.Prefix != "" {
prefixed = append([]string{e.Prefix}, key...)
} else {
prefixed = key
}
k := strings.ToUpper(UnderscoreKey(prefixed))
v, ok := os.LookupEnv(k)
if !ok {
continue
}
l.Set(key, v)
}
return nil
}