-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
55 lines (45 loc) · 1.16 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
package pivot
import (
"io/ioutil"
"github.com/fatih/structs"
"github.com/ghodss/yaml"
)
type Configuration struct {
Backend string `json:"backend"`
Indexer string `json:"indexer"`
Environments map[string]Configuration `json:"environments"`
}
func LoadConfigFile(path string) (Configuration, error) {
config := Configuration{}
if data, err := ioutil.ReadFile(path); err == nil {
if err := yaml.Unmarshal(data, &config); err != nil {
return config, err
}
} else {
return config, err
}
return config, nil
}
func (self *Configuration) ForEnv(env string) Configuration {
config := *self
base := structs.New(&config)
if envConfig, ok := self.Environments[env]; ok {
specific := structs.New(envConfig)
// merge the environment-specific values over top of the general ones
for _, field := range base.Fields() {
if field.IsExported() {
switch field.Name() {
case `Environment`:
continue
default:
if specificField, ok := specific.FieldOk(field.Name()); ok {
if !specificField.IsZero() {
field.Set(specificField.Value())
}
}
}
}
}
}
return config
}