forked from caicloud/nirvana
/
config.go
554 lines (516 loc) · 15.1 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
Copyright 2018 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"os"
"reflect"
"sort"
"strings"
"time"
"github.com/caicloud/nirvana"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/errors"
"github.com/caicloud/nirvana/utils/printer"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// CustomOption must be a pointer to struct.
//
// Here is an example:
// type Option struct {
// FirstName string `desc:"Desc for First Name"`
// Age uint16 `desc:"Desc for Age"`
// }
// The struct has two fields (with prefix example):
// Field Flag ENV Key (In config file)
// FirstName --example-first-name EXAMPLE_FIRST_NAME example.firstName
// Age --example-age EXAMPLE_AGE example.age
// When you execute command with `--help`, you can see the help doc of flags and
// descriptions (From field tag `desc`).
//
// The priority is:
// Flag > ENV > Key > The value you set in option
type CustomOption interface{}
// Plugin is for plugins to collect configurations
type Plugin interface {
// Name returns plugin name.
Name() string
// Configure configures nirvana config via current options.
Configure(cfg *nirvana.Config) error
}
// Option contains basic configurations of nirvana.
type Option struct {
// IP is the IP to listen.
IP string `desc:"Nirvana server listening IP"`
// Port is the port to listen.
Port uint16 `desc:"Nirvana server listening Port"`
// Key is private key for HTTPS.
Key string `desc:"TLS private key (PEM format) for HTTPS"`
// Cert is certificate for HTTPS.
Cert string `desc:"TLS certificate (PEM format) for HTTPS"`
}
// NewDefaultOption creates a default option.
func NewDefaultOption() *Option {
return &Option{
IP: "",
Port: 8080,
}
}
// Name returns plugin name.
func (p *Option) Name() string {
return ""
}
// Configure configures nirvana config via current option.
func (p *Option) Configure(cfg *nirvana.Config) error {
cfg.Configure(
nirvana.IP(p.IP),
nirvana.Port(p.Port),
nirvana.TLS(p.Key, p.Cert),
)
return nil
}
// NirvanaCommand is a nirvana command.
type NirvanaCommand interface {
// EnablePlugin enables plugins.
EnablePlugin(plugins ...Plugin) NirvanaCommand
// AddOption will fill up options from flags/ENV/config after executing.
// A non-empty prefix is recommended. It's used to divide option namespaces.
AddOption(prefix string, options ...CustomOption) NirvanaCommand
// Add adds a field by key.
// If you don't have any struct to describe an option, you can use the method to
// add a single field into nirvana command.
// `pointer` must be a pointer to golang basic data type (e.g. *int, *string).
// `key` must a config key. It's like 'nirvana.ip' and 'myconfig.name.firstName'.
// The key will be converted to flag and env (e.g. --nirvana-ip and NIRVANA_IP).
// If you want a short flag for the field, you can only set a one-char string.
// `desc` describes the field.
Add(pointer interface{}, key string, shortFlag string, desc string) NirvanaCommand
// Execute runs nirvana server.
Execute(descriptors ...definition.Descriptor) error
// ExecuteWithConfig runs nirvana server from a custom config.
ExecuteWithConfig(cfg *nirvana.Config) error
// Command returns a command for command.
Command(cfg *nirvana.Config) *cobra.Command
// SetHook sets nirvana command hook.
SetHook(hook NirvanaCommandHook)
// Hook returns nirvana command hook.
Hook() NirvanaCommandHook
}
// NewDefaultNirvanaCommand creates a nirvana command with default option.
func NewDefaultNirvanaCommand() NirvanaCommand {
return NewNirvanaCommand(nil)
}
// NewNirvanaCommand creates a nirvana command. Nil option means default option.
func NewNirvanaCommand(option *Option) NirvanaCommand {
return NewNamedNirvanaCommand("server", option)
}
// NewNamedNirvanaCommand creates a nirvana command with an unique name.
// Empty name means `server`. Nil option means default option.
func NewNamedNirvanaCommand(name string, option *Option) NirvanaCommand {
if name == "" {
name = "server"
}
if option == nil {
option = NewDefaultOption()
}
cmd := &command{
name: name,
option: option,
plugins: []Plugin{},
fields: map[string]*configField{},
hook: &NirvanaCommandHookFunc{},
}
cmd.EnablePlugin(cmd.option)
return cmd
}
type configField struct {
pointer interface{}
desired interface{}
key string
env string
shortFlag string
longFlag string
description string
}
type command struct {
name string
option *Option
plugins []Plugin
fields map[string]*configField
hook NirvanaCommandHook
}
// EnablePlugin enables plugins.
func (s *command) EnablePlugin(plugins ...Plugin) NirvanaCommand {
s.plugins = append(s.plugins, plugins...)
for _, plugin := range plugins {
s.AddOption(plugin.Name(), plugin)
}
return s
}
func walkthrough(index []int, typ reflect.Type, f func(index []int, field reflect.StructField)) {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if field.Anonymous {
walkthrough(append(index, i), field.Type, f)
} else {
if field.Name != "" && field.Name[0] >= 'A' && field.Name[0] <= 'Z' {
f(append(index, i), field)
}
}
}
}
// AddOption will fill up options from config/ENV/flags after executing.
func (s *command) AddOption(prefix string, options ...CustomOption) NirvanaCommand {
if prefix != "" {
prefix += "."
}
for _, opt := range options {
val := reflect.ValueOf(opt)
typ := val.Type()
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
panic(errors.InternalServerError.Error("${type} is not a pointer to struct", typ.String()))
}
if val.IsNil() {
panic(errors.InternalServerError.Error("${type} should not be nil", typ.String()))
}
val = val.Elem()
typ = val.Type()
walkthrough([]int{}, typ, func(index []int, field reflect.StructField) {
ptr := val.FieldByIndex(index).Addr().Interface()
s.Add(ptr, prefix+field.Name, "", field.Tag.Get("desc"))
})
}
return s
}
// Add adds a field by key.
func (s *command) Add(pointer interface{}, key string, shortFlag string, desc string) NirvanaCommand {
if pointer == nil || reflect.ValueOf(pointer).IsNil() {
panic(errors.InternalServerError.Error("pointer of ${key} should not be nil", key))
}
keyParts := splitKey(key)
cf := &configField{
pointer: pointer,
shortFlag: shortFlag,
description: desc,
}
for i, parts := range keyParts {
if i > 0 {
cf.longFlag += "-"
}
cf.longFlag += strings.Join(parts, "-")
}
cf.longFlag = strings.ToLower(cf.longFlag)
nameParts := splitKey(s.name)
nameParts = append(nameParts, keyParts...)
for i, parts := range nameParts {
if i > 0 {
cf.key += "."
}
for j, part := range parts {
if j == 0 {
part = strings.ToLower(part)
}
cf.key += part
}
}
for i, parts := range nameParts {
if i > 0 {
cf.env += "_"
}
cf.env += strings.Join(parts, "_")
}
cf.env = strings.ToUpper(cf.env)
if _, ok := s.fields[cf.key]; ok {
panic(errors.InternalServerError.Error("${key} has been registered", cf.key))
}
s.fields[cf.key] = cf
return s
}
// Execute runs nirvana server.
func (s *command) Execute(descriptors ...definition.Descriptor) error {
cfg := nirvana.NewDefaultConfig()
if len(descriptors) > 0 {
cfg.Configure(nirvana.Descriptor(descriptors...))
}
return s.ExecuteWithConfig(cfg)
}
// ExecuteWithConfig runs nirvana server from a custom config.
func (s *command) ExecuteWithConfig(cfg *nirvana.Config) error {
return s.Command(cfg).Execute()
}
// Command returns a command for nirvana.
func (s *command) Command(cfg *nirvana.Config) *cobra.Command {
cmd := &cobra.Command{
Use: s.name,
Short: "Starting a nirvana server to handle http requests",
Run: func(cmd *cobra.Command, args []string) {
fs := cmd.Flags()
// Restore configs.
for _, f := range s.fields {
val := reflect.ValueOf(f.pointer).Elem()
if f.desired != nil && !fs.Lookup(f.longFlag).Changed {
val.Set(reflect.ValueOf(f.desired))
}
Set(f.key, val.Interface())
}
if err := s.hook.PreConfigure(cfg); err != nil {
cfg.Logger().Fatal(err)
}
for _, plugin := range s.plugins {
if err := plugin.Configure(cfg); err != nil {
cfg.Logger().Fatalf("Failed to install plugin %s: %s", plugin.Name(), err.Error())
}
}
if err := s.hook.PostConfigure(cfg); err != nil {
cfg.Logger().Fatal(err)
}
server := nirvana.NewServer(cfg)
if err := s.hook.PreServe(cfg, server); err != nil {
cfg.Logger().Fatal(err)
}
cfg.Logger().Infof("Listening on %s:%d", cfg.IP(), cfg.Port())
if err := s.hook.PostServe(cfg, server, server.Serve()); err != nil {
cfg.Logger().Fatal(err)
}
},
}
s.registerFields(cmd.Flags())
p := printer.NewTable(30)
rows := make([][]interface{}, 0, len(s.fields))
for _, f := range s.fields {
rows = append(rows, []interface{}{0, f.key, f.env, "--" + f.longFlag, f.desired})
}
sort.Slice(rows, func(i, j int) bool {
return rows[i][1].(string) < rows[j][1].(string)
})
p.AddRow("", "Config Key", "ENV", "Flag", "Current Value")
for i, row := range rows {
row[0] = i + 1
p.AddRow(row...)
}
cmd.Long = "ConfigKey-ENV-Flag Mapping Table\n\n" + p.String()
return cmd
}
func (s *command) registerFields(fs *pflag.FlagSet) {
for _, f := range s.fields {
var value, envValue interface{} = nil, nil
env := os.Getenv(f.env)
switch v := f.pointer.(type) {
case *uint8:
if IsSet(f.key) {
value = GetUint8(f.key)
}
if env != "" {
envValue = cast.ToUint8(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Uint8VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *uint16:
if IsSet(f.key) {
value = GetUint16(f.key)
}
if env != "" {
envValue = cast.ToUint16(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Uint16VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *uint32:
if IsSet(f.key) {
value = GetUint32(f.key)
}
if env != "" {
envValue = cast.ToUint32(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Uint32VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *uint64:
if IsSet(f.key) {
value = GetUint64(f.key)
}
if env != "" {
envValue = cast.ToUint64(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Uint64VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *uint:
if IsSet(f.key) {
value = GetUint(f.key)
}
if env != "" {
envValue = cast.ToUint(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.UintVarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *int8:
if IsSet(f.key) {
value = GetInt8(f.key)
}
if env != "" {
envValue = cast.ToInt8(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Int8VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *int16:
if IsSet(f.key) {
value = GetInt16(f.key)
}
if env != "" {
envValue = cast.ToInt16(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Int16VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *int32:
if IsSet(f.key) {
value = GetInt32(f.key)
}
if env != "" {
envValue = cast.ToInt32(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Int32VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *int64:
if IsSet(f.key) {
value = GetInt64(f.key)
}
if env != "" {
envValue = cast.ToInt64(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Int64VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *int:
if IsSet(f.key) {
value = GetInt(f.key)
}
if env != "" {
envValue = cast.ToInt(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.IntVarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *float32:
if IsSet(f.key) {
value = GetFloat32(f.key)
}
if env != "" {
envValue = cast.ToFloat32(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Float32VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *float64:
if IsSet(f.key) {
value = GetFloat64(f.key)
}
if env != "" {
envValue = cast.ToFloat64(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.Float64VarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *string:
if IsSet(f.key) {
value = GetString(f.key)
}
if env != "" {
envValue = cast.ToString(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.StringVarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *[]string:
if IsSet(f.key) {
value = GetStringSlice(f.key)
}
if env != "" {
envValue = cast.ToStringSlice(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.StringSliceVarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *bool:
if IsSet(f.key) {
value = GetBool(f.key)
}
if env != "" {
envValue = cast.ToBool(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.BoolVarP(v, f.longFlag, f.shortFlag, *v, f.description)
case *time.Duration:
if IsSet(f.key) {
value = GetDuration(f.key)
}
if env != "" {
envValue = cast.ToDuration(env)
}
f.desired = chooseValue(value, envValue, *v)
fs.DurationVarP(v, f.longFlag, f.shortFlag, *v, f.description)
default:
panic(errors.InternalServerError.Error("unrecognized type ${type} for ${key}", reflect.TypeOf(f.pointer).String(), f.key))
}
}
}
// splitKey splits key to parts.
func splitKey(key string) [][]string {
fieldParts := strings.Split(key, ".")
nameParts := make([][]string, len(fieldParts))
for index, field := range fieldParts {
parts := []string{}
lastIsCapital := true
lastIndex := 0
for i, char := range field {
if char >= '0' && char <= '9' {
// Numbers inherit last char.
continue
}
currentIsCapital := char >= 'A' && char <= 'Z'
if i > 0 && lastIsCapital != currentIsCapital {
end := 0
if currentIsCapital {
end = i
} else {
end = i - 1
}
if end > lastIndex {
parts = append(parts, field[lastIndex:end])
lastIndex = end
}
}
lastIsCapital = currentIsCapital
}
if lastIndex < len(field) {
parts = append(parts, field[lastIndex:])
}
nameParts[index] = parts
}
return nameParts
}
// chooseValue chooses expected value form multiple configurations.
// Priority: ENV > Config > Default
func chooseValue(cfgValue interface{}, envValue interface{}, defaultValue interface{}) interface{} {
val := defaultValue
if cfgValue != nil {
val = cfgValue
}
if envValue != nil {
val = envValue
}
return val
}
// SetHook sets nirvana command hook.
func (s *command) SetHook(hook NirvanaCommandHook) {
if hook == nil {
hook = &NirvanaCommandHookFunc{}
}
s.hook = hook
}
// Hook returns nirvana command hook.
func (s *command) Hook() NirvanaCommandHook {
return s.hook
}