-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
62 lines (54 loc) · 1.07 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
package env
import (
"fmt"
"os"
)
var envvars []*envvar
var prefix string
type envvar struct {
name string
dest *string
def string
desc string
}
func (e *envvar) read() {
value := os.Getenv(prefix + e.name)
if value == "" {
value = e.def
}
*e.dest = value
}
func Var(dest *string, name string, def string, desc string) {
v := &envvar{
name: name,
dest: dest,
def: def,
desc: desc,
}
envvars = append(envvars, v)
}
func Parse(p string, allowArgs bool) {
if prefix = p; prefix != "" {
prefix = prefix + "_"
}
if len(os.Args) > 1 && allowArgs == false {
printHelpAndExit()
}
for _, e := range envvars {
e.read()
}
}
func printHelpAndExit() {
fmt.Printf("'%s' needs to be configured via environment variables and does not take any arguments.\n", os.Args[0])
fmt.Println("The following environment variables are read ('_nil_' implies an empty string):\n")
for _, e := range envvars {
def := e.def
if def == "" {
def = "_nil_"
} else {
def = "'" + def + "'"
}
fmt.Printf("\t%s%s=%s: %s\n", prefix, e.name, def, e.desc)
}
os.Exit(1)
}