-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
292 lines (261 loc) · 6.79 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"unsafe"
"github.com/0xrawsec/golang-utils/log"
)
// Config : configuration structure definition
type Config map[string]Value
// Value : stored in the configuration
type Value interface{}
var (
ErrNoSuchKey = errors.New("No such key")
)
//////////////////////////////// Utils /////////////////////////////////////////
func configErrorf(fmt string, i ...interface{}) {
log.Errorf(fmt, i...)
os.Exit(1)
}
func getRequiredError(key, ofType string, err error) {
configErrorf("Cannot get mandatory parameter %s as %s: %s ", key, ofType, err)
}
////////////////////////////////////////////////////////////////////////////////
// Loads : loads a configuration structure from a data buffer
// @data : buffer containing the configuration object
// return (Config, error) : the Config struct filled from data, error code
func Loads(data []byte) (c Config, err error) {
err = json.Unmarshal(data, &c)
if err != nil {
return
}
return
}
// Load : loads a configuration structure from a file
// @path : path where the configuration is stored as a json file
// return (Config, error) : the Config struct parsed, error code
func Load(path string) (c Config, err error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return
}
return Loads([]byte(data))
}
// Dumps : Dumps Config structure into a byte slice
// return ([]byte, error) : byte slice and error code
func (c *Config) Dumps() (dump []byte, err error) {
dump, err = json.Marshal(c)
if err != nil {
return
}
return
}
// Debug : prints out the configuration in debug information
func (c *Config) Debug() {
for key, val := range *c {
log.Debugf("config[%s] = %v", key, val)
}
}
// Get : get the Value associated to a key found in Config structure
// return (Value, error) : Value associated to key and error code
func (c *Config) Get(key string) (Value, error) {
val, ok := (*c)[key]
if !ok {
return val, ErrNoSuchKey
}
return val, nil
}
// GetString gets the value associated to a key as string
// return (string, error)
func (c *Config) GetString(key string) (string, error) {
val, ok := (*c)[key]
if !ok {
return "", ErrNoSuchKey
}
s, ok := val.(string)
if !ok {
return s, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, s)
}
return val.(string), nil
}
// GetInt64 gets the value associated to a key as int64
// return (int64, error)
func (c *Config) GetInt64(key string) (i int64, err error) {
val, ok := (*c)[key]
if !ok {
return 0, ErrNoSuchKey
}
switch val.(type) {
case int8:
return int64(val.(int8)), nil
case int16:
return int64(val.(int16)), nil
case int:
return int64(val.(int)), nil
case int32:
return int64(val.(int32)), nil
case int64:
return val.(int64), nil
case float64:
// json loads float64 so handle that case
return int64(val.(float64)), nil
default:
return 0, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, i)
}
}
// GetUint64 gets the value associated to a key as uint64
// return (uint64, error)
func (c *Config) GetUint64(key string) (u uint64, err error) {
val, ok := (*c)[key]
if !ok {
return 0, ErrNoSuchKey
}
switch val.(type) {
case uint8:
return uint64(val.(uint8)), nil
case uint16:
return uint64(val.(uint16)), nil
case uint32:
return uint64(val.(uint32)), nil
case uint:
return uint64(val.(uint)), nil
case uint64:
return val.(uint64), nil
case float64:
// json loads float64 so handle that case
return uint64(val.(float64)), nil
default:
return 0, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, u)
}
}
// GetSubConfig : get a subconfig referenced by key
// return (Config, error)
func (c *Config) GetSubConfig(key string) (Config, error) {
val, err := c.Get(key)
if err != nil {
return Config{}, err
}
sc, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, sc)
}
return *(*Config)(unsafe.Pointer(&(sc))), nil
}
// GetRequiredSubConfig : get a subconfig referenced by key
// return (Config)
func (c *Config) GetRequiredSubConfig(key string) Config {
sc, err := c.GetSubConfig(key)
if err != nil {
getRequiredError(key, "map[string]interface{}", err)
}
return sc
}
// GetRequired : get the Value associated to a key found in Config structure and exit if
// not available
// return (Value) : Value associated to key if it exists
func (c *Config) GetRequired(key string) Value {
val, err := c.Get(key)
if err != nil {
configErrorf("Configuration parameter %s is mandatory", key)
}
return val
}
func (c *Config) GetRequiredString(key string) string {
s, err := c.GetString(key)
if err != nil {
getRequiredError(key, "string", err)
}
return s
}
func (c *Config) GetRequiredInt64(key string) int64 {
val, err := c.GetInt64(key)
if err != nil {
getRequiredError(key, "int64", err)
}
return val
}
func (c *Config) GetRequiredUint64(key string) uint64 {
val, err := c.GetUint64(key)
if err != nil {
getRequiredError(key, "uint64", err)
}
return val
}
func (c *Config) GetStringSlice(key string) (s []string, err error) {
s = make([]string, 0)
val, err := c.Get(key)
if err != nil {
return
}
ival, ok := val.([]interface{})
if !ok {
return s, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, []interface{}{})
}
for _, e := range ival {
s = append(s, e.(string))
}
return
}
func (c *Config) GetRequiredStringSlice(key string) []string {
ss, err := c.GetStringSlice(key)
if err != nil {
getRequiredError(key, "[]string", err)
}
return ss
}
func (c *Config) GetUint64Slice(key string) (u []uint64, err error) {
u = make([]uint64, 0)
val, err := c.Get(key)
if err != nil {
return
}
ival, ok := val.([]interface{})
if !ok {
return u, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, []interface{}{})
}
for _, e := range ival {
u = append(u, e.(uint64))
}
return
}
func (c *Config) GetRequiredUint64Slice(key string) []uint64 {
val, err := c.GetUint64Slice(key)
if err != nil {
getRequiredError(key, "[]uint64", err)
}
return val
}
func (c *Config) GetInt64Slice(key string) (i []int64, err error) {
i = make([]int64, 0)
val, err := c.Get(key)
if err != nil {
return
}
ival, ok := val.([]interface{})
if !ok {
return i, fmt.Errorf("Wrong type for %s (Type:%T Expecting:%T)", key, val, []interface{}{})
}
for _, e := range ival {
i = append(i, e.(int64))
}
return
}
func (c *Config) GetRequiredInt64Slice(key string) []int64 {
val, err := c.GetInt64Slice(key)
if err != nil {
getRequiredError(key, "[]int64", err)
}
return val
}
// Set : set parameter identified by key of the Config struct with a Value
func (c *Config) Set(key string, value interface{}) {
(*c)[key] = value
}
// HasKey returns true if the configuration has the given key
func (c *Config) HasKey(key string) bool {
_, ok := (*c)[key]
return ok
}