-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
107 lines (94 loc) · 2.87 KB
/
Copy pathoption.go
File metadata and controls
107 lines (94 loc) · 2.87 KB
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
package confx
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Option func(opts *initOptions)
// DefaultTagName is the default tag name for struct fields
var DefaultTagName = "confx"
// DefaultUsageTagName is the default tag name for field usage descriptions
var DefaultUsageTagName = "usage"
type initOptions struct {
flagSet *pflag.FlagSet
envPrefix string
tagName string
usageTagName string
viperInstance *viper.Viper
validator Validator
fieldHook func(f *Field) (*Field, error)
}
// WithFlagSet sets a custom pflag.FlagSet instance for parsing command line flags
// If not set, the default pflag.CommandLine is used.
func WithFlagSet(flagSet *pflag.FlagSet) Option {
if flagSet == nil {
panic("flagSet cannot be nil")
}
return func(opts *initOptions) {
opts.flagSet = flagSet
}
}
// WithEnvPrefix sets a custom environment variable prefix for reading configuration from environment variables.
// If not set, environment variables are read without a prefix.
func WithEnvPrefix(envPrefix string) Option {
if envPrefix == "" {
panic("envPrefix cannot be empty")
}
return func(opts *initOptions) {
opts.envPrefix = envPrefix
}
}
type Field struct {
ViperKey string
FlagKey string
EnvKey string
Usage string
}
// WithFieldHook sets a custom field hook function that maps configuration field names to Viper keys, flag names, environment variable names and usage strings.
func WithFieldHook(hook func(f *Field) (*Field, error)) Option {
if hook == nil {
panic("hook cannot be nil")
}
return func(opts *initOptions) {
opts.fieldHook = hook
}
}
// WithValidator sets a custom validator instance for validating the configuration struct.
// If not set, the default ValidatorWithSkipNestedUnless(validator.New(validator.WithRequiredStructEnabled())) is used.
func WithValidator(v Validator) Option {
if v == nil {
panic("validator cannot be nil")
}
return func(opts *initOptions) {
opts.validator = v
}
}
// WithViper sets a custom Viper instance for reading configuration from different sources.
// If not set, the default viper.GetViper() is used.
func WithViper(v *viper.Viper) Option {
if v == nil {
panic("viperInstance cannot be nil")
}
return func(opts *initOptions) {
opts.viperInstance = v
}
}
// WithTagName sets a custom struct tag name for reading configuration from struct fields.
// If not set, the default tag name is "confx".
func WithTagName(tagName string) Option {
if tagName == "" {
panic("tagName cannot be empty")
}
return func(opts *initOptions) {
opts.tagName = tagName
}
}
// WithUsageTagName sets a custom struct tag name for reading field usage descriptions.
// If not set, the default tag name is "usage".
func WithUsageTagName(usageTagName string) Option {
if usageTagName == "" {
panic("usageTagName cannot be empty")
}
return func(opts *initOptions) {
opts.usageTagName = usageTagName
}
}