-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
114 lines (93 loc) · 2.15 KB
/
options.go
File metadata and controls
114 lines (93 loc) · 2.15 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
108
109
110
111
112
113
114
package config
type config struct {
filenames []string
envPrefix string
varPrefix string
omitDefaults bool
omitEnvVars bool
ignoreMissingFiles bool
tagName string
autoFormatFieldName bool
defaultOverrides map[string]string
}
func (c *config) Copy() *config {
return &config{
filenames: c.filenames,
envPrefix: c.envPrefix,
varPrefix: c.varPrefix,
omitDefaults: c.omitDefaults,
omitEnvVars: c.omitEnvVars,
ignoreMissingFiles: c.ignoreMissingFiles,
tagName: c.tagName,
autoFormatFieldName: c.autoFormatFieldName,
defaultOverrides: c.defaultOverrides,
}
}
func (c *config) With(opts ...Option) *config {
for _, opt := range opts {
opt(c)
}
return c
}
func defaultOptions() *config {
return &config{
filenames: []string{},
tagName: "config",
ignoreMissingFiles: true,
autoFormatFieldName: true,
defaultOverrides: make(map[string]string),
}
}
type Option func(*config)
func WithFilename(filename string) Option {
return func(c *config) {
c.filenames = append(c.filenames, filename)
}
}
func WithFilenames(filenames ...string) Option {
return func(c *config) {
c.filenames = append(c.filenames, filenames...)
}
}
func WithEnvPrefix(prefix string) Option {
return func(c *config) {
c.envPrefix = prefix + "_"
}
}
func WithVarPrefix(prefix string) Option {
return func(c *config) {
c.varPrefix = prefix + "_"
}
}
func WithPrefix(prefix string) Option {
return func(c *config) {
WithEnvPrefix(prefix)(c)
WithVarPrefix(prefix)(c)
}
}
func OmitDefaults(c *config) {
c.omitDefaults = true
}
func OmitEnvVars(c *config) {
c.omitEnvVars = true
}
func WithTagName(name string) Option {
return func(c *config) {
c.tagName = name
}
}
func WithDefaultOverride(key string, value string) Option {
return func(c *config) {
c.defaultOverrides[key] = value
}
}
func WithIgnoreMissingFiles(shouldIgnore bool) Option {
return func(c *config) {
c.ignoreMissingFiles = shouldIgnore
}
}
func WithAutoFormatFieldName(shouldAutoFormat bool) Option {
return func(c *config) {
c.autoFormatFieldName = shouldAutoFormat
}
}