Skip to content
Closed
Show file tree
Hide file tree
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
96 changes: 96 additions & 0 deletions cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package completion

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// NewCmd creates the completion command
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "completions [bash|zsh|fish|powershell]",
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Use string shows the shell argument in square brackets ([bash|zsh|fish|powershell]), which conventionally indicates an optional argument, but the command enforces cobra.ExactArgs(1) so the shell is actually required. To avoid confusing users and keep the CLI contract clear, consider updating the usage line to use a required placeholder (for example, completions SHELL) or relaxing the arg validation if you intend the argument to be optional.

Suggested change
Use: "completions [bash|zsh|fish|powershell]",
Use: "completions SHELL",

Copilot uses AI. Check for mistakes.
Short: "Generate shell completion script",
Long: `Generate shell completion script for srvctl.

The shell code must be evaluated to provide interactive completion of srvctl
commands. This can be done by sourcing it from your shell configuration file.

Note for zsh users: zsh completions require zsh >= 5.2.

Bash:

# Load completions into current shell
source <(srvctl completions bash)

# To load completions for each session, execute once:

# Linux (requires bash-completion package):
srvctl completions bash > /etc/bash_completion.d/srvctl

# Linux (user-level):
srvctl completions bash > ~/.bash_completion

# macOS (requires bash-completion package from homebrew):
# For Bash 3.2 (default macOS): brew install bash-completion
# For Bash 4.1+: brew install bash-completion@2
srvctl completions bash > $(brew --prefix)/etc/bash_completion.d/srvctl

Zsh:

# Load completions into current shell
source <(srvctl completions zsh)

# To load completions for each session, add the following to ~/.zshrc:
source <(srvctl completions zsh)

# If shell completion is not already enabled in your environment,
# you may need to add this to ~/.zshrc:
autoload -U compinit; compinit

Fish:

# Load completions into current shell
srvctl completions fish | source

# To load completions for each session, execute once:
srvctl completions fish > ~/.config/fish/completions/srvctl.fish

PowerShell:

# Load completions into current shell
srvctl completions powershell | Out-String | Invoke-Expression

# To load completions for each session, add to your PowerShell profile:

# Option 1: Save to script and source from profile
srvctl completions powershell > "$HOME\.srvctl\completion.ps1"
Add-Content $PROFILE ". '$HOME\.srvctl\completion.ps1'"

# Option 2: Lazy load with command check
Add-Content $PROFILE "if (Get-Command srvctl -ErrorAction SilentlyContinue) {
srvctl completions powershell | Out-String | Invoke-Expression
}"
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
rootCmd := cmd.Root()
switch args[0] {
Comment on lines +77 to +81
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that Args uses cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), args[0] can only ever be one of the four valid shells, so the default branch in the switch below is effectively unreachable. To simplify the control flow, either drop cobra.OnlyValidArgs and rely on the default case for validation, or keep OnlyValidArgs and remove the dead default branch.

Copilot uses AI. Check for mistakes.
case "bash":
return rootCmd.GenBashCompletionV2(os.Stdout, true)
case "zsh":
return rootCmd.GenZshCompletion(os.Stdout)
case "fish":
return rootCmd.GenFishCompletion(os.Stdout, true)
case "powershell":
return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
default:
return fmt.Errorf("unsupported shell: %s", args[0])
}
},
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/serverscom/srvctl/cmd/base"
"github.com/serverscom/srvctl/cmd/completion"
"github.com/serverscom/srvctl/cmd/config"
"github.com/serverscom/srvctl/cmd/context"
"github.com/serverscom/srvctl/cmd/entities/account"
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewRootCmd(version string) *cobra.Command {
cmd.AddCommand(login.NewCmd(cmdContext, clientFactory))
cmd.AddCommand(context.NewCmd(cmdContext))
cmd.AddCommand(config.NewCmd(cmdContext))
cmd.AddCommand(completion.NewCmd())

// resources comands
cmd.AddCommand(sshkeys.NewCmd(cmdContext))
Expand Down
5 changes: 5 additions & 0 deletions docs/srvctl-completions/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Generate shell completion scripts for srvctl.

Shell completions provide command and flag suggestions when pressing Tab in your terminal, making it easier to discover and use srvctl commands.

Supported shells: bash, zsh, fish, and powershell.
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"powershell" is capitalized inconsistently with the rest of the docs (and the official name), which use "PowerShell". Consider updating this line to use "PowerShell" for consistency and correctness.

Suggested change
Supported shells: bash, zsh, fish, and powershell.
Supported shells: bash, zsh, fish, and PowerShell.

Copilot uses AI. Check for mistakes.
41 changes: 41 additions & 0 deletions docs/srvctl-completions/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Load completions in current bash session:

```
source <(srvctl completions bash)
```

Install completions permanently on Linux (system-wide):

```
srvctl completions bash > /etc/bash_completion.d/srvctl
```

Install completions permanently on Linux (user-level):

```
srvctl completions bash > ~/.bash_completion
```

Install completions permanently on macOS with Homebrew:

```
srvctl completions bash > $(brew --prefix)/etc/bash_completion.d/srvctl
```

Install completions for zsh (add to ~/.zshrc):

```
source <(srvctl completions zsh)
```

Install completions for fish:

```
srvctl completions fish > ~/.config/fish/completions/srvctl.fish
```

Install completions for PowerShell:

```
srvctl completions powershell | Out-String | Invoke-Expression
```
Loading