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

View File

@@ -54,6 +54,37 @@ Examples:
Version: AppVersion,
Args: cobra.ArbitraryArgs,
SilenceUsage: true,
SilenceErrors: true, // We'll handle errors ourselves
// ValidArgsFunction provides shell completion for host names
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Only complete the first positional argument (host name)
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var hosts []config.SSHHost
var err error
if configFile != "" {
hosts, err = config.ParseSSHConfigFile(configFile)
} else {
hosts, err = config.ParseSSHConfig()
}
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var completions []string
toCompleteLower := strings.ToLower(toComplete)
for _, host := range hosts {
if strings.HasPrefix(strings.ToLower(host.Name), toCompleteLower) {
completions = append(completions, host.Name)
}
}
return completions, cobra.ShellCompDirectiveNoFileComp
},
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {