mirror of
https://github.com/chenasraf/wand.git
synced 2026-05-17 17:38:02 +00:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
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
|
|
}
|