feat: add shell completion for host names (#37)

- Add ValidArgsFunction to RootCmd for dynamic host completion
- Add 'sshm completion' subcommand for bash/zsh/fish/powershell
- Support prefix matching and case-insensitive filtering
- Respect --config flag for custom SSH config files
- Add comprehensive tests for completion functionality
- Document setup instructions in README

Co-authored-by: Guillaume Archambault <67098259+Gu1llaum-3@users.noreply.github.com>
This commit is contained in:
David Ibia
2026-01-04 19:37:52 +01:00
committed by GitHub
parent 435597f694
commit 2f9587c8c8
4 changed files with 423 additions and 0 deletions

60
cmd/completion.go Normal file
View File

@@ -0,0 +1,60 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script",
Long: `Generate shell completion script for sshm.
To load completions:
Bash:
$ source <(sshm completion bash)
# To load completions for each session, add to your ~/.bashrc:
# echo 'source <(sshm completion bash)' >> ~/.bashrc
Zsh:
$ source <(sshm completion zsh)
# To load completions for each session, add to your ~/.zshrc:
# echo 'source <(sshm completion zsh)' >> ~/.zshrc
Fish:
$ sshm completion fish | source
# To load completions for each session:
$ sshm completion fish > ~/.config/fish/completions/sshm.fish
PowerShell:
PS> sshm completion powershell | Out-String | Invoke-Expression
# To load completions for each session, add to your PowerShell profile:
# Add-Content $PROFILE 'sshm completion 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 {
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletionV2(os.Stdout, true)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
return nil
},
}
func init() {
RootCmd.AddCommand(completionCmd)
}