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>
This commit is contained in:
+8
-4
@@ -208,9 +208,10 @@ func rootCmd() *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// cobra automatically adds a "completion" subcommand; we extend it with "install".
|
|
||||||
root.AddCommand(configureCmd(), searchCmd(), cacheCmd())
|
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() {
|
for _, cmd := range root.Commands() {
|
||||||
if cmd.Name() == "completion" {
|
if cmd.Name() == "completion" {
|
||||||
cmd.AddCommand(completionInstallCmd(root))
|
cmd.AddCommand(completionInstallCmd(root))
|
||||||
@@ -244,7 +245,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command {
|
|||||||
file = filepath.Join(dir, "netssh")
|
file = filepath.Join(dir, "netssh")
|
||||||
gen = func() ([]byte, error) {
|
gen = func() ([]byte, error) {
|
||||||
var buf strings.Builder
|
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
|
note = "Reload your shell or run: source " + file
|
||||||
case "zsh":
|
case "zsh":
|
||||||
@@ -252,7 +254,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command {
|
|||||||
file = filepath.Join(dir, "_netssh")
|
file = filepath.Join(dir, "_netssh")
|
||||||
gen = func() ([]byte, error) {
|
gen = func() ([]byte, error) {
|
||||||
var buf strings.Builder
|
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"
|
note = "Make sure ~/.zfunc is in your fpath:\n fpath=(~/.zfunc $fpath)\n autoload -Uz compinit && compinit"
|
||||||
case "fish":
|
case "fish":
|
||||||
@@ -261,7 +264,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command {
|
|||||||
file = filepath.Join(dir, "netssh.fish")
|
file = filepath.Join(dir, "netssh.fish")
|
||||||
gen = func() ([]byte, error) {
|
gen = func() ([]byte, error) {
|
||||||
var buf strings.Builder
|
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."
|
note = "Reload your shell or start a new fish session."
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user