@@ -2,9 +2,7 @@ package core
22
33import (
44 "context"
5- "fmt"
65 "reflect"
7- "sort"
86 "strings"
97
108 "github.com/scaleway/scaleway-cli/v2/core/human"
@@ -117,26 +115,6 @@ func (c *Command) Override(builder func(command *Command) *Command) {
117115 * c = * builder (c )
118116}
119117
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-
140118func (c * Command ) GetCommandLine (binaryName string ) string {
141119 return strings .Trim (
142120 binaryName + " " + strings .ReplaceAll (c .getPath (), indexCommandSeparator , " " ),
@@ -162,24 +140,6 @@ func (c *Command) GetUsage(binaryName string, commands *Commands) string {
162140 return strings .Join (parts , " " )
163141}
164142
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-
183143// AddInterceptors add one or multiple interceptors to a command.
184144// These new interceptors will be added after the already present interceptors (if any).
185145func (c * Command ) AddInterceptors (interceptors ... CommandInterceptor ) {
@@ -204,145 +164,27 @@ func (c *Command) MatchAlias(alias alias.Alias) bool {
204164 return true
205165}
206166
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
297175 }
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
309180 }
310181
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
323183}
324184
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
346188}
347189
348190func (c * Command ) getHumanMarshalerOpt () * human.MarshalOpt {
@@ -353,98 +195,40 @@ func (c *Command) getHumanMarshalerOpt() *human.MarshalOpt {
353195 return nil
354196}
355197
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 ))
376201
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
381205
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 ,
386209 }
387-
388- return c .Find (command .Namespace , alias .Command [1 ]) != nil
210+ seeAlsos = append (seeAlsos , strings .Join (seeAlsoLines , "\n " ))
389211 }
390212
391- return false
213+ return strings . Join ( seeAlsos , " \n \n " )
392214}
393215
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
401219 }
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 )
421223 }
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 )
432226 }
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 )
437229 }
438230
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 )
448232
449- return NewCommands ( newCommands ... )
233+ return c . path
450234}
0 commit comments