Skip to content

Commit 469fa20

Browse files
committed
feat(cli): fixed code for copilote
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
1 parent d823b7e commit 469fa20

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

internal/cmd/completion.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ To make completions permanent, run:
6767

6868
func runCompletionInstall(_ *cobra.Command, _ []string) error {
6969
shell := os.Getenv("SHELL")
70+
if shell == "" {
71+
return fmt.Errorf("SHELL environment variable is not set — please run 'ovhcloud completion bash|zsh|fish|powershell' manually")
72+
}
7073
shellName := filepath.Base(shell)
7174

7275
var rcFile string

internal/cmd/completion_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ func TestCompletionInstall_UnsupportedShell(t *testing.T) {
150150
t.Errorf("unexpected error message: %v", err)
151151
}
152152
}
153+
154+
func TestCompletionInstall_EmptyShell(t *testing.T) {
155+
setupHome(t)
156+
t.Setenv("SHELL", "")
157+
158+
err := runCompletionInstall(nil, nil)
159+
if err == nil {
160+
t.Fatal("expected an error when SHELL is unset, got nil")
161+
}
162+
if !strings.Contains(err.Error(), "SHELL environment variable is not set") {
163+
t.Errorf("unexpected error message: %v", err)
164+
}
165+
}

internal/completion/completion.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package completion
66

77
import (
88
"fmt"
9+
"net/url"
910
"strconv"
1011
"time"
1112

@@ -41,7 +42,9 @@ func fetchSuggestions(endpoint, labelField, cacheKey string) ([]string, cobra.Sh
4142
return
4243
}
4344

44-
var suggestions []string
45+
// Start from a non-nil slice so a valid empty result is still cached
46+
// (nil is reserved for the error path above, which must not be cached).
47+
suggestions := make([]string, 0, len(ids))
4548
for _, raw := range ids {
4649
switch v := raw.(type) {
4750
case string:
@@ -116,7 +119,7 @@ func CloudResources(pathTemplate string) func(*cobra.Command, []string, string)
116119
if project == "" {
117120
return nil, cobra.ShellCompDirectiveNoFileComp
118121
}
119-
return fetchSuggestions(fmt.Sprintf(pathTemplate, project), "", "")
122+
return fetchSuggestions(fmt.Sprintf(pathTemplate, url.PathEscape(project)), "", "")
120123
}
121124
}
122125

@@ -133,9 +136,9 @@ func CloudResourceWithChild(parentTemplate, childTemplate string) func(*cobra.Co
133136
}
134137
switch len(args) {
135138
case 0:
136-
return fetchSuggestions(fmt.Sprintf(parentTemplate, project), "", "")
139+
return fetchSuggestions(fmt.Sprintf(parentTemplate, url.PathEscape(project)), "", "")
137140
case 1:
138-
return fetchSuggestions(fmt.Sprintf(childTemplate, project, args[0]), "", "")
141+
return fetchSuggestions(fmt.Sprintf(childTemplate, url.PathEscape(project), url.PathEscape(args[0])), "", "")
139142
default:
140143
return nil, cobra.ShellCompDirectiveNoFileComp
141144
}

0 commit comments

Comments
 (0)