-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.go
207 lines (165 loc) · 3.73 KB
/
cfg.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
package config
import (
"time"
"github.com/spf13/viper"
)
type myCfg struct {
v *viper.Viper
options map[CfgOption]bool
}
type newBuilderT struct {
cfg *myCfg
}
type configNameT struct {
cfg *myCfg
}
type configTypeT struct {
cfg *myCfg
}
type defaultsT struct {
cfg *myCfg
}
type configPathsT struct {
cfg *myCfg
}
type optionsT struct {
cfg *myCfg
}
// CfgOption provides a type for config options which can be passed using Options()
type CfgOption string
const (
// NoErrorOnMissingCfgFileOption defines that there should be no error upon creation of Cfg if none of config files could be found.
NoErrorOnMissingCfgFileOption CfgOption = "NoErrorOnMissingCfgFileOption"
)
// NewBuilder creates a new config builder
func NewBuilder() *newBuilderT {
return &newBuilderT{
cfg: &myCfg{
v: viper.New(),
options: make(map[CfgOption]bool),
},
}
}
func (t *newBuilderT) ConfigName(name string) *configNameT {
t.cfg.v.SetConfigName(name)
return &configNameT{
cfg: t.cfg,
}
}
func (t *configNameT) ConfigType(typ string) *configTypeT {
t.cfg.v.SetConfigType(typ)
return &configTypeT{
cfg: t.cfg,
}
}
func configPaths(cfg *myCfg, paths ...string) *configPathsT {
for _, p := range paths {
cfg.v.AddConfigPath(p)
}
return &configPathsT{
cfg: cfg,
}
}
func (t *configNameT) ConfigPaths(path ...string) *configPathsT {
return configPaths(t.cfg, path...)
}
func (t *configTypeT) ConfigPaths(path ...string) *configPathsT {
return configPaths(t.cfg, path...)
}
func defaults(c *myCfg, defs map[string]interface{}) *defaultsT {
for k, v := range defs {
c.v.SetDefault(string(k), v)
}
return &defaultsT{
cfg: c,
}
}
func (t *configPathsT) Defaults(defs map[string]interface{}) *defaultsT {
return defaults(t.cfg, defs)
}
func (t *configPathsT) NoDefaults() *defaultsT {
return &defaultsT{
cfg: t.cfg,
}
}
func (t *defaultsT) Options(options ...CfgOption) *optionsT {
for _, i := range options {
t.cfg.options[i] = true
}
return &optionsT{
cfg: t.cfg,
}
}
func (c *myCfg) HasOption(option CfgOption) bool {
if _, ok := c.options[option]; ok {
return true
}
return false
}
func (t *optionsT) Build() (Cfg, error) {
err := t.cfg.v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if !t.cfg.HasOption(NoErrorOnMissingCfgFileOption) {
return nil, err
}
} else {
return nil, err
}
}
return t.cfg, nil
}
func (c *myCfg) GetInt(key string) int {
return c.v.GetInt(key)
}
func (c *myCfg) GetInt32(key string) int32 {
return c.v.GetInt32(key)
}
func (c *myCfg) GetInt64(key string) int64 {
return c.v.GetInt64(key)
}
func (c *myCfg) GetUint(key string) uint {
return c.v.GetUint(key)
}
func (c *myCfg) GetUint32(key string) uint32 {
return c.v.GetUint32(key)
}
func (c *myCfg) GetUint64(key string) uint64 {
return c.v.GetUint64(key)
}
func (c *myCfg) GetString(key string) string {
return c.v.GetString(key)
}
func (c *myCfg) GetBool(key string) bool {
return c.v.GetBool(key)
}
func (c *myCfg) Get(key string) interface{} {
return c.v.Get(key)
}
func (c *myCfg) GetFloat64(key string) float64 {
return c.v.GetFloat64(key)
}
func (c *myCfg) GetAll() map[string]interface{} {
return c.v.AllSettings()
}
func (c *myCfg) GetDuration(key string) time.Duration {
return c.v.GetDuration(key)
}
func (c *myCfg) GetIntSlice(key string) []int {
return c.v.GetIntSlice(key)
}
func (c *myCfg) GetSizeInBytes(key string) uint {
return c.v.GetSizeInBytes(key)
}
func (c *myCfg) GetTime(key string) time.Time {
return c.v.GetTime(key)
}
func (c *myCfg) GetStringSlice(key string) []string {
return c.v.GetStringSlice(key)
}
func (c *myCfg) Set(key string, val interface{}) {
c.v.Set(key, val)
}
func (c *myCfg) Write() error {
return c.v.WriteConfig()
}