Skip to content

Commit 7ade14b

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

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

cli/completer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type argOption struct {
115115

116116
func buildArgOptions(response map[string]interface{}, hasID bool, valueField string) []argOption {
117117
argOptions := []argOption{}
118+
seenValues := make(map[string]struct{})
118119
for _, v := range response {
119120
switch obj := v.(type) {
120121
case []interface{}:
@@ -179,6 +180,13 @@ func buildArgOptions(response map[string]interface{}, hasID bool, valueField str
179180
opt.Value = detail
180181
}
181182
}
183+
if valueField != "" {
184+
if _, seen := seenValues[opt.Value]; seen {
185+
continue
186+
}
187+
seenValues[opt.Value] = struct{}{}
188+
}
189+
182190
argOptions = append(argOptions, opt)
183191
}
184192
break
@@ -240,7 +248,7 @@ func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[st
240248

241249
if apiFound.Verb == "list" && strings.TrimSuffix(arg.Name, "=") == "version" {
242250
for _, responseKey := range apiFound.ResponseKeys {
243-
if responseKey == "version" {
251+
if strings.TrimSuffix(responseKey, ",") == "version" {
244252
return apiFound
245253
}
246254
}

cli/completer_test.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ func TestFindAutocompleteAPIVersionUsesCurrentListAPI(t *testing.T) {
234234
Verb: "list",
235235
Noun: "hosts",
236236
ResponseKeys: []string{
237-
"id",
238-
"name",
239-
"version",
237+
"id,",
238+
"name,",
239+
"version,",
240240
},
241241
},
242242
},
@@ -247,8 +247,8 @@ func TestFindAutocompleteAPIVersionUsesCurrentListAPI(t *testing.T) {
247247
Verb: "list",
248248
Noun: "routers",
249249
ResponseKeys: []string{
250-
"name",
251-
"version",
250+
"name,",
251+
"version,",
252252
},
253253
},
254254
},
@@ -280,22 +280,39 @@ func TestFindAutocompleteAPIVersionUsesCurrentListAPI(t *testing.T) {
280280

281281
func TestBuildArgOptionsUsesValueField(t *testing.T) {
282282
response := map[string]interface{}{
283-
"host": []interface{}{
283+
"router": []interface{}{
284284
map[string]interface{}{
285-
"id": "host-id",
286-
"name": "nvs-kvm01",
287-
"version": "4.22.1.0",
285+
"id": "router-1",
286+
"name": "router-1",
287+
"version": "4.22.1.0",
288+
"ipaddress": "10.0.0.1",
289+
},
290+
map[string]interface{}{
291+
"id": "router-2",
292+
"name": "router-2",
293+
"version": "4.22.1.0",
294+
"ipaddress": "10.0.0.2",
295+
},
296+
map[string]interface{}{
297+
"id": "router-3",
298+
"name": "router-3",
299+
"version": "4.21.0.0",
300+
"ipaddress": "10.0.0.3",
288301
},
289302
},
290303
}
291304

292305
options := buildArgOptions(response, false, "version")
293306

294-
if len(options) != 1 {
295-
t.Fatalf("expected 1 option, got %d", len(options))
307+
if len(options) != 2 {
308+
t.Fatalf("expected 2 options, got %d", len(options))
296309
}
297310

298311
if options[0].Value != "4.22.1.0" {
299312
t.Fatalf("expected 4.22.1.0, got %s", options[0].Value)
300313
}
314+
315+
if options[1].Value != "4.21.0.0" {
316+
t.Fatalf("expected 4.21.0.0, got %s", options[1].Value)
317+
}
301318
}

0 commit comments

Comments
 (0)