Skip to content

Commit cb1ecfd

Browse files
hsato03Henrique Sato
authored andcommitted
Fix usagetype parameter autocomplete (apache#156)
* Fix usage type autocomplete --------- Co-authored-by: Henrique Sato <[email protected]>
1 parent 7b7b451 commit cb1ecfd

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

cli/completer.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package cli
2020
import (
2121
"fmt"
2222
"sort"
23+
"strconv"
2324
"strings"
2425
"unicode"
2526

@@ -123,7 +124,14 @@ func buildArgOptions(response map[string]interface{}, hasID bool) []argOption {
123124
}
124125
var id, name, detail string
125126
if resource["id"] != nil {
126-
id = resource["id"].(string)
127+
switch rawID := resource["id"].(type) {
128+
case string:
129+
id = rawID
130+
case float64:
131+
id = strconv.FormatFloat(rawID, 'f', -1, 64)
132+
default:
133+
panic(fmt.Errorf("detected an invalid type at path (%v:%T). This should have been caught during validation, indicating a bug in CloudMonkey. Please report this issue", rawID, rawID))
134+
}
127135
}
128136
if resource["name"] != nil {
129137
name = resource["name"].(string)
@@ -417,15 +425,23 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
417425
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
418426
t.Config.StopSpinner(spinner)
419427

420-
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=")
428+
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") || autocompleteAPI.Name == "listUsageTypes"
421429
argOptions = buildArgOptions(response, hasID)
422430
}
423431

424432
filteredOptions := []argOption{}
425433
if len(argOptions) > 0 {
426-
sort.Slice(argOptions, func(i, j int) bool {
427-
return argOptions[i].Value < argOptions[j].Value
428-
})
434+
if autocompleteAPI.Name == "listUsageTypes" {
435+
sort.Slice(argOptions, func(i, j int) bool {
436+
i, _ = strconv.Atoi(argOptions[i].Value)
437+
j, _ = strconv.Atoi(argOptions[j].Value)
438+
return i < j
439+
})
440+
} else {
441+
sort.Slice(argOptions, func(i, j int) bool {
442+
return argOptions[i].Value < argOptions[j].Value
443+
})
444+
}
429445
for _, item := range argOptions {
430446
if strings.HasPrefix(item.Value, argInput) {
431447
filteredOptions = append(filteredOptions, item)

0 commit comments

Comments
 (0)