Dev/olivierdubo/issues autocomplete#193
Conversation
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
| } | ||
|
|
||
| var projectIDs []string | ||
| if err := httpLib.Client.Get("/v1/cloud/project", &projectIDs); err != nil { |
There was a problem hiding this comment.
should directly use fetchExpandedArray and then iterate on the results
| Short: "Manage billing alert configurations in the given cloud project", | ||
| } | ||
| alertingCmd.PersistentFlags().StringVar(&cloud.CloudProject, "cloud-project", "", "Cloud project ID") | ||
| alertingCmd.RegisterFlagCompletionFunc("cloud-project", completion.CloudProjects) //nolint:errcheck |
There was a problem hiding this comment.
instead of adding this line in every file, I think we could iterate on the children of the root cloud command and assign the completion func to each of them
There was a problem hiding this comment.
Pull request overview
Adds shell autocompletion support focused on improving UX for Public Cloud commands by completing --cloud-project values from the OVHcloud API, and introduces a ovhcloud completion install helper to persist completion setup in common shells.
Changes:
- Add a new
internal/completionpackage with an API-backed completion provider for--cloud-project. - Introduce a new top-level
completioncommand (bash/zsh/fish/powershell) pluscompletion install. - Register
--cloud-projectflag completion across manyovhcloud cloud ...subcommands.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/completion/completion.go | Implements API-backed completion suggestions for --cloud-project. |
| internal/cmd/completion.go | Adds ovhcloud completion generation and completion install workflow. |
| internal/cmd/cloud_user.go | Registers --cloud-project flag completion for cloud user. |
| internal/cmd/cloud_storage_swift.go | Registers --cloud-project flag completion for cloud storage swift. |
| internal/cmd/cloud_storage_object.go | Registers --cloud-project flag completion for cloud storage object. |
| internal/cmd/cloud_storage_file.go | Registers --cloud-project flag completion for cloud storage file. |
| internal/cmd/cloud_storage_block.go | Registers --cloud-project flag completion for cloud storage block. |
| internal/cmd/cloud_ssh_key.go | Registers --cloud-project flag completion for cloud ssh-key. |
| internal/cmd/cloud_savings_plan.go | Registers --cloud-project flag completion for cloud savings-plan. |
| internal/cmd/cloud_region.go | Registers --cloud-project flag completion for cloud region. |
| internal/cmd/cloud_reference.go | Registers --cloud-project flag completion for cloud reference. |
| internal/cmd/cloud_quota.go | Registers --cloud-project flag completion for cloud quota. |
| internal/cmd/cloud_project.go | Registers --cloud-project flag completion for cloud project. |
| internal/cmd/cloud_operation.go | Registers --cloud-project flag completion for cloud operation. |
| internal/cmd/cloud_network.go | Registers --cloud-project flag completion for cloud network. |
| internal/cmd/cloud_managed_registry.go | Registers --cloud-project flag completion for cloud managed-registry. |
| internal/cmd/cloud_managed_rancher.go | Registers --cloud-project flag completion for cloud managed-rancher. |
| internal/cmd/cloud_managed_kubernetes.go | Registers --cloud-project flag completion for cloud managed-kubernetes. |
| internal/cmd/cloud_managed_database.go | Registers --cloud-project flag completion for cloud managed-database. |
| internal/cmd/cloud_managed_analytics.go | Registers --cloud-project flag completion for cloud managed-analytics. |
| internal/cmd/cloud_loadbalancer.go | Registers --cloud-project flag completion for cloud loadbalancer. |
| internal/cmd/cloud_ip.go | Registers --cloud-project flag completion for cloud ip. |
| internal/cmd/cloud_instance.go | Registers --cloud-project flag completion for cloud instance. |
| internal/cmd/cloud_alerting.go | Registers --cloud-project flag completion for cloud alerting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var suggestions []string | ||
| for _, id := range projectIDs { | ||
| var project struct { | ||
| Description string `json:"description"` | ||
| } | ||
| if err := httpLib.Client.Get(fmt.Sprintf("/v1/cloud/project/%s", id), &project); err == nil && project.Description != "" { | ||
| suggestions = append(suggestions, id+"\t"+project.Description) | ||
| } else { | ||
| suggestions = append(suggestions, id) | ||
| } | ||
| } |
There was a problem hiding this comment.
also, we should cache the results to avoid making the call every time
| var rcFile string | ||
| var completionLine string | ||
| home, _ := os.UserHomeDir() | ||
|
|
| func runCompletionInstall(_ *cobra.Command, _ []string) error { | ||
| shell := os.Getenv("SHELL") | ||
| shellName := filepath.Base(shell) | ||
|
|
||
| var rcFile string | ||
| var completionLine string | ||
| home, _ := os.UserHomeDir() | ||
|
|
||
| switch shellName { | ||
| case "bash": | ||
| rcFile = filepath.Join(home, ".bashrc") | ||
| completionLine = `eval "$(ovhcloud completion bash)"` | ||
| case "zsh": | ||
| rcFile = filepath.Join(home, ".zshrc") | ||
| completionLine = `eval "$(ovhcloud completion zsh)"` | ||
| case "fish": | ||
| configDir := os.Getenv("XDG_CONFIG_HOME") | ||
| if configDir == "" { | ||
| configDir = filepath.Join(home, ".config") | ||
| } | ||
| fishCompDir := filepath.Join(configDir, "fish", "completions") | ||
| if err := os.MkdirAll(fishCompDir, 0755); err != nil { | ||
| return fmt.Errorf("failed to create fish completions dir: %w", err) | ||
| } | ||
| destFile := filepath.Join(fishCompDir, "ovhcloud.fish") | ||
| f, err := os.Create(destFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create fish completion file: %w", err) | ||
| } | ||
| defer f.Close() | ||
| if err := rootCmd.GenFishCompletion(f, true); err != nil { | ||
| return fmt.Errorf("failed to write fish completion: %w", err) | ||
| } | ||
| fmt.Printf("✅ Fish completion installed to %s\n", destFile) | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unsupported shell %q — please run 'ovhcloud completion bash|zsh|fish|powershell' manually", shellName) | ||
| } | ||
|
|
| import ( | ||
| "github.com/ovh/ovhcloud-cli/internal/services/cloud" | ||
| "github.com/spf13/cobra" | ||
| "github.com/ovh/ovhcloud-cli/internal/completion" | ||
| ) |
| func init() { | ||
| completionCmd := &cobra.Command{ | ||
| Use: "completion [bash|zsh|fish|powershell]", | ||
| Short: "Generate shell completion scripts", | ||
| Long: `Generate shell completion scripts for ovhcloud CLI. | ||
|
|
||
| To load completions in your current shell session: | ||
|
|
||
| bash: | ||
| source <(ovhcloud completion bash) | ||
|
|
||
| zsh: | ||
| source <(ovhcloud completion zsh) | ||
|
|
||
| fish: | ||
| ovhcloud completion fish | source | ||
|
|
||
| To make completions permanent, run: | ||
|
|
||
| ovhcloud completion install | ||
| `, | ||
| ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, | ||
| Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) == 0 { | ||
| return cmd.Help() | ||
| } | ||
| switch args[0] { | ||
| case "bash": | ||
| return rootCmd.GenBashCompletion(os.Stdout) | ||
| case "zsh": | ||
| return rootCmd.GenZshCompletion(os.Stdout) | ||
| case "fish": | ||
| return rootCmd.GenFishCompletion(os.Stdout, true) | ||
| case "powershell": | ||
| return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| completionCmd.AddCommand(&cobra.Command{ | ||
| Use: "install", | ||
| Short: "Install shell completion permanently in your shell rc file", | ||
| RunE: runCompletionInstall, | ||
| }) | ||
|
|
||
| rootCmd.AddCommand(completionCmd) | ||
| } |
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Signed-off-by: olivier dubo <olivier.dubo@ovhcloud.com>
Description
Added shell autocompletion for the --cloud-project flag across all ovhcloud cloud subcommands. When pressing TAB after --cloud-project, the CLI queries the OVHcloud API and displays the list of available project IDs with their descriptions. A new ovhcloud completion install command automatically sets up completion in the user's shell (~/.bashrc or ~/.zshrc) without any manual configuration.
Fixes #xx (issue)
Type of change
Please delete options that are not relevant.
Checklist:
go mod tidy