@@ -2,9 +2,7 @@ package core
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"reflect"
7
- "sort"
8
6
"strings"
9
7
10
8
"github.com/scaleway/scaleway-cli/v2/core/human"
@@ -117,26 +115,6 @@ func (c *Command) Override(builder func(command *Command) *Command) {
117
115
* c = * builder (c )
118
116
}
119
117
120
- func (c * Command ) getPath () string {
121
- if c .path != "" {
122
- return c .path
123
- }
124
- path := []string (nil )
125
- if c .Namespace != "" {
126
- path = append (path , c .Namespace )
127
- }
128
- if c .Resource != "" {
129
- path = append (path , c .Resource )
130
- }
131
- if c .Verb != "" {
132
- path = append (path , c .Verb )
133
- }
134
-
135
- c .path = strings .Join (path , indexCommandSeparator )
136
-
137
- return c .path
138
- }
139
-
140
118
func (c * Command ) GetCommandLine (binaryName string ) string {
141
119
return strings .Trim (
142
120
binaryName + " " + strings .ReplaceAll (c .getPath (), indexCommandSeparator , " " ),
@@ -162,24 +140,6 @@ func (c *Command) GetUsage(binaryName string, commands *Commands) string {
162
140
return strings .Join (parts , " " )
163
141
}
164
142
165
- // seeAlsosAsStr returns all See Alsos as a single string
166
- func (c * Command ) seeAlsosAsStr () string {
167
- seeAlsos := make ([]string , 0 , len (c .SeeAlsos ))
168
-
169
- for _ , cmdSeeAlso := range c .SeeAlsos {
170
- short := " # " + cmdSeeAlso .Short
171
- commandStr := " " + cmdSeeAlso .Command
172
-
173
- seeAlsoLines := []string {
174
- short ,
175
- commandStr ,
176
- }
177
- seeAlsos = append (seeAlsos , strings .Join (seeAlsoLines , "\n " ))
178
- }
179
-
180
- return strings .Join (seeAlsos , "\n \n " )
181
- }
182
-
183
143
// AddInterceptors add one or multiple interceptors to a command.
184
144
// These new interceptors will be added after the already present interceptors (if any).
185
145
func (c * Command ) AddInterceptors (interceptors ... CommandInterceptor ) {
@@ -204,145 +164,27 @@ func (c *Command) MatchAlias(alias alias.Alias) bool {
204
164
return true
205
165
}
206
166
207
- // Commands represent a list of CLI commands, with a index to allow searching.
208
- type Commands struct {
209
- commands []* Command
210
- commandIndex map [string ]* Command
211
- }
212
-
213
- func NewCommands (cmds ... * Command ) * Commands {
214
- c := & Commands {
215
- commands : make ([]* Command , 0 , len (cmds )),
216
- commandIndex : make (map [string ]* Command , len (cmds )),
217
- }
218
-
219
- for _ , cmd := range cmds {
220
- c .Add (cmd )
221
- }
222
-
223
- return c
224
- }
225
-
226
- func NewCommandsMerge (cmdsList ... * Commands ) * Commands {
227
- cmdCount := 0
228
- for _ , cmds := range cmdsList {
229
- cmdCount += len (cmds .commands )
230
- }
231
- c := & Commands {
232
- commands : make ([]* Command , 0 , cmdCount ),
233
- commandIndex : make (map [string ]* Command , cmdCount ),
234
- }
235
- for _ , cmds := range cmdsList {
236
- for _ , cmd := range cmds .commands {
237
- c .Add (cmd )
238
- }
239
- }
240
-
241
- return c
242
- }
243
-
244
- func (c * Commands ) MustFind (path ... string ) * Command {
245
- cmd , exist := c .find (path ... )
246
- if exist {
247
- return cmd
248
- }
249
-
250
- panic (fmt .Errorf ("command %v not found" , strings .Join (path , " " )))
251
- }
252
-
253
- func (c * Commands ) Find (path ... string ) * Command {
254
- cmd , exist := c .find (path ... )
255
- if exist {
256
- return cmd
257
- }
258
-
259
- return nil
260
- }
261
-
262
- func (c * Commands ) Remove (namespace , verb string ) {
263
- for i := range c .commands {
264
- if c .commands [i ].Namespace == namespace && c .commands [i ].Verb == verb {
265
- c .commands = append (c .commands [:i ], c .commands [i + 1 :]... )
266
-
267
- return
268
- }
269
- }
270
- }
271
-
272
- func (c * Commands ) RemoveResource (namespace , resource string ) {
273
- for i := range c .commands {
274
- if c .commands [i ].Namespace == namespace && c .commands [i ].Resource == resource &&
275
- c .commands [i ].Verb == "" {
276
- c .commands = append (c .commands [:i ], c .commands [i + 1 :]... )
277
-
278
- return
279
- }
280
- }
281
- }
282
-
283
- func (c * Commands ) Add (cmd * Command ) {
284
- c .commands = append (c .commands , cmd )
285
- c .commandIndex [cmd .getPath ()] = cmd
286
- }
287
-
288
- func (c * Commands ) Merge (cmds * Commands ) {
289
- for _ , cmd := range cmds .commands {
290
- c .Add (cmd )
291
- }
292
- }
293
-
294
- func (c * Commands ) MergeAll (cmds ... * Commands ) {
295
- for _ , command := range cmds {
296
- c .Merge (command )
167
+ // Copy returns a copy of a command
168
+ func (c * Command ) Copy () * Command {
169
+ newCommand := * c
170
+ newCommand .Aliases = append ([]string (nil ), c .Aliases ... )
171
+ newCommand .Examples = make ([]* Example , len (c .Examples ))
172
+ for i := range c .Examples {
173
+ e := * c .Examples [i ]
174
+ newCommand .Examples [i ] = & e
297
175
}
298
- }
299
-
300
- func (c * Commands ) GetAll () []* Command {
301
- return c .commands
302
- }
303
-
304
- // find must take the command path, eg. find("instance","get","server")
305
- func (c * Commands ) find (path ... string ) (* Command , bool ) {
306
- cmd , exist := c .commandIndex [strings .Join (path , indexCommandSeparator )]
307
- if exist {
308
- return cmd , true
176
+ newCommand .SeeAlsos = make ([]* SeeAlso , len (c .SeeAlsos ))
177
+ for i := range c .SeeAlsos {
178
+ sa := * c .SeeAlsos [i ]
179
+ newCommand .SeeAlsos [i ] = & sa
309
180
}
310
181
311
- return nil , false
312
- }
313
-
314
- // GetSortedCommand returns a slice of commands sorted alphabetically
315
- func (c * Commands ) GetSortedCommand () []* Command {
316
- commands := make ([]* Command , len (c .commands ))
317
- copy (commands , c .commands )
318
- sort .Slice (commands , func (i , j int ) bool {
319
- return commands [i ].signature () < commands [j ].signature ()
320
- })
321
-
322
- return commands
182
+ return & newCommand
323
183
}
324
184
325
- func (c * Commands ) HasSubCommands (cmd * Command ) bool {
326
- if cmd .Namespace != "" && cmd .Resource != "" && cmd .Verb != "" {
327
- return false
328
- }
329
- if cmd .Namespace == "" && cmd .Resource == "" && cmd .Verb == "" {
330
- return true
331
- }
332
- for _ , command := range c .commands {
333
- if command == cmd {
334
- continue
335
- }
336
- if cmd .Resource == "" && cmd .Namespace == command .Namespace {
337
- return true
338
- }
339
- if cmd .Verb == "" && cmd .Namespace == command .Namespace &&
340
- cmd .Resource == command .Resource {
341
- return true
342
- }
343
- }
344
-
345
- return false
185
+ // get a signature to sort commands
186
+ func (c * Command ) signature () string {
187
+ return c .Namespace + " " + c .Resource + " " + c .Verb + " " + c .Short
346
188
}
347
189
348
190
func (c * Command ) getHumanMarshalerOpt () * human.MarshalOpt {
@@ -353,98 +195,40 @@ func (c *Command) getHumanMarshalerOpt() *human.MarshalOpt {
353
195
return nil
354
196
}
355
197
356
- // get a signature to sort commands
357
- func (c * Command ) signature () string {
358
- return c .Namespace + " " + c .Resource + " " + c .Verb + " " + c .Short
359
- }
360
-
361
- // AliasIsValidCommandChild returns true is alias is a valid child command of given command
362
- // Useful for this case:
363
- // isl => instance server list
364
- // valid child of "instance"
365
- // invalid child of "rdb instance"
366
- func (c * Commands ) AliasIsValidCommandChild (command * Command , alias alias.Alias ) bool {
367
- // if alias is of size one, it means it cannot be a child
368
- if len (alias .Command ) == 1 {
369
- return true
370
- }
371
-
372
- // if command is verb, it cannot have children
373
- if command .Verb != "" {
374
- return true
375
- }
198
+ // seeAlsosAsStr returns all See Alsos as a single string
199
+ func (c * Command ) seeAlsosAsStr () string {
200
+ seeAlsos := make ([]string , 0 , len (c .SeeAlsos ))
376
201
377
- // if command is a resource, check command with alias' verb
378
- if command .Resource != "" {
379
- return c .Find (command .Namespace , command .Resource , alias .Command [1 ]) != nil
380
- }
202
+ for _ , cmdSeeAlso := range c .SeeAlsos {
203
+ short := " # " + cmdSeeAlso .Short
204
+ commandStr := " " + cmdSeeAlso .Command
381
205
382
- // if command is a namespace, check for alias' verb or resource
383
- if command .Namespace != "" {
384
- if len (alias .Command ) > 2 {
385
- return c .Find (command .Namespace , alias .Command [1 ], alias .Command [2 ]) != nil
206
+ seeAlsoLines := []string {
207
+ short ,
208
+ commandStr ,
386
209
}
387
-
388
- return c .Find (command .Namespace , alias .Command [1 ]) != nil
210
+ seeAlsos = append (seeAlsos , strings .Join (seeAlsoLines , "\n " ))
389
211
}
390
212
391
- return false
213
+ return strings . Join ( seeAlsos , " \n \n " )
392
214
}
393
215
394
- // addAliases add valid aliases to a command
395
- func (c * Commands ) addAliases (command * Command , aliases []alias.Alias ) {
396
- names := make ([]string , 0 , len (aliases ))
397
- for i := range aliases {
398
- if c .AliasIsValidCommandChild (command , aliases [i ]) && command .MatchAlias (aliases [i ]) {
399
- names = append (names , aliases [i ].Name )
400
- }
216
+ func (c * Command ) getPath () string {
217
+ if c .path != "" {
218
+ return c .path
401
219
}
402
- command .Aliases = append (command .Aliases , names ... )
403
- }
404
-
405
- // applyAliases add resource aliases to each commands
406
- func (c * Commands ) applyAliases (config * alias.Config ) {
407
- for _ , command := range c .commands {
408
- aliases := []alias.Alias (nil )
409
- exists := false
410
- switch {
411
- case command .Verb != "" :
412
- aliases , exists = config .ResolveAliasesByFirstWord (command .Verb )
413
- case command .Resource != "" :
414
- aliases , exists = config .ResolveAliasesByFirstWord (command .Resource )
415
- case command .Namespace != "" :
416
- aliases , exists = config .ResolveAliasesByFirstWord (command .Namespace )
417
- }
418
- if exists {
419
- c .addAliases (command , aliases )
420
- }
220
+ path := []string (nil )
221
+ if c .Namespace != "" {
222
+ path = append (path , c .Namespace )
421
223
}
422
- }
423
-
424
- // Copy returns a copy of a command
425
- func (c * Command ) Copy () * Command {
426
- newCommand := * c
427
- newCommand .Aliases = append ([]string (nil ), c .Aliases ... )
428
- newCommand .Examples = make ([]* Example , len (c .Examples ))
429
- for i := range c .Examples {
430
- e := * c .Examples [i ]
431
- newCommand .Examples [i ] = & e
224
+ if c .Resource != "" {
225
+ path = append (path , c .Resource )
432
226
}
433
- newCommand .SeeAlsos = make ([]* SeeAlso , len (c .SeeAlsos ))
434
- for i := range c .SeeAlsos {
435
- sa := * c .SeeAlsos [i ]
436
- newCommand .SeeAlsos [i ] = & sa
227
+ if c .Verb != "" {
228
+ path = append (path , c .Verb )
437
229
}
438
230
439
- return & newCommand
440
- }
441
-
442
- // Copy return a copy of all commands
443
- func (c * Commands ) Copy () * Commands {
444
- newCommands := make ([]* Command , len (c .commands ))
445
- for i := range c .commands {
446
- newCommands [i ] = c .commands [i ].Copy ()
447
- }
231
+ c .path = strings .Join (path , indexCommandSeparator )
448
232
449
- return NewCommands ( newCommands ... )
233
+ return c . path
450
234
}
0 commit comments