From 7485aeab41289bad241406d3f94c09616bf0091e Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 18 May 2022 16:57:48 +0000 Subject: [PATCH] fix: Don't complete _chezmoi in zsh completion --- completions/chezmoi.zsh | 2 +- pkg/cmd/completioncmd.go | 10 +++++++++- pkg/cmd/testdata/scripts/completion.txt | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/completions/chezmoi.zsh b/completions/chezmoi.zsh index 73820e4e274..eb7de626704 100644 --- a/completions/chezmoi.zsh +++ b/completions/chezmoi.zsh @@ -1,4 +1,4 @@ -#compdef _chezmoi chezmoi +#compdef chezmoi # zsh completion for chezmoi -*- shell-script -*- diff --git a/pkg/cmd/completioncmd.go b/pkg/cmd/completioncmd.go index 0b2c67f8899..79a570828d6 100644 --- a/pkg/cmd/completioncmd.go +++ b/pkg/cmd/completioncmd.go @@ -31,27 +31,35 @@ func (c *Config) newCompletionCmd() *cobra.Command { func (c *Config) runCompletionCmd(cmd *cobra.Command, args []string) error { builder := strings.Builder{} builder.Grow(16384) + var output string switch args[0] { case "bash": includeDesc := true if err := cmd.Root().GenBashCompletionV2(&builder, includeDesc); err != nil { return err } + output = builder.String() case "fish": includeDesc := true if err := cmd.Root().GenFishCompletion(&builder, includeDesc); err != nil { return err } + output = builder.String() case "powershell": if err := cmd.Root().GenPowerShellCompletion(&builder); err != nil { return err } + output = builder.String() case "zsh": if err := cmd.Root().GenZshCompletion(&builder); err != nil { return err } + // FIXME remove the following when + // https://github.com/spf13/cobra/pull/1690 is merged and released + output = "#compdef chezmoi\n" + strings.TrimPrefix(builder.String(), "#compdef _chezmoi chezmoi\n") + default: return fmt.Errorf("%s: unsupported shell", args[0]) } - return c.writeOutputString(builder.String()) + return c.writeOutputString(output) } diff --git a/pkg/cmd/testdata/scripts/completion.txt b/pkg/cmd/testdata/scripts/completion.txt index dc430e24562..0beeb0e9b22 100644 --- a/pkg/cmd/testdata/scripts/completion.txt +++ b/pkg/cmd/testdata/scripts/completion.txt @@ -12,4 +12,4 @@ stdout 'Register-ArgumentCompleter' # test that chezmoi completion zsh generates zsh completions chezmoi completion zsh -stdout '#compdef _chezmoi chezmoi' +stdout '#compdef chezmoi'