-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
181 lines (154 loc) · 4.63 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
175
176
177
178
179
180
181
// Copyright 2016 struktur AG. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package phoenix
import (
conf "github.com/dlintw/goconf"
)
// Config provides read access to the application's configuration.
//
// GetXXXDefault methods return dflt if the named option in section has no
// value. Use HasOption to determine the status of an option thus defaulted.
type Config interface {
HasSection(section string) bool
GetSections() []string
GetOptions(section string) ([]string, error)
HasOption(section, option string) bool
GetBool(section, option string) (bool, error)
GetBoolDefault(section, option string, dflt bool) bool
GetInt(section, option string) (int, error)
GetIntDefault(section, option string, dflt int) int
GetFloat64(section, option string) (float64, error)
GetFloat64Default(section, option string, dflt float64) float64
GetString(section, option string) (string, error)
GetStringDefault(section, option, dflt string) string
}
// ConfigUpdater provides access to the applications's configuration and allows
// to update it.
//
// Update method takes a string mapping like [section][option]=value. Sections
// are automatically created as needed and existing values are overwritten.
type ConfigUpdater interface {
Config
Update(map[string]map[string]string) error
}
type config struct {
*conf.ConfigFile
path string
defaultPath string
overridePath string
Defaults, Overrides *conf.ConfigFile
}
func newConfig() *config {
return &config{
ConfigFile: conf.NewConfigFile(),
Defaults: conf.NewConfigFile(),
Overrides: conf.NewConfigFile(),
}
}
func (config *config) GetBoolDefault(section, option string, dflt bool) bool {
if value, err := config.GetBool(section, option); err == nil {
return value
}
return dflt
}
func (config *config) GetIntDefault(section, option string, dflt int) int {
if value, err := config.GetInt(section, option); err == nil {
return value
}
return dflt
}
func (config *config) GetFloat64Default(section, option string, dflt float64) float64 {
if value, err := config.GetFloat64(section, option); err == nil {
return value
}
return dflt
}
func (config *config) GetStringDefault(section, option, dflt string) string {
if value, err := config.GetString(section, option); err == nil {
return value
}
return dflt
}
func (config *config) HasPath() bool {
return config.path != ""
}
func (config *config) Path() string {
return config.path
}
func (config *config) SetPath(path string) {
config.path = path
}
func (config *config) HasDefaultPath() bool {
return config.defaultPath != ""
}
func (config *config) DefaultPath() string {
return config.defaultPath
}
func (config *config) SetDefaultPath(path string) {
config.defaultPath = path
}
func (config *config) HasOverridePath() bool {
return config.overridePath != ""
}
func (config *config) OverridePath() string {
return config.overridePath
}
func (config *config) SetOverridePath(path string) {
config.overridePath = path
}
func (config *config) DefaultOption(section, name, value string) {
config.Defaults.AddOption(section, name, value)
}
func (config *config) OverrideOption(section, name, value string) {
config.Overrides.AddOption(section, name, value)
}
func (config *config) Update(updates map[string]map[string]string) error {
for section, options := range updates {
for option, value := range options {
config.ConfigFile.AddOption(section, option, value)
}
}
return nil
}
func (config *config) load() (err error) {
if config.HasPath() {
config.ConfigFile, err = conf.ReadConfigFile(config.Path())
if err != nil {
return
}
} else {
config.ConfigFile = conf.NewConfigFile()
}
if config.HasDefaultPath() {
// Load defaults if a path was given.
config.Defaults, err = conf.ReadConfigFile(config.DefaultPath())
if err != nil {
return
}
}
if config.HasOverridePath() {
// Load overrides if a path was given.
config.Overrides, err = conf.ReadConfigFile(config.OverridePath())
if err != nil {
return
}
}
for _, section := range config.Defaults.GetSections() {
options, _ := config.Defaults.GetOptions(section)
for _, option := range options {
if !config.ConfigFile.HasOption(section, option) {
value, _ := config.Defaults.GetRawString(section, option)
config.ConfigFile.AddOption(section, option, value)
}
}
}
for _, section := range config.Overrides.GetSections() {
options, _ := config.Overrides.GetOptions(section)
for _, option := range options {
value, _ := config.Overrides.GetRawString(section, option)
config.ConfigFile.AddOption(section, option, value)
}
}
return
}