This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_cli_flags.go
229 lines (199 loc) · 4.74 KB
/
source_cli_flags.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package croconf
import (
"encoding"
"fmt"
"strconv"
"go.k6.io/croconf/flag"
)
type SourceCLI struct {
flags []string
parser *flag.Parser
fs *flag.Set
}
func NewSourceFromCLIFlags(flags []string) *SourceCLI {
return &SourceCLI{
flags: flags,
parser: flag.NewParser(),
}
}
func (sc *SourceCLI) Initialize() error {
fs, err := sc.parser.Parse(sc.flags)
if err != nil {
return err
}
sc.fs = fs
return nil
}
func (sc *SourceCLI) GetName() string {
return "CLI flags" // TODO
}
func (sc *SourceCLI) FromName(name string) *cliBinder {
return &cliBinder{source: sc, longhand: name}
}
func (sc *SourceCLI) FromNameAndShorthand(name, shorthand string) *cliBinder {
return &cliBinder{
source: sc,
longhand: name,
shorthand: shorthand,
}
}
func (sc *SourceCLI) FromPositionalArg(position int) *cliBinder {
return &cliBinder{source: sc, position: position}
}
type cliBinder struct {
source *SourceCLI
shorthand string
longhand string
position int
// lookupfn defines a custom lookup logic
lookupfn func() (string, error)
}
func (cb *cliBinder) boundName() string {
// TODO: improve?
if cb.position > 0 {
return fmt.Sprintf("argument #%d", cb.position)
}
if cb.shorthand != "" {
return fmt.Sprintf("--%s / -%s", cb.shorthand, cb.longhand)
}
return fmt.Sprintf("--%s", cb.longhand)
}
func (cb *cliBinder) newBinding(apply func() error) *cliBinding {
return &cliBinding{
binder: cb,
apply: apply,
}
}
// TODO: refactor
func (cb *cliBinder) lookup() (string, error) {
// custom lookup
if cb.lookupfn != nil {
return cb.lookupfn()
}
// default
if cb.position > 0 {
arg, ok := cb.source.fs.Positional(uint(cb.position))
if !ok {
return "", ErrorMissing
}
return arg, nil
}
opt, ok := cb.source.fs.Option(cb.longhand, cb.shorthand)
if !ok {
return opt, ErrorMissing
}
return opt, nil
}
// we can use this function to get the string representation of any simple
// binary value and parse it ourselves
func (cb *cliBinder) textValueHelper(callback func(string) error) Binding {
return cb.newBinding(func() error {
vtext, err := cb.lookup()
if err != nil {
return ErrorMissing
}
return callback(vtext)
})
}
func (cb *cliBinder) BindStringValueTo(dest *string) Binding {
return cb.textValueHelper(func(s string) error {
*dest = s
return nil
})
}
func (cb *cliBinder) BindTextBasedValueTo(dest encoding.TextUnmarshaler) Binding {
return cb.textValueHelper(func(s string) error {
return dest.UnmarshalText([]byte(s))
})
}
func (cb *cliBinder) BindIntValueTo(dest *int64) Binding {
return cb.newBinding(func() error {
v, err := cb.lookup()
if err != nil {
return NewBindFieldMissingError(cb.source.GetName(), cb.boundName())
}
val, bindErr := parseInt(v)
if bindErr != nil {
return bindErr.withFuncName("BindIntValue")
}
*dest = val
return nil
})
}
func (cb *cliBinder) BindUintValueTo(dest *uint64) Binding {
return cb.newBinding(func() error {
v, err := cb.lookup()
if err != nil {
return NewBindFieldMissingError(cb.source.GetName(), cb.boundName())
}
val, bindErr := parseUint(v)
if bindErr != nil {
return bindErr.withFuncName("BindIntValue")
}
*dest = val
return nil
})
}
func (cb *cliBinder) BindFloatValueTo(dest *float64) Binding {
return cb.newBinding(func() error {
v, err := cb.lookup()
if err != nil {
return NewBindFieldMissingError(cb.source.GetName(), cb.boundName())
}
val, bindErr := parseFloat(v)
if bindErr != nil {
return bindErr.withFuncName("BindIntValue")
}
*dest = val
return nil
})
}
func (cb *cliBinder) BindBoolValueTo(dest *bool) Binding {
cb.source.parser.RegisterUnary(cb.longhand, cb.shorthand)
return cb.textValueHelper(func(v string) error {
b, err := strconv.ParseBool(v)
if err != nil {
return err
}
*dest = b
return nil
})
}
func (cb *cliBinder) BindArrayValueTo(length *int, element *func(int) LazySingleValueBinder) Binding {
cb.source.parser.RegisterSlice(cb.longhand, cb.shorthand)
return cb.newBinding(func() error {
opts := cb.source.fs.Options(cb.longhand, cb.shorthand)
if len(opts) < 1 {
return ErrorMissing
}
*length = len(opts)
*element = func(i int) LazySingleValueBinder {
cbcopy := *cb
cbcopy.lookupfn = func() (string, error) {
if i >= len(opts) {
return "", fmt.Errorf("tried to access invalid element %s, array only has %d elements", cbcopy.longhand, i)
}
return opts[i], nil
}
return &cbcopy
}
return nil
})
}
type cliBinding struct {
binder *cliBinder
apply func() error
}
var _ interface {
Binding
BindingFromSource
} = &cliBinding{}
func (cb *cliBinding) Apply() error {
return cb.apply()
}
func (cb *cliBinding) Source() Source {
return cb.binder.source
}
func (cb *cliBinding) BoundName() string {
return cb.binder.boundName()
}