forked from jinzhu/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configor.go
192 lines (167 loc) · 4.86 KB
/
configor.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package configor
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"regexp"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// ENV will return environment
func ENV() string {
if env := os.Getenv("CONFIGOR_ENV"); env != "" {
return env
}
// return test when running go test
if isTest, _ := regexp.MatchString("/_test/", os.Args[0]); isTest {
return "test"
}
return "development"
}
func getConfigurationWithENV(file, env string) (string, error) {
var envFile string
var extname = path.Ext(file)
if extname == "" {
envFile = fmt.Sprintf("%v.%v", file, env)
} else {
envFile = fmt.Sprintf("%v.%v%v", strings.TrimSuffix(file, extname), env, extname)
}
if fileInfo, err := os.Stat(envFile); err == nil && fileInfo.Mode().IsRegular() {
return envFile, nil
}
return "", fmt.Errorf("failed to find file %v", file)
}
func getConfigurations(files ...string) []string {
var results []string
env := ENV()
for i := len(files) - 1; i >= 0; i-- {
var foundFile bool
var file = files[i]
// check configuration
if fileInfo, err := os.Stat(file); err == nil && fileInfo.Mode().IsRegular() {
foundFile = true
results = append(results, file)
}
// check env configuration
if file, err := getConfigurationWithENV(file, env); err == nil {
foundFile = true
results = append(results, file)
}
// check example configuration
if !foundFile {
if example, err := getConfigurationWithENV(file, "example"); err == nil {
fmt.Printf("Failed to find configuration %v, using example file %v\n", file, example)
results = append(results, example)
} else {
fmt.Printf("Failed to find configuration %v\n", file)
}
}
}
return results
}
func getPrefix(config interface{}) string {
if prefix := os.Getenv("CONFIGOR_ENV_PREFIX"); prefix != "" {
return prefix
}
return "configor"
}
// Load will unmarshal configurations to struct from files that you provide
func Load(config interface{}, files ...string) error {
for _, file := range getConfigurations(files...) {
if err := load(config, file); err != nil {
return err
}
}
if prefix := getPrefix(config); prefix == "-" {
return processTags(config)
} else {
return processTags(config, prefix)
}
}
func getPrefixForStruct(prefix []string, fieldStruct *reflect.StructField) []string {
if fieldStruct.Anonymous && fieldStruct.Tag.Get("anonymous") == "true" {
return prefix
}
return append(prefix, fieldStruct.Name)
}
func processTags(config interface{}, prefix ...string) error {
configValue := reflect.Indirect(reflect.ValueOf(config))
if configValue.Kind() != reflect.Struct {
return errors.New("invalid config, should be struct")
}
configType := configValue.Type()
for i := 0; i < configType.NumField(); i++ {
fieldStruct := configType.Field(i)
field := configValue.Field(i)
// read configuration from shell env
var envName = fieldStruct.Tag.Get("env")
if envName == "" {
envName = strings.ToUpper(strings.Join(append(prefix, fieldStruct.Name), "_"))
}
if envName != "" {
if value := os.Getenv(envName); value != "" {
if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
}
}
}
if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank {
// set default configuration if is blank
if value := fieldStruct.Tag.Get("default"); value != "" {
if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
}
} else if fieldStruct.Tag.Get("required") == "true" {
// set configuration has value if it is required
return errors.New(fieldStruct.Name + " is required, but blank")
}
}
for field.Kind() == reflect.Ptr {
field = field.Elem()
}
if field.Kind() == reflect.Struct {
if err := processTags(field.Addr().Interface(), getPrefixForStruct(prefix, &fieldStruct)...); err != nil {
return err
}
}
if field.Kind() == reflect.Slice {
var length = field.Len()
for i := 0; i < length; i++ {
if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
if err := processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefix, &fieldStruct), fmt.Sprintf("%d", i))...); err != nil {
return err
}
}
}
}
}
return nil
}
func load(config interface{}, file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
switch {
case strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml"):
return yaml.Unmarshal(data, config)
case strings.HasSuffix(file, ".toml"):
return toml.Unmarshal(data, config)
case strings.HasSuffix(file, ".json"):
return json.Unmarshal(data, config)
default:
if toml.Unmarshal(data, config) != nil {
if json.Unmarshal(data, config) != nil {
if yaml.Unmarshal(data, config) != nil {
return errors.New("failed to decode config")
}
}
}
return nil
}
}