forked from tkanos/gonfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gonfig.go
222 lines (186 loc) · 5.59 KB
/
gonfig.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Package gonfig implements simple configuration reading
// from both YAML files and environment variables.
package gonfig
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"log"
"github.com/ghodss/yaml"
)
// tag name to override the field name of an environment variable
const envTagName = "env"
const argTagName = "arg"
const defaultTagName = "default"
// GetConf aggregates all the YAML and environment variable values
// and puts them into the passed interface.
func GetConf(configuration interface{}) (err error) {
GetConfByFilename(getProgramName()+".yaml", configuration)
return
}
// GetConfByFilename aggregates all the YAML and environment variable values
// and puts them into the passed interface.
func GetConfByFilename(filename string, configuration interface{}) (err error) {
configValue := reflect.ValueOf(configuration)
if typ := configValue.Type(); typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
return fmt.Errorf("configuration should be a pointer to a struct type")
}
setDefaults(configuration)
getFromYAML(filename, configuration)
getFromArguments(configuration)
getFromEnvVariables(configuration)
return
}
func getProgramName() string {
splitArg := strings.Split(os.Args[0], "\\")
if len(splitArg) <= 1 {
splitArg = strings.Split(os.Args[0], "/")
}
return strings.TrimSuffix(splitArg[len(splitArg)-1], ".exe")
}
func setDefaults(configuration interface{}) (err error) {
getFromEnvVariablesOrArguments(defaultTagName, getFromDefault, configuration)
return
}
func getFromYAML(filename string, configuration interface{}) (err error) {
if len(filename) == 0 {
return
}
file, err := os.Open(filename)
if err != nil {
log.Println("Could not open file : " + filename + " skipping reading config from YAML.")
return
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
log.Println("Could not read from file : " + filename + " skipping reading config from YAML.")
return
}
err = yaml.Unmarshal(data, &configuration)
if err != nil {
log.Println("Could not unmarschal from file : " + filename + " skipping extracting config from YAML.")
return
}
return
}
func getFromArguments(configuration interface{}) {
getFromEnvVariablesOrArguments(argTagName, getFromArg, configuration)
}
func getFromEnvVariables(configuration interface{}) {
getFromEnvVariablesOrArguments(envTagName, getFromEnv, configuration)
}
type getData func(reflect.StructField, string) string
func getFromDefault(p reflect.StructField, key string) string {
tagContent := p.Tag.Get(defaultTagName)
if len(tagContent) > 0 {
return tagContent
}
return ""
}
func getFromEnv(p reflect.StructField, key string) string {
return os.Getenv(key)
}
func getFromArg(p reflect.StructField, key string) string {
for i := range os.Args {
if strings.HasPrefix(os.Args[i], ("--" + key + "=")) {
return os.Args[i][len(key)+3:]
} else if os.Args[i] == ("--" + key) {
if len(os.Args) > i+1 {
if strings.HasPrefix(os.Args[i+1], "-") {
return "true"
}
return os.Args[i+1]
}
return "true"
}
}
return ""
}
func getFromEnvVariablesOrArguments(tagName string, fnGetData getData, configuration interface{}) {
typ := reflect.TypeOf(configuration)
// if a pointer to a struct is passed, get the type of the dereferenced object
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
// check if we've got a field name override for the environment
tagContent := p.Tag.Get(tagName)
value := ""
if len(tagContent) > 0 {
value = fnGetData(p, tagContent)
} else {
value = fnGetData(p, p.Name)
}
if !p.Anonymous && len(value) > 0 {
// struct
s := reflect.ValueOf(configuration).Elem()
if s.Kind() == reflect.Struct {
// exported field
f := s.FieldByName(p.Name)
if f.IsValid() && f.CanSet() {
// A Value can be changed only if it is
// addressable and was not obtained by
// the use of unexported struct fields.
// change value
kind := f.Kind()
if kind == reflect.Int || kind == reflect.Int64 {
setStringToInt(f, value, 64)
} else if kind == reflect.Int32 {
setStringToInt(f, value, 32)
} else if kind == reflect.Int16 {
setStringToInt(f, value, 16)
} else if kind == reflect.Uint || kind == reflect.Uint64 {
setStringToUInt(f, value, 64)
} else if kind == reflect.Uint32 {
setStringToUInt(f, value, 32)
} else if kind == reflect.Uint16 {
setStringToUInt(f, value, 16)
} else if kind == reflect.Bool {
setStringToBool(f, value)
} else if kind == reflect.Float64 {
setStringToFloat(f, value, 64)
} else if kind == reflect.Float32 {
setStringToFloat(f, value, 32)
} else if kind == reflect.String {
f.SetString(value)
}
}
}
}
}
}
func setStringToInt(f reflect.Value, value string, bitSize int) {
convertedValue, err := strconv.ParseInt(value, 10, bitSize)
if err == nil {
if !f.OverflowInt(convertedValue) {
f.SetInt(convertedValue)
}
}
}
func setStringToUInt(f reflect.Value, value string, bitSize int) {
convertedValue, err := strconv.ParseUint(value, 10, bitSize)
if err == nil {
if !f.OverflowUint(convertedValue) {
f.SetUint(convertedValue)
}
}
}
func setStringToBool(f reflect.Value, value string) {
convertedValue, err := strconv.ParseBool(value)
if err == nil {
f.SetBool(convertedValue)
}
}
func setStringToFloat(f reflect.Value, value string, bitSize int) {
convertedValue, err := strconv.ParseFloat(value, bitSize)
if err == nil {
if !f.OverflowFloat(convertedValue) {
f.SetFloat(convertedValue)
}
}
}