From df0c9d7b7e9e5ea2307e74bb36498f11467b549c Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 3 Apr 2026 09:44:27 +0300 Subject: [PATCH] feat: support wand file shell completion --- cmd/completion.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ 2 files changed, 51 insertions(+) create mode 100644 cmd/completion.go diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 0000000..af90d82 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +func newCompletionCmd(rootCmd *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate shell completion script", + Long: `Generate a shell completion script for wand. +Use --name to generate completions for an alias: + + # Source completions for an alias directly + source <(wand --wand-file ~/.config/wand/nextcloud.yml completion --name wand-nc zsh) + + # Or write to a file + wand --wand-file ~/.config/wand/nextcloud.yml completion --name wand-nc zsh > _wand-nc`, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + DisableFlagsInUseLine: true, + RunE: func(cmd *cobra.Command, args []string) error { + name, _ := cmd.Flags().GetString("name") + if name != "" { + rootCmd.Use = name + } + + switch args[0] { + case "bash": + return rootCmd.GenBashCompletion(os.Stdout) + case "zsh": + return rootCmd.GenZshCompletion(os.Stdout) + case "fish": + return rootCmd.GenFishCompletion(os.Stdout, true) + case "powershell": + return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) + default: + return fmt.Errorf("unsupported shell: %s", args[0]) + } + }, + } + + cmd.Flags().String("name", "", "command name to use in the completion script (for aliases)") + + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index 4435c35..b3bf877 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,6 +40,8 @@ func Execute() error { }, ) + rootCmd.AddCommand(newCompletionCmd(rootCmd)) + return rootCmd.Execute() }