diff --git a/cmd/netssh/main.go b/cmd/netssh/main.go index 47adbc6..cc7300c 100644 --- a/cmd/netssh/main.go +++ b/cmd/netssh/main.go @@ -208,9 +208,10 @@ func rootCmd() *cobra.Command { }, } - // cobra automatically adds a "completion" subcommand; we extend it with "install". root.AddCommand(configureCmd(), searchCmd(), cacheCmd()) + // cobra builds the "completion" command lazily; force init so we can extend it. + root.InitDefaultCompletionCmd() for _, cmd := range root.Commands() { if cmd.Name() == "completion" { cmd.AddCommand(completionInstallCmd(root)) @@ -244,7 +245,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "netssh") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenBashCompletionV2(&buf, true) + err := root.GenBashCompletionV2(&buf, true) + return []byte(buf.String()), err } note = "Reload your shell or run: source " + file case "zsh": @@ -252,7 +254,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "_netssh") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenZshCompletion(&buf) + err := root.GenZshCompletion(&buf) + return []byte(buf.String()), err } note = "Make sure ~/.zfunc is in your fpath:\n fpath=(~/.zfunc $fpath)\n autoload -Uz compinit && compinit" case "fish": @@ -261,7 +264,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "netssh.fish") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenFishCompletion(&buf, true) + err := root.GenFishCompletion(&buf, true) + return []byte(buf.String()), err } note = "Reload your shell or start a new fish session." default: