feat: support wand file shell completion

This commit is contained in:
2026-04-03 09:44:27 +03:00
parent 31c5027f81
commit df0c9d7b7e
2 changed files with 51 additions and 0 deletions

49
cmd/completion.go Normal file
View File

@@ -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
}

View File

@@ -40,6 +40,8 @@ func Execute() error {
},
)
rootCmd.AddCommand(newCompletionCmd(rootCmd))
return rootCmd.Execute()
}