Skip to content
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

Fix usagetype parameter autocomplete #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 30 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 isNumeric(argOptions[0].Value) {
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 All @@ -433,3 +449,12 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)

return options, offset
}

func isNumeric(str string) bool {
for _, char := range str {
if !unicode.IsDigit(char) {
return false
}
}
return true
}