Skip to content

Commit 307d778

Browse files
committed
Fix usage type autocomplete
1 parent 1f45761 commit 307d778

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

cli/completer.go

Lines changed: 30 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)
@@ -398,15 +406,23 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
398406
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
399407
t.Config.StopSpinner(spinner)
400408

401-
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=")
409+
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") || autocompleteAPI.Name == "listUsageTypes"
402410
argOptions = buildArgOptions(response, hasID)
403411
}
404412

405413
filteredOptions := []argOption{}
406414
if len(argOptions) > 0 {
407-
sort.Slice(argOptions, func(i, j int) bool {
408-
return argOptions[i].Value < argOptions[j].Value
409-
})
415+
if isNumeric(argOptions[0].Value) {
416+
sort.Slice(argOptions, func(i, j int) bool {
417+
i, _ = strconv.Atoi(argOptions[i].Value)
418+
j, _ = strconv.Atoi(argOptions[j].Value)
419+
return i < j
420+
})
421+
} else {
422+
sort.Slice(argOptions, func(i, j int) bool {
423+
return argOptions[i].Value < argOptions[j].Value
424+
})
425+
}
410426
for _, item := range argOptions {
411427
if strings.HasPrefix(item.Value, argInput) {
412428
filteredOptions = append(filteredOptions, item)
@@ -433,3 +449,12 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
433449

434450
return options, offset
435451
}
452+
453+
func isNumeric(str string) bool {
454+
for _, char := range str {
455+
if !unicode.IsDigit(char) {
456+
return false
457+
}
458+
}
459+
return true
460+
}

0 commit comments

Comments
 (0)