@@ -57,48 +57,11 @@ type FlagSpec struct {
57
57
EnumValues []string
58
58
}
59
59
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
+ }
102
65
}
103
66
104
67
// newAutoCompleteResponse builds a new AutocompleteResponse
@@ -111,11 +74,13 @@ func newAutoCompleteResponse(suggestions []string) *AutocompleteResponse {
111
74
112
75
// NewAutoCompleteCommandNode creates a new node corresponding to a command or subcommand.
113
76
// 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 ) ),
117
80
Type : AutoCompleteNodeTypeCommand ,
118
81
}
82
+ node .addFlags (flags )
83
+ return node
119
84
}
120
85
121
86
// NewAutoCompleteArgNode creates a new node corresponding to a command argument.
@@ -136,10 +101,18 @@ func NewAutoCompleteArgNode(cmd *Command, argSpec *ArgSpec) *AutoCompleteNode {
136
101
// or the lowest nodes are the possible values if the exist.
137
102
func NewAutoCompleteFlagNode (parent * AutoCompleteNode , flagSpec * FlagSpec ) * AutoCompleteNode {
138
103
node := & AutoCompleteNode {
139
- Children : make (map [string ]* AutoCompleteNode ),
140
- Type : AutoCompleteNodeTypeFlag ,
141
- Name : flagSpec .Name ,
104
+ Type : AutoCompleteNodeTypeFlag ,
105
+ Name : flagSpec .Name ,
142
106
}
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
+
143
116
if flagSpec .HasVariableValue {
144
117
node .Children [positionalValueNodeID ] = & AutoCompleteNode {
145
118
Children : parent .Children ,
@@ -161,9 +134,9 @@ func NewAutoCompleteFlagNode(parent *AutoCompleteNode, flagSpec *FlagSpec) *Auto
161
134
// GetChildOrCreate search a child node by name,
162
135
// and either returns it if found
163
136
// 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 {
165
138
if _ , exist := node .Children [name ]; ! exist {
166
- childNode := NewAutoCompleteCommandNode ()
139
+ childNode := NewAutoCompleteCommandNode (flags )
167
140
node .Children [name ] = childNode
168
141
for _ , alias := range aliases {
169
142
node .Children [alias ] = childNode
@@ -209,17 +182,16 @@ func (node *AutoCompleteNode) isLeafCommand() bool {
209
182
}
210
183
211
184
// 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 )
215
188
for _ , cmd := range commands .commands {
216
189
node := root
217
190
218
191
// Creates nodes for namespaces, resources, verbs
219
192
for _ , part := range []string {cmd .Namespace , cmd .Resource , cmd .Verb } {
220
193
if part != "" {
221
- node = node .GetChildOrCreate (part , cmd .Aliases )
222
- node .addGlobalFlags ()
194
+ node = node .GetChildOrCreate (part , cmd .Aliases , globalFlags )
223
195
}
224
196
}
225
197
@@ -256,7 +228,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
256
228
commands := ExtractCommands (ctx )
257
229
258
230
// Create AutoComplete Tree
259
- commandTreeRoot := BuildAutoCompleteTree (commands )
231
+ commandTreeRoot := BuildAutoCompleteTree (ctx , commands )
260
232
261
233
// 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`.
262
234
node := commandTreeRoot
0 commit comments