-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvtoflag.go
66 lines (57 loc) · 1.5 KB
/
envtoflag.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
package envtoflag
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
// Prase() parses the environment variables based on the following rules and
// sets the flags
// - "prefix" is prefixed before the variable name
// - dots are converted to underscores
// - dashes are converted to underscores
// - resulting variable name is converted to uppercase
func Parse(prefix string) {
if err := parse(prefix); err != nil {
log.Fatalln(err)
}
}
// ParseAll() does the same functionality as Prase()
// except that it omits the "prefix" string
func ParseAll() {
if err := parse(""); err != nil {
log.Fatalln(err)
}
}
// parse() iterates through the flags and populate the
// values from environment variables if present
func parse(prefix string) (errp error) {
cmdline := make(map[string]bool)
flag.Visit(func(f *flag.Flag) {
cmdline[f.Name] = true
})
flag.VisitAll(func(f *flag.Flag) {
if _, ok := cmdline[f.Name]; !ok {
val := os.Getenv(getEnvName(prefix, f.Name))
if val != "" {
err := f.Value.Set(val)
if err != nil {
if errp != nil {
errp = fmt.Errorf(errp.Error()+", value:\"%s\" for flag:\"%s\"", val, f.Name)
} else {
errp = fmt.Errorf(":envtoflag: error setting value:\"%s\" for flag:\"%s\"", val, f.Name)
}
}
}
}
})
return
}
// getEnvName() takes the
func getEnvName(prefix string, flag string) string {
name := strings.ToUpper(prefix + "_" + flag)
name = strings.Replace(name, ".", "_", -1)
name = strings.Replace(name, "-", "_", -1)
return name
}