forked from koding/multiconfig
/
env.go
116 lines (95 loc) · 2.92 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
package multiconfig
import (
"fmt"
"os"
"reflect"
"strings"
"github.com/fatih/camelcase"
"github.com/fatih/structs"
)
// EnvironmentLoader satisifies the loader interface. It loads the
// configuration from the environment variables in the form of
// STRUCTNAME_FIELDNAME.
type EnvironmentLoader struct {
// Prefix prepends given string to every environment variable
// {STRUCTNAME}_FIELDNAME will be {PREFIX}_FIELDNAME
Prefix string
// CamelCase adds a seperator for field names in camelcase form. A
// fieldname of "AccessKey" would generate a environment name of
// "STRUCTNAME_ACCESSKEY". If CamelCase is enabled, the environment name
// will be generated in the form of "STRUCTNAME_ACCESS_KEY"
CamelCase bool
}
func (e *EnvironmentLoader) getPrefix(s *structs.Struct) string {
if e.Prefix != "" {
return e.Prefix
}
return s.Name()
}
// Load loads the source into the config defined by struct s
func (e *EnvironmentLoader) Load(s interface{}) error {
strct := structs.New(s)
prefix := e.getPrefix(strct)
for _, field := range strct.Fields() {
if err := e.processField(prefix, field); err != nil {
return err
}
}
return nil
}
// processField gets leading name for the env variable and combines the current
// field's name and generates environemnt variable names recursively
func (e *EnvironmentLoader) processField(prefix string, field *structs.Field) error {
fieldName := e.generateFieldName(prefix, field)
switch {
case field.Kind() == reflect.Struct && !implementsTextUnmarshaler(field):
for _, f := range field.Fields() {
if err := e.processField(fieldName, f); err != nil {
return err
}
}
case field.Kind() == reflect.Ptr:
field.InitElem()
return e.processField(prefix, field)
default:
v := os.Getenv(fieldName)
if v == "" {
return nil
}
if err := fieldSet(field, v); err != nil {
return err
}
}
return nil
}
// PrintEnvs prints the generated environment variables to the std out.
func (e *EnvironmentLoader) PrintEnvs(s interface{}) {
strct := structs.New(s)
prefix := e.getPrefix(strct)
for _, field := range strct.Fields() {
e.printField(prefix, field)
}
}
// printField prints the field of the config struct for the flag.Usage
func (e *EnvironmentLoader) printField(prefix string, field *structs.Field) {
fieldName := e.generateFieldName(prefix, field)
if field.IsExported() {
switch {
case field.Kind() == reflect.Struct && !implementsTextUnmarshaler(field):
for _, f := range field.Fields() {
e.printField(fieldName, f)
}
default:
fmt.Println(" ", fieldName)
}
}
}
// generateFieldName generates the fiels name combined with the prefix and the
// struct's field name
func (e *EnvironmentLoader) generateFieldName(prefix string, field *structs.Field) string {
fieldName := strings.ToUpper(field.Name())
if e.CamelCase {
fieldName = strings.ToUpper(strings.Join(camelcase.Split(field.Name()), "_"))
}
return strings.ToUpper(prefix) + "_" + fieldName
}