-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
174 lines (160 loc) · 4.38 KB
/
config.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
package main
import (
"github.com/BurntSushi/toml"
flag "github.com/getgauge/mflag"
"github.com/hashicorp/logutils"
"io/ioutil"
"log"
"os"
"runtime"
"strings"
"reflect"
"strconv"
)
var (
cfg config
logFilter *logutils.LevelFilter
logLevels = []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"}
)
const (
logMinLevel = logutils.LogLevel("INFO")
envDelimiter = "_"
)
type config struct {
sectionName string
data map[string]interface{}
}
func readConfig() {
var (
configFile string
showHelp, showVersion bool
)
logFilter = &logutils.LevelFilter{
Levels: logLevels,
MinLevel: logMinLevel,
Writer: os.Stderr,
}
log.SetOutput(logFilter)
flag.StringVar(&configFile, []string{"c", "-config"}, "/etc/logear/logear.conf", "config file")
flag.StringVar(&logFile, []string{"l", "-log"}, "", "log file")
flag.BoolVar(&showHelp, []string{"h", "-help"}, false, "display the help")
flag.BoolVar(&showVersion, []string{"v", "-version"}, false, "display version info")
flag.Parse()
if showHelp {
flag.Usage()
os.Exit(0)
}
if showVersion {
println(versionstring)
println("OS: " + runtime.GOOS)
println("Architecture: " + runtime.GOARCH)
os.Exit(0)
}
cfg.parseTomlFile(configFile)
startLogging()
log.Printf("%s started with pid %d", versionstring, os.Getpid())
}
func (c *config) parseTomlFile(filename string) {
if data, err := ioutil.ReadFile(filename); err != nil {
log.Fatal("[ERROR] Can't read config file ", filename, ", error: ", err)
} else {
if _, err := toml.Decode(string(data), &c.data); err != nil {
log.Fatal("[ERROR] Can't parse config file ", filename, ", error: ", err)
}
}
}
func (c *config) getSection(name string) (interface{}, bool) {
data, ok := c.data[name];
if !ok {
log.Printf("[WARNING] Don't found section named: %s", name)
return nil, false
}
data = overrideByEnv(data, strings.ToLower(progname + envDelimiter + name))
return data, true
}
func overrideByEnv(originalData interface{}, sectionName string) interface{} {
for _, env := range os.Environ() {
if strings.HasPrefix(env, sectionName) {
envNameValue := strings.SplitN(env, "=", 2)
envNameValue[0] = strings.TrimPrefix(envNameValue[0], sectionName + envDelimiter)
switch reflect.TypeOf(originalData).Kind() {
case reflect.Map:
data := originalData.(map[string]interface{})
applyProcess(envNameValue, data)
originalData = data
case reflect.Slice:
data := originalData.([]map[string]interface{})
items := strings.SplitN(envNameValue[0], envDelimiter, 2)
if index, err := strconv.Atoi(items[0]); err == nil {
envNameValue[0] = items[1]
lastIndex := len(data) - 1
if index > lastIndex {
newData := make(map[string]interface{})
applyProcess(envNameValue, newData)
data = append(data, newData)
} else {
applyProcess(envNameValue, data[index])
}
}
originalData = data
}
}
}
return originalData
}
func applyProcess(env []string, data map[string]interface{}) {
var propName string
var index int
var isArray bool = false
items := strings.Split(env[0], envDelimiter)
if len(items) > 1 {
if i, err := strconv.Atoi(items[len(items)-1]); err == nil {
propName = strings.Join(items[:len(items)-1], envDelimiter)
index = i
isArray = true
} else {
propName = env[0]
}
} else {
propName = env[0]
}
if _, ok := data[propName]; ok {
switch reflect.TypeOf(data[propName]).Kind() {
case reflect.Slice, reflect.Array:
lastIndex := len(data[propName].([]interface{})) - 1
if index > lastIndex {
data[propName] = append(data[propName].([]interface{}), env[1])
} else {
data[propName].([]interface{})[index] = apply(data[propName].([]interface{})[index], env[1])
}
default:
data[propName] = apply(data[propName], env[1])
}
} else if isArray {
data[propName] = append([]interface{}{}, env[1])
} else {
data[propName] = env[1]
}
}
func apply(cfgVal interface{}, envVal string) interface{} {
switch reflect.TypeOf(cfgVal).Kind() {
case reflect.String:
return envVal
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, err := strconv.ParseInt(envVal, 10, 64)
if err == nil {
return val
}
case reflect.Float32, reflect.Float64:
val, err := strconv.ParseFloat(envVal, 64)
if err == nil {
return val
}
case reflect.Bool:
val, err := strconv.ParseBool(envVal)
if err == nil {
return val
}
}
return cfgVal
}