-
Notifications
You must be signed in to change notification settings - Fork 50
/
options.go
93 lines (72 loc) · 1.8 KB
/
options.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
package cfg
import (
"flag"
"os"
"strings"
)
type Option func(cfg *config) error
type configFiles []string
func (c *configFiles) String() string {
return strings.Join(*c, ",")
}
func (c *configFiles) Set(value string) error {
*c = append(*c, value)
return nil
}
func WithConfigFile(filePath string, fileType string) Option {
return func(cfg *config) error {
return readConfigFromFile(cfg, filePath, fileType)
}
}
func WithConfigFileFlag(flagName string) Option {
return func(cfg *config) error {
flags := flag.NewFlagSet("cfg", flag.ContinueOnError)
var configFiles configFiles
flags.Var(&configFiles, flagName, "path to a config file")
err := flags.Parse(os.Args[1:])
if err != nil {
return err
}
for _, configFile := range configFiles {
err := readConfigFromFile(cfg, configFile, "yml")
if err != nil {
return err
}
}
return nil
}
}
func WithConfigMap(settings map[string]interface{}, mergeOptions ...MergeOption) Option {
return func(cfg *config) error {
return cfg.merge(".", settings, mergeOptions...)
}
}
func WithConfigSetting(key string, settings interface{}, mergeOptions ...MergeOption) Option {
return func(cfg *config) error {
return cfg.merge(key, settings, mergeOptions...)
}
}
func WithEnvKeyPrefix(prefix string) Option {
return func(cfg *config) error {
cfg.envKeyPrefix = prefix
return nil
}
}
func WithEnvKeyReplacer(replacer *strings.Replacer) Option {
return func(cfg *config) error {
cfg.envKeyReplacer = replacer
return nil
}
}
func WithErrorHandlers(handlers ...ErrorHandler) Option {
return func(cfg *config) error {
cfg.errorHandlers = handlers
return nil
}
}
func WithSanitizers(sanitizer ...Sanitizer) Option {
return func(cfg *config) error {
cfg.sanitizers = append(cfg.sanitizers, sanitizer...)
return nil
}
}