-
Notifications
You must be signed in to change notification settings - Fork 0
/
expandoptions.go
206 lines (180 loc) · 6.95 KB
/
expandoptions.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
/*
Copyright © 2022 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package config
type expandOptions struct {
defaultValue any
expressions bool
externalFuncMaps bool
funcMaps map[string]func(*Config, string, bool) (string, error)
initialValue any
lookupTables []map[string]string
nodecode bool
rawstring bool
trimPrefix bool
trimSpace bool
}
// ExpandOptions control the way configuration options undergo string
// expansion through the underlying [ExpandString] functions.
// ExpandOptions can be passed to any of the normal lookup functions
// that are provided to override [viper] versions, such as [GetString].
//
// e.g.
//
// s := config.GetString("config.value", ExternalLookups(false), LookupTable(configMap), Prefix("myconf", myFunc))
type ExpandOptions func(*expandOptions)
var defaultFuncMaps = map[string]func(*Config, string, bool) (string, error){
"http": fetchURL,
"https": fetchURL,
"file": fetchFile,
}
func evalExpandOptions(c *Config, options ...ExpandOptions) (e *expandOptions) {
e = &expandOptions{
funcMaps: map[string]func(*Config, string, bool) (string, error){},
externalFuncMaps: true,
trimSpace: true,
}
for _, opt := range c.defaultExpandOptions {
opt(e)
}
for _, opt := range options {
opt(e)
}
if e.externalFuncMaps {
for k, v := range e.funcMaps {
defaultFuncMaps[k] = v
}
e.funcMaps = defaultFuncMaps
}
if e.expressions {
e.funcMaps["expr"] = expr
}
if e.defaultValue == nil {
e.defaultValue = ""
}
return
}
// DefaultExpandOptions sets defaults to all subsequent calls to
// functions that perform configuration expansion. These defaults can be
// reset by calling DefaultExpandOptions with no arguments.
func (c *Config) DefaultExpandOptions(options ...ExpandOptions) {
c.defaultExpandOptions = options
}
// NoExpand overrides all other options except Default and returns the
// value (or the default) as-is with no expansion applied. This is to
// allow the normal functions and methods to be called but to receive
// the underlying configuration item, such as an encoded password.
func NoExpand() ExpandOptions {
return func(e *expandOptions) {
e.rawstring = true
}
}
// NoDecode disables the expansion of encoded values.
func NoDecode(n bool) ExpandOptions {
return func(e *expandOptions) {
e.nodecode = n
}
}
// LookupTable adds a lookup map to the Expand functions. If there are
// no maps defined then `${item}` is looked up as an environment
// variable. When string expansion is done to a plain word, ie. without
// a prefix, then `${item}` is looked up in each map, in the order the
// LookupTable options are given, and first match, if any, wins. If
// there is no match in any of the lookup maps then a nil value is
// returned and the environment variables are not checked.
func LookupTable(values map[string]string) ExpandOptions {
return func(e *expandOptions) {
e.lookupTables = append(e.lookupTables, values)
}
}
// LookupTables sets the expansion lookup tables to the slice of maps
// passed as values. Any existing lookup tables are discarded.
func LookupTables(values []map[string]string) ExpandOptions {
return func(e *expandOptions) {
e.lookupTables = values
}
}
// Prefix defines a custom mapping for the given prefix to an
// expand-like function. The prefix should not include the terminating
// ":". If the configuration prefix matches during expansion then the
// function is called with the config data and the contents of the
// expansion including the prefix (for URLs) but stripped of the opening
// `${` and closing `}`
func Prefix(prefix string, fn func(*Config, string, bool) (string, error)) ExpandOptions {
return func(e *expandOptions) {
e.funcMaps[prefix] = fn
}
}
// ExternalLookups enables or disables the built-in expansion options
// that fetch data from outside the program, such as URLs and file
// paths. The default is true.
func ExternalLookups(yes bool) ExpandOptions {
return func(e *expandOptions) {
e.externalFuncMaps = yes
}
}
// Expressions enables or disables the built-in expansion for
// expressions via the `github.com/maja42/goval` package. The default is
// false.
func Expressions(yes bool) ExpandOptions {
return func(e *expandOptions) {
e.expressions = yes
}
}
// TrimPrefix enables the removal of the prefix from the string passed
// to expansion functions. If this is not set then URLs can be passed
// as-is since the prefix is part of the URL. If set then URLs would
// need the schema explicitly added after the prefix. Using this option
// allows standard function like [strings.ToUpper] to be used without
// additional wrappers.
func TrimPrefix() ExpandOptions {
return func(e *expandOptions) {
e.trimPrefix = true
}
}
// TrimSpace enables the removal of leading and trailing spaces on all
// values in an expansion. The default is `true`. If a default
// value is given using the Default() then this is never trimmed.
func TrimSpace(yes bool) ExpandOptions {
return func(e *expandOptions) {
e.trimSpace = yes
}
}
// Default sets a default value to be returned if the resulting
// expansion of the whole config value is empty (after any optional
// trimming of leading and trailing spaces). This includes cases where
// external lookups fail or a configuration item is not found. If
// TrimSpace is false and the returned value consists wholly of
// whitespace then this is returned and not the default given here.
func Default(value any) ExpandOptions {
return func(e *expandOptions) {
e.defaultValue = value
}
}
// Initial sets an initial default value to be used if the configuration
// item is empty (or nil) to start. This differs from Default() which
// supplies a value to use if the value if empty after expansion. The
// initial value, if used, is expanded as would any configuration value.
//
// If config.NoExpand() is also used then this initial value is used as a
// secondary default - i.e. if config.Default() is empty.
func Initial(value any) ExpandOptions {
return func(e *expandOptions) {
e.initialValue = value
}
}