forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqa.go
More file actions
403 lines (337 loc) · 10 KB
/
qa.go
File metadata and controls
403 lines (337 loc) · 10 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package qa
import (
"fmt"
"strconv"
"strings"
"github.com/scaleway/scaleway-cli/v2/core"
)
func LintCommands(commands *core.Commands) []error {
errors := []error(nil)
errors = append(errors, testShortEndWithDotError(commands)...)
errors = append(errors, testShortIsNotPresentError(commands)...)
errors = append(errors, testWellKnownArgAtTheEndError(commands)...)
errors = append(errors, testArgMustUseDashError(commands)...)
errors = append(errors, testPositionalArgMustBeRequiredError(commands)...)
errors = append(errors, testExampleCanHaveOnlyOneTypeOfExampleError(commands)...)
errors = append(errors, testDifferentLocalizationForNamespaceError(commands)...)
errors = append(errors, testDuplicatedCommandError(commands)...)
errors = append(errors, testAtLeastOneExampleIsPresentError(commands)...)
errors = append(errors, testArgSpecInvalidError(commands)...)
errors = append(errors, testArgSpecMissingError(commands)...)
errors = append(errors, testCommandInvalidJSONExampleError(commands)...)
errors = append(errors, testCommandInvalidSeeAlsoError(commands)...)
errors = append(errors, testAtLeastOneSeeAlsoIsPresentError(commands)...)
errors = filterIgnore(errors)
return errors
}
type ShortMustNotEndWithDotError struct {
Command *core.Command
}
func (err ShortMustNotEndWithDotError) Error() string {
return "short must not end with '.' for command '" + err.Command.GetCommandLine("scw") + "'"
}
func testShortEndWithDotError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
if strings.HasSuffix(command.Short, ".") {
errors = append(errors, &ShortMustNotEndWithDotError{
Command: command,
})
}
}
return errors
}
type ShortMustBePresentError struct {
Command *core.Command
}
func (err ShortMustBePresentError) Error() string {
return "short must be present for command '" + err.Command.GetCommandLine("scw") + "'"
}
func testShortIsNotPresentError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
if command.Short == "" {
errors = append(errors, &ShortMustBePresentError{
Command: command,
})
}
}
return errors
}
type WellKnownArgOrderError struct {
Command *core.Command
Argspec *core.ArgSpec
}
func (err WellKnownArgOrderError) Error() string {
return "well-known arg order must be respected '" + err.Command.GetCommandLine(
"scw",
) + "', arg '" + err.Argspec.Name + "'"
}
type WellKnownArgAtTheEndError struct {
Command *core.Command
Argspec *core.ArgSpec
}
func (err WellKnownArgAtTheEndError) Error() string {
return "well-known arg must be at the end'" + err.Command.GetCommandLine(
"scw",
) + "', arg '" + err.Argspec.Name + "'"
}
const (
wellKnownArgOrganizationID = "organization-id"
wellKnownArgOrganization = "organization"
wellKnownArgRegion = "region"
wellKnownArgZone = "zone"
)
var (
wellKnownArgs = map[string]struct{}{
wellKnownArgOrganizationID: {},
wellKnownArgOrganization: {},
wellKnownArgRegion: {},
wellKnownArgZone: {},
}
wellKnownArgsOrder = []string{
wellKnownArgOrganizationID,
wellKnownArgOrganization,
wellKnownArgRegion,
wellKnownArgZone,
}
)
func testWellKnownArgAtTheEndError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
wkaCounter := 0
wkaNotAtTheEnd := false
lastWKA := (*core.ArgSpec)(nil)
for argPosition, argspec := range command.ArgSpecs {
if _, ok := wellKnownArgs[argspec.Name]; ok {
respectOrder := false
for ; wkaCounter < len(wellKnownArgsOrder); wkaCounter++ {
if argspec.Name == wellKnownArgsOrder[wkaCounter] {
respectOrder = true
wkaCounter++ // next well-known arg can't be the same
break
}
}
if !respectOrder {
errors = append(errors, &WellKnownArgOrderError{
Command: command,
Argspec: argspec,
})
}
wkaNotAtTheEnd = argPosition+1 != len(command.ArgSpecs)
lastWKA = argspec
}
}
if wkaNotAtTheEnd {
errors = append(errors, &WellKnownArgAtTheEndError{
Command: command,
Argspec: lastWKA,
})
}
}
return errors
}
type ArgMustUseDashError struct {
Command *core.Command
Argspec *core.ArgSpec
}
func (err ArgMustUseDashError) Error() string {
return "arg must use dash for command '" + err.Command.GetCommandLine(
"scw",
) + "', arg '" + err.Argspec.Name + "'"
}
func testArgMustUseDashError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
for _, argspec := range command.ArgSpecs {
if strings.Contains(argspec.Name, "_") {
errors = append(errors, &ArgMustUseDashError{
Command: command,
Argspec: argspec,
})
}
}
}
return errors
}
type PositionalArgMustBeRequiredError struct {
Command *core.Command
Argspec *core.ArgSpec
}
func (err PositionalArgMustBeRequiredError) Error() string {
return "positional argument must be required '" + err.Command.GetCommandLine(
"scw",
) + "', arg '" + err.Argspec.Name + "'"
}
func testPositionalArgMustBeRequiredError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
for _, argSpec := range command.ArgSpecs {
if argSpec.Positional && !argSpec.Required {
errors = append(errors, &PositionalArgMustBeRequiredError{
Command: command,
Argspec: argSpec,
})
}
}
}
return errors
}
type ExampleCanHaveOnlyOneTypeOfExampleError struct {
Command *core.Command
ExampleIndex int
}
func (err ExampleCanHaveOnlyOneTypeOfExampleError) Error() string {
return "arg must use dash for command '" + err.Command.GetCommandLine(
"scw",
) + "', example #" + strconv.Itoa(
err.ExampleIndex,
)
}
func testExampleCanHaveOnlyOneTypeOfExampleError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
for i, example := range command.Examples {
if example.ArgsJSON != "" && example.Raw != "" {
errors = append(errors, &ExampleCanHaveOnlyOneTypeOfExampleError{
Command: command,
ExampleIndex: i,
})
}
}
}
return errors
}
type DifferentLocalizationForNamespaceError struct {
Command1 *core.Command
Command2 *core.Command
Checks []bool
ArgNames1 []string
ArgNames2 []string
}
func (err DifferentLocalizationForNamespaceError) Error() string {
return fmt.Sprintf(
"different localization for commands '%v', '%v': %v, %v",
err.Command1.GetCommandLine(
"scw",
),
err.Command2.GetCommandLine("scw"),
err.ArgNames1,
err.ArgNames2,
)
}
func testDifferentLocalizationForNamespaceError(commands *core.Commands) []error {
errors := []error(nil)
for i, command1 := range commands.GetAll() {
for j, command2 := range commands.GetAll() {
if i >= j {
continue
}
samePathLength := strings.Count(
command1.GetCommandLine("scw"),
" ",
) == strings.Count(
command2.GetCommandLine("scw"),
" ",
)
sameNamespace := command1.Namespace == command2.Namespace
c1HasRegionOnly := command1.ArgSpecs.GetByName("region") != nil &&
command1.ArgSpecs.GetByName("zone") == nil
c2HasRegionOnly := command2.ArgSpecs.GetByName("region") != nil &&
command2.ArgSpecs.GetByName("zone") == nil
c1HasZoneOnly := command1.ArgSpecs.GetByName("region") == nil &&
command1.ArgSpecs.GetByName("zone") != nil
c2HasZoneOnly := command2.ArgSpecs.GetByName("region") == nil &&
command2.ArgSpecs.GetByName("zone") != nil
c1NoRegionNoZone := command1.ArgSpecs.GetByName("region") == nil &&
command1.ArgSpecs.GetByName("zone") == nil
c2NoRegionNoZone := command2.ArgSpecs.GetByName("region") == nil &&
command2.ArgSpecs.GetByName("zone") == nil
if !samePathLength {
continue
}
if !sameNamespace {
continue
}
switch {
case c1HasRegionOnly && c2HasRegionOnly:
// do nothing
case c1HasZoneOnly && c2HasZoneOnly:
// do nothing
case c1NoRegionNoZone && c2NoRegionNoZone:
// do nothing
default:
argNames1 := []string(nil)
for _, argSpec := range command1.ArgSpecs {
if argSpec.Name == "zone" || argSpec.Name == "region" {
argNames1 = append(argNames1, argSpec.Name)
}
}
argNames2 := []string(nil)
for _, argSpec := range command2.ArgSpecs {
if argSpec.Name == "zone" || argSpec.Name == "region" {
argNames2 = append(argNames2, argSpec.Name)
}
}
errors = append(errors, &DifferentLocalizationForNamespaceError{
Command1: command1,
Command2: command2,
ArgNames1: argNames1,
ArgNames2: argNames2,
Checks: []bool{
sameNamespace,
c1HasRegionOnly,
c2HasRegionOnly,
c1HasZoneOnly,
c2HasZoneOnly,
c1NoRegionNoZone,
c2NoRegionNoZone,
},
})
}
}
}
return errors
}
type DuplicatedCommandError struct {
Command *core.Command
}
func (err DuplicatedCommandError) Error() string {
return fmt.Sprintf("duplicated command '%s'", err.Command.GetCommandLine("scw"))
}
// testDuplicatedCommandError testes that there is no duplicate command.
func testDuplicatedCommandError(commands *core.Commands) []error {
errors := []error(nil)
uniqueness := make(map[string]bool)
for _, command := range commands.GetAll() {
key := command.GetCommandLine("scw")
if uniqueness[key] {
errors = append(errors, &DuplicatedCommandError{Command: command})
continue
}
uniqueness[key] = true
}
return errors
}
type MissingExampleError struct {
Command *core.Command
}
func (err MissingExampleError) Error() string {
return fmt.Sprintf("command without examples '%s'", err.Command.GetCommandLine("scw"))
}
// testDuplicatedCommandError testes that there is no duplicate command.
func testAtLeastOneExampleIsPresentError(commands *core.Commands) []error {
errors := []error(nil)
for _, command := range commands.GetAll() {
// Namespace and resources commands do not need examples
// We focus on command with a verb
if command.Run == nil {
continue
}
if len(command.Examples) == 0 {
errors = append(errors, &MissingExampleError{Command: command})
continue
}
}
return errors
}