feat: show subcommands after hostnames in shell completion
Release / release (push) Successful in 48s

Use ShellCompDirectiveKeepOrder so fish/zsh/bash preserve the returned
order instead of sorting alphabetically. ValidArgsFunction now appends
subcommand names (with their short descriptions) after all hostname
entries, so hosts always appear first in the completion list.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Sebastian Unterschütz
2026-05-23 14:42:41 +02:00
parent 574c4dbf58
commit cdf750081e
+11 -5
View File
@@ -199,12 +199,18 @@ func rootCmd() *cobra.Command {
} }
c := cache.New(cfg.Cache.Path, cfg.Cache.TTL) c := cache.New(cfg.Cache.Path, cfg.Cache.TTL)
_ = c.Load() _ = c.Load()
entries := c.Search(toComplete)
names := make([]cobra.Completion, len(entries)) var completions []cobra.Completion
for i, e := range entries { for _, e := range c.Search(toComplete) {
names[i] = cobra.Completion(e.Name) completions = append(completions, cobra.Completion(e.Name))
} }
return names, cobra.ShellCompDirectiveNoFileComp // Subcommands at the end, after all hostnames.
for _, sub := range cmd.Commands() {
if sub.IsAvailableCommand() && strings.HasPrefix(sub.Name(), toComplete) {
completions = append(completions, cobra.Completion(sub.Name()+"\t"+sub.Short))
}
}
return completions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}, },
} }