From 307d7783ce939f28eac083d7a65475344c5c5dbc Mon Sep 17 00:00:00 2001 From: Henrique Sato Date: Wed, 1 Jan 2025 12:43:33 -0300 Subject: [PATCH 1/3] Fix usage type autocomplete --- cli/completer.go | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index af8cab05..817b0739 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -20,6 +20,7 @@ package cli import ( "fmt" "sort" + "strconv" "strings" "unicode" @@ -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) @@ -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) @@ -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 +} From 57d76d8d27016bb18bbacaaec59b12822e2e14fa Mon Sep 17 00:00:00 2001 From: Henrique Sato Date: Sat, 26 Jul 2025 19:13:06 -0300 Subject: [PATCH 2/3] address review --- cli/completer.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index 817b0739..117fbd94 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -412,7 +412,7 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) filteredOptions := []argOption{} if len(argOptions) > 0 { - if isNumeric(argOptions[0].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) @@ -449,12 +449,3 @@ 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 -} From 460c5786fc533fe31a0660174c814e7db73bc8e3 Mon Sep 17 00:00:00 2001 From: Henrique Sato Date: Thu, 7 Aug 2025 09:31:13 -0300 Subject: [PATCH 3/3] Fix lint --- cli/completer.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index 117fbd94..40fff11c 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -124,13 +124,13 @@ func buildArgOptions(response map[string]interface{}, hasID bool) []argOption { } var id, name, detail string if resource["id"] != nil { - switch rawId := resource["id"].(type) { + switch rawID := resource["id"].(type) { case string: - id = rawId + id = rawID case float64: - id = strconv.FormatFloat(rawId, 'f', -1, 64) + 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)) + 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 {