1 Commits

Author SHA1 Message Date
Sebastian Unterschütz 574c4dbf58 fix: completion install writes non-empty scripts
Release / release (push) Successful in 47s
GenBashCompletionV2/GenZshCompletion/GenFishCompletion write into the
buffer as a side effect; capturing buf.String() in the return statement
before the Gen* call runs means the buffer is always empty. Separate
the call from the return to fix evaluation order.

Also call InitDefaultCompletionCmd() before iterating root.Commands()
so the lazily-initialized completion subtree is visible before Execute().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 13:50:53 +02:00
+8 -4
View File
@@ -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: