@@ -57,48 +57,11 @@ type FlagSpec struct {
5757 EnumValues []string
5858}
5959
60- func (node * AutoCompleteNode ) addGlobalFlags () {
61- printerTypes := []string {
62- PrinterTypeHuman .String (),
63- PrinterTypeJSON .String (),
64- PrinterTypeYAML .String (),
65- PrinterTypeTemplate .String (),
66- }
67-
68- node .Children ["-c" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
69- Name : "-c" ,
70- })
71- node .Children ["--config" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
72- Name : "--config" ,
73- })
74- node .Children ["-D" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
75- Name : "-D" ,
76- })
77- node .Children ["--debug" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
78- Name : "--debug" ,
79- })
80- node .Children ["-h" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
81- Name : "-h" ,
82- })
83- node .Children ["--help" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
84- Name : "--help" ,
85- })
86- node .Children ["-o" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
87- Name : "-o" ,
88- EnumValues : printerTypes ,
89- })
90- node .Children ["--output" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
91- Name : "--output" ,
92- EnumValues : printerTypes ,
93- })
94- node .Children ["-p" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
95- Name : "-p" ,
96- HasVariableValue : true ,
97- })
98- node .Children ["--profile" ] = NewAutoCompleteFlagNode (node , & FlagSpec {
99- Name : "--profile" ,
100- HasVariableValue : true ,
101- })
60+ func (node * AutoCompleteNode ) addFlags (flags []FlagSpec ) {
61+ for i := range flags {
62+ flag := & flags [i ]
63+ node .Children [flag .Name ] = NewAutoCompleteFlagNode (node , flag )
64+ }
10265}
10366
10467// newAutoCompleteResponse builds a new AutocompleteResponse
@@ -111,11 +74,13 @@ func newAutoCompleteResponse(suggestions []string) *AutocompleteResponse {
11174
11275// NewAutoCompleteCommandNode creates a new node corresponding to a command or subcommand.
11376// These nodes are not necessarily leaf nodes.
114- func NewAutoCompleteCommandNode () * AutoCompleteNode {
115- return & AutoCompleteNode {
116- Children : make (map [string ]* AutoCompleteNode ),
77+ func NewAutoCompleteCommandNode (flags [] FlagSpec ) * AutoCompleteNode {
78+ node := & AutoCompleteNode {
79+ Children : make (map [string ]* AutoCompleteNode , len ( flags ) ),
11780 Type : AutoCompleteNodeTypeCommand ,
11881 }
82+ node .addFlags (flags )
83+ return node
11984}
12085
12186// NewAutoCompleteArgNode creates a new node corresponding to a command argument.
@@ -136,10 +101,18 @@ func NewAutoCompleteArgNode(cmd *Command, argSpec *ArgSpec) *AutoCompleteNode {
136101// or the lowest nodes are the possible values if the exist.
137102func NewAutoCompleteFlagNode (parent * AutoCompleteNode , flagSpec * FlagSpec ) * AutoCompleteNode {
138103 node := & AutoCompleteNode {
139- Children : make (map [string ]* AutoCompleteNode ),
140- Type : AutoCompleteNodeTypeFlag ,
141- Name : flagSpec .Name ,
104+ Type : AutoCompleteNodeTypeFlag ,
105+ Name : flagSpec .Name ,
142106 }
107+ childrenCount := len (flagSpec .EnumValues )
108+ if flagSpec .HasVariableValue {
109+ childrenCount ++
110+ }
111+
112+ if childrenCount > 0 {
113+ node .Children = make (map [string ]* AutoCompleteNode , childrenCount )
114+ }
115+
143116 if flagSpec .HasVariableValue {
144117 node .Children [positionalValueNodeID ] = & AutoCompleteNode {
145118 Children : parent .Children ,
@@ -161,9 +134,9 @@ func NewAutoCompleteFlagNode(parent *AutoCompleteNode, flagSpec *FlagSpec) *Auto
161134// GetChildOrCreate search a child node by name,
162135// and either returns it if found
163136// or create new children with the given name and aliases, and returns it.
164- func (node * AutoCompleteNode ) GetChildOrCreate (name string , aliases []string ) * AutoCompleteNode {
137+ func (node * AutoCompleteNode ) GetChildOrCreate (name string , aliases []string , flags [] FlagSpec ) * AutoCompleteNode {
165138 if _ , exist := node .Children [name ]; ! exist {
166- childNode := NewAutoCompleteCommandNode ()
139+ childNode := NewAutoCompleteCommandNode (flags )
167140 node .Children [name ] = childNode
168141 for _ , alias := range aliases {
169142 node .Children [alias ] = childNode
@@ -209,17 +182,16 @@ func (node *AutoCompleteNode) isLeafCommand() bool {
209182}
210183
211184// BuildAutoCompleteTree builds the autocomplete tree from the commands, subcommands and arguments
212- func BuildAutoCompleteTree (commands * Commands ) * AutoCompleteNode {
213- root := NewAutoCompleteCommandNode ( )
214- root . addGlobalFlags ( )
185+ func BuildAutoCompleteTree (ctx context. Context , commands * Commands ) * AutoCompleteNode {
186+ globalFlags := getGlobalFlags ( ctx )
187+ root := NewAutoCompleteCommandNode ( globalFlags )
215188 for _ , cmd := range commands .commands {
216189 node := root
217190
218191 // Creates nodes for namespaces, resources, verbs
219192 for _ , part := range []string {cmd .Namespace , cmd .Resource , cmd .Verb } {
220193 if part != "" {
221- node = node .GetChildOrCreate (part , cmd .Aliases )
222- node .addGlobalFlags ()
194+ node = node .GetChildOrCreate (part , cmd .Aliases , globalFlags )
223195 }
224196 }
225197
@@ -256,7 +228,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
256228 commands := ExtractCommands (ctx )
257229
258230 // Create AutoComplete Tree
259- commandTreeRoot := BuildAutoCompleteTree (commands )
231+ commandTreeRoot := BuildAutoCompleteTree (ctx , commands )
260232
261233 // For each left word that is not a flag nor an argument, we try to go deeper in the autocomplete tree and store the current node in `node`.
262234 node := commandTreeRoot
0 commit comments