forked from jzelinskie/cobrautil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
must.go
365 lines (327 loc) · 11.1 KB
/
must.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package cobrautil
import (
"net"
"os"
"time"
"github.com/spf13/cobra"
)
// MustGetStringExpanded returns the string value of a flag with the given name,
// calls os.Expand on it, and panics if that flag was never defined.
func MustGetStringExpanded(cmd *cobra.Command, name string) string {
return os.ExpandEnv(MustGetString(cmd, name))
}
// MustGetBool returns the bool value of a flag with the given name and panics
// if that flag was never defined.
func MustGetBool(cmd *cobra.Command, name string) bool {
value, err := cmd.Flags().GetBool(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetBoolSlice returns the []bool value of a flag with the given name and
// panics if that flag was never defined.
func MustGetBoolSlice(cmd *cobra.Command, name string) []bool {
value, err := cmd.Flags().GetBoolSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetBytesBase64 returns the []byte value of a flag with the given name and
// panics if that flag was never defined.
func MustGetBytesBase64(cmd *cobra.Command, name string) []byte {
value, err := cmd.Flags().GetBytesBase64(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetBytesHex returns the []byte value of a flag with the given name and
// panics if that flag was never defined.
func MustGetBytesHex(cmd *cobra.Command, name string) []byte {
value, err := cmd.Flags().GetBytesHex(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetCount returns the int value of a flag with the given name and panics
// if that flag was never defined.
func MustGetCount(cmd *cobra.Command, name string) int {
value, err := cmd.Flags().GetCount(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetDuration returns the time.Duration of a flag with the given name and
// panics if that flag was never defined.
func MustGetDuration(cmd *cobra.Command, name string) time.Duration {
value, err := cmd.Flags().GetDuration(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetDurationSlice returns the []time.Duration of a flag with the given
// name and panics if that flag was never defined.
func MustGetDurationSlice(cmd *cobra.Command, name string) []time.Duration {
value, err := cmd.Flags().GetDurationSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetFloat32 returns the float32 value of a flag with the given name and
// panics if that flag was never defined.
func MustGetFloat32(cmd *cobra.Command, name string) float32 {
value, err := cmd.Flags().GetFloat32(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetFloat32Slice returns the []float32 value of a flag with the given name
// and panics if that flag was never defined.
func MustGetFloat32Slice(cmd *cobra.Command, name string) []float32 {
value, err := cmd.Flags().GetFloat32Slice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetFloat64 returns the float64 value of a flag with the given name and
// panics if that flag was never defined.
func MustGetFloat64(cmd *cobra.Command, name string) float64 {
value, err := cmd.Flags().GetFloat64(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetFloat64Slice returns the []float64 value of a flag with the given name
// and panics if that flag was never defined.
func MustGetFloat64Slice(cmd *cobra.Command, name string) []float64 {
value, err := cmd.Flags().GetFloat64Slice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetIP returns the net.IP value of a flag with the given name and panics
// if that flag was never defined.
func MustGetIP(cmd *cobra.Command, name string) net.IP {
value, err := cmd.Flags().GetIP(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetIPNet returns the net.IPNet value of a flag with the given name and
// panics if that flag was never defined.
func MustGetIPNet(cmd *cobra.Command, name string) net.IPNet {
value, err := cmd.Flags().GetIPNet(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetIPSlice returns the []net.IP value of a flag with the given name and
// panics if that flag was never defined.
func MustGetIPSlice(cmd *cobra.Command, name string) []net.IP {
value, err := cmd.Flags().GetIPSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetIPv4Mask returns the net.IPMask value of a flag with the given name
// and panics if that flag was never defined.
func MustGetIPv4Mask(cmd *cobra.Command, name string) net.IPMask {
value, err := cmd.Flags().GetIPv4Mask(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt returns the int value of a flag with the given name and panics if
// that flag was never defined.
func MustGetInt(cmd *cobra.Command, name string) int {
value, err := cmd.Flags().GetInt(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt16 returns the int16 value of a flag with the given name and panics if
// that flag was never defined.
func MustGetInt16(cmd *cobra.Command, name string) int16 {
value, err := cmd.Flags().GetInt16(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt32 returns the int32 value of a flag with the given name and panics if
// that flag was never defined.
func MustGetInt32(cmd *cobra.Command, name string) int32 {
value, err := cmd.Flags().GetInt32(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt32Slice returns the []int32 value of a flag with the given name and panics if
// that flag was never defined.
func MustGetInt32Slice(cmd *cobra.Command, name string) []int32 {
value, err := cmd.Flags().GetInt32Slice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt64 returns the int64 value of a flag with the given name and panics
// if that flag was never defined.
func MustGetInt64(cmd *cobra.Command, name string) int64 {
value, err := cmd.Flags().GetInt64(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt64Slice returns the []int64 value of a flag with the given name and panics if
// that flag was never defined.
func MustGetInt64Slice(cmd *cobra.Command, name string) []int64 {
value, err := cmd.Flags().GetInt64Slice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetInt8 returns the int8 value of a flag with the given name and panics
// if that flag was never defined.
func MustGetInt8(cmd *cobra.Command, name string) int8 {
value, err := cmd.Flags().GetInt8(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetIntSlice returns the []int value of a flag with the given name and
// panics if that flag was never defined.
func MustGetIntSlice(cmd *cobra.Command, name string) []int {
value, err := cmd.Flags().GetIntSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetString returns the string value of a flag with the given name and
// panics if that flag was never defined.
func MustGetString(cmd *cobra.Command, name string) string {
value, err := cmd.Flags().GetString(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetStringSlice returns the []string value of a flag with the given name
// and panics if that flag was never defined.
func MustGetStringSlice(cmd *cobra.Command, name string) []string {
value, err := cmd.Flags().GetStringSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetStringSlice returns the []string value of a flag with the given name,
// calls os.ExpandEnv on values, and panics if that flag was never defined.
func MustGetStringSliceExpanded(cmd *cobra.Command, name string) []string {
slice := MustGetStringSlice(cmd, name)
for i, str := range slice {
slice[i] = os.ExpandEnv(str)
}
return slice
}
// MustGetStringToInt returns the map[string]int value of a flag with the given
// name and panics if that flag was never defined.
func MustGetStringToInt(cmd *cobra.Command, name string) map[string]int {
value, err := cmd.Flags().GetStringToInt(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetStringToInt64 returns the map[string]int64 value of a flag with the
// given name and panics if that flag was never defined.
func MustGetStringToInt64(cmd *cobra.Command, name string) map[string]int64 {
value, err := cmd.Flags().GetStringToInt64(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetStringToString returns the map[string]string value of a flag with the
// given name and panics if that flag was never defined.
func MustGetStringToString(cmd *cobra.Command, name string) map[string]string {
value, err := cmd.Flags().GetStringToString(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUint returns the uint value of a flag with the given name and panics
// if that flag was never defined.
func MustGetUint(cmd *cobra.Command, name string) uint {
value, err := cmd.Flags().GetUint(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUint16 returns the uint16 value of a flag with the given name and
// panics if that flag was never defined.
func MustGetUint16(cmd *cobra.Command, name string) uint16 {
value, err := cmd.Flags().GetUint16(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUint32 returns the uint32 value of a flag with the given name and
// panics if that flag was never defined.
func MustGetUint32(cmd *cobra.Command, name string) uint32 {
value, err := cmd.Flags().GetUint32(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUint64 returns the uint64 value of a flag with the given name and
// panics if that flag was never defined.
func MustGetUint64(cmd *cobra.Command, name string) uint64 {
value, err := cmd.Flags().GetUint64(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUint8 returns the uint8 value of a flag with the given name and panics
// if that flag was never defined.
func MustGetUint8(cmd *cobra.Command, name string) uint8 {
value, err := cmd.Flags().GetUint8(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}
// MustGetUintSlice returns the []uint value of a flag with the given name and
// panics if that flag was never defined.
func MustGetUintSlice(cmd *cobra.Command, name string) []uint {
value, err := cmd.Flags().GetUintSlice(name)
if err != nil {
panic("failed to find cobra flag: " + name)
}
return value
}