...
1
16
17 package templates
18
19 import (
20 "github.com/spf13/cobra"
21 )
22
23 type CommandGroup struct {
24 Message string
25 Commands []*cobra.Command
26 }
27
28 type CommandGroups []CommandGroup
29
30 func (g CommandGroups) Add(c *cobra.Command) {
31 for _, group := range g {
32 c.AddCommand(group.Commands...)
33 }
34 }
35
36 func (g CommandGroups) Has(c *cobra.Command) bool {
37 for _, group := range g {
38 for _, command := range group.Commands {
39 if command == c {
40 return true
41 }
42 }
43 }
44 return false
45 }
46
47 func AddAdditionalCommands(g CommandGroups, message string, cmds []*cobra.Command) CommandGroups {
48 group := CommandGroup{Message: message}
49 for _, c := range cmds {
50
51 if !g.Has(c) && len(c.Short) != 0 {
52 group.Commands = append(group.Commands, c)
53 }
54 }
55 if len(group.Commands) == 0 {
56 return g
57 }
58 return append(g, group)
59 }
60
View as plain text