Skip to content

Commit 0f0a7da

Browse files
committed
Fix version parameter autocompletion
1 parent efbb895 commit 0f0a7da

2 files changed

Lines changed: 104 additions & 4 deletions

File tree

cli/completer.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type argOption struct {
113113
Detail string
114114
}
115115

116-
func buildArgOptions(response map[string]interface{}, hasID bool) []argOption {
116+
func buildArgOptions(response map[string]interface{}, hasID bool, valueField string) []argOption {
117117
argOptions := []argOption{}
118118
for _, v := range response {
119119
switch obj := v.(type) {
@@ -164,9 +164,18 @@ func buildArgOptions(response map[string]interface{}, hasID bool) []argOption {
164164
opt.Detail = detail
165165
}
166166
} else {
167-
opt.Value = name
167+
if valueField != "" {
168+
if value, ok := resource[valueField].(string); ok {
169+
opt.Value = value
170+
}
171+
}
172+
173+
if len(opt.Value) == 0 {
174+
opt.Value = name
175+
}
176+
168177
opt.Detail = detail
169-
if len(name) == 0 {
178+
if len(opt.Value) == 0 {
170179
opt.Value = detail
171180
}
172181
}
@@ -229,6 +238,14 @@ func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[st
229238
return nil
230239
}
231240

241+
if apiFound.Verb == "list" && strings.TrimSuffix(arg.Name, "=") == "version" {
242+
for _, responseKey := range apiFound.ResponseKeys {
243+
if responseKey == "version" {
244+
return apiFound
245+
}
246+
}
247+
}
248+
232249
var autocompleteAPI *config.API
233250
argName := strings.Replace(arg.Name, "=", "", -1)
234251
relatedNoun := argName
@@ -479,7 +496,12 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
479496
t.Config.StopSpinner(spinner)
480497

481498
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") || autocompleteAPI.Name == "listUsageTypes"
482-
argOptions = buildArgOptions(response, hasID)
499+
valueField := ""
500+
if apiFound == autocompleteAPI {
501+
valueField = strings.TrimSuffix(arg.Name, "=")
502+
}
503+
504+
argOptions = buildArgOptions(response, hasID, valueField)
483505
}
484506

485507
filteredOptions := []argOption{}

cli/completer_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,81 @@ func TestFindAutocompleteAPIHeuristicWinsOverRelated(t *testing.T) {
221221
t.Fatalf("expected listProjects, got %s", result.Name)
222222
}
223223
}
224+
225+
func TestFindAutocompleteAPIVersionUsesCurrentListAPI(t *testing.T) {
226+
tests := []struct {
227+
name string
228+
apiFound *config.API
229+
}{
230+
{
231+
name: "listHosts",
232+
apiFound: &config.API{
233+
Name: "listHosts",
234+
Verb: "list",
235+
Noun: "hosts",
236+
ResponseKeys: []string{
237+
"id",
238+
"name",
239+
"version",
240+
},
241+
},
242+
},
243+
{
244+
name: "listRouters",
245+
apiFound: &config.API{
246+
Name: "listRouters",
247+
Verb: "list",
248+
Noun: "routers",
249+
ResponseKeys: []string{
250+
"name",
251+
"version",
252+
},
253+
},
254+
},
255+
}
256+
257+
for _, tt := range tests {
258+
t.Run(tt.name, func(t *testing.T) {
259+
arg := &config.APIArg{
260+
Name: "version=",
261+
}
262+
263+
apiMap := map[string][]*config.API{
264+
"list": {
265+
{
266+
Name: "listKubernetesSupportedVersions",
267+
Noun: "kubernetessupportedversions",
268+
},
269+
},
270+
}
271+
272+
result := findAutocompleteAPI(arg, tt.apiFound, apiMap)
273+
274+
if result != tt.apiFound {
275+
t.Fatalf("expected %s, got %v", tt.apiFound.Name, result)
276+
}
277+
})
278+
}
279+
}
280+
281+
func TestBuildArgOptionsUsesValueField(t *testing.T) {
282+
response := map[string]interface{}{
283+
"host": []interface{}{
284+
map[string]interface{}{
285+
"id": "host-id",
286+
"name": "nvs-kvm01",
287+
"version": "4.22.1.0",
288+
},
289+
},
290+
}
291+
292+
options := buildArgOptions(response, false, "version")
293+
294+
if len(options) != 1 {
295+
t.Fatalf("expected 1 option, got %d", len(options))
296+
}
297+
298+
if options[0].Value != "4.22.1.0" {
299+
t.Fatalf("expected 4.22.1.0, got %s", options[0].Value)
300+
}
301+
}

0 commit comments

Comments
 (0)