-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoptions.go
109 lines (93 loc) · 3.2 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package flagsfiller
import "github.com/iancoleman/strcase"
// Renamer takes a field's name and returns the flag name to be used
type Renamer func(name string) string
// DefaultFieldRenamer is used when no WithFieldRenamer option is passed to the FlagSetFiller
// constructor.
var DefaultFieldRenamer = KebabRenamer()
// FillerOption instances are passed to the FlagSetFiller constructor.
type FillerOption func(opt *fillerOptions)
type fillerOptions struct {
fieldRenamer []Renamer
envRenamer []Renamer
noSetFromEnv bool
valueSplitPattern string
}
// WithFieldRenamer declares an option to customize the Renamer used to convert field names
// to flag names.
func WithFieldRenamer(renamer Renamer) FillerOption {
return func(opt *fillerOptions) {
opt.fieldRenamer = append(opt.fieldRenamer, renamer)
}
}
// WithEnv activates pre-setting the flag values from environment variables.
// Fields are mapped to environment variables names by prepending the given prefix and
// converting word-wise to SCREAMING_SNAKE_CASE. The given prefix can be empty.
func WithEnv(prefix string) FillerOption {
return WithEnvRenamer(
CompositeRenamer(PrefixRenamer(prefix), ScreamingSnakeRenamer()))
}
// WithEnvRenamer activates pre-setting the flag values from environment variables where fields
// are mapped to environment variable names by applying the given Renamer
func WithEnvRenamer(renamer Renamer) FillerOption {
return func(opt *fillerOptions) {
opt.envRenamer = append(opt.envRenamer, renamer)
}
}
// NoSetFromEnv ignores setting values from the environment.
// All environment variable renamers are run but values are not set from the environment.
// This is good to use if you need to build a flag set with default values that don't consider the current environment.
func NoSetFromEnv() FillerOption {
return func(opt *fillerOptions) {
opt.noSetFromEnv = true
}
}
// WithValueSplitPattern allows for changing the default value splitting regex pattern from newlines and commas.
// Any empty string can be provided for pattern to disable value splitting.
func WithValueSplitPattern(pattern string) FillerOption {
return func(opt *fillerOptions) {
opt.valueSplitPattern = pattern
}
}
func (o *fillerOptions) renameLongName(name string) string {
if len(o.fieldRenamer) == 0 {
return DefaultFieldRenamer(name)
} else {
for _, renamer := range o.fieldRenamer {
name = renamer(name)
}
return name
}
}
func newFillerOptions(options ...FillerOption) *fillerOptions {
v := &fillerOptions{
valueSplitPattern: "[\n,]",
}
for _, opt := range options {
opt(v)
}
return v
}
// PrefixRenamer prepends the given prefix to a name
func PrefixRenamer(prefix string) Renamer {
return func(name string) string {
return prefix + name
}
}
// KebabRenamer converts a given name into kebab-case
func KebabRenamer() Renamer {
return strcase.ToKebab
}
// ScreamingSnakeRenamer converts a given name into SCREAMING_SNAKE_CASE
func ScreamingSnakeRenamer() Renamer {
return strcase.ToScreamingSnake
}
// CompositeRenamer applies all of the given Renamers to a name
func CompositeRenamer(renamers ...Renamer) Renamer {
return func(name string) string {
for _, r := range renamers {
name = r(name)
}
return name
}
}