Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions cli/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cli
import (
"fmt"
"sort"
"strconv"
"strings"
"unicode"

Expand Down Expand Up @@ -123,7 +124,14 @@ func buildArgOptions(response map[string]interface{}, hasID bool) []argOption {
}
var id, name, detail string
if resource["id"] != nil {
id = resource["id"].(string)
switch rawID := resource["id"].(type) {
case string:
id = rawID
case float64:
id = strconv.FormatFloat(rawID, 'f', -1, 64)
default:
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))
}
}
if resource["name"] != nil {
name = resource["name"].(string)
Expand Down Expand Up @@ -398,15 +406,23 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
t.Config.StopSpinner(spinner)

hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=")
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") || autocompleteAPI.Name == "listUsageTypes"
argOptions = buildArgOptions(response, hasID)
}

filteredOptions := []argOption{}
if len(argOptions) > 0 {
sort.Slice(argOptions, func(i, j int) bool {
return argOptions[i].Value < argOptions[j].Value
})
if autocompleteAPI.Name == "listUsageTypes" {
sort.Slice(argOptions, func(i, j int) bool {
i, _ = strconv.Atoi(argOptions[i].Value)
j, _ = strconv.Atoi(argOptions[j].Value)
return i < j
})
} else {
sort.Slice(argOptions, func(i, j int) bool {
return argOptions[i].Value < argOptions[j].Value
})
}
for _, item := range argOptions {
if strings.HasPrefix(item.Value, argInput) {
filteredOptions = append(filteredOptions, item)
Expand Down