Skip to content

Fix usagetype parameter autocomplete #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2025
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