mirror of
https://github.com/chenasraf/wand.git
synced 2026-05-17 17:38:02 +00:00
feat: support wand file shell completion
This commit is contained in:
49
cmd/completion.go
Normal file
49
cmd/completion.go
Normal 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
|
||||
}
|
||||
@@ -40,6 +40,8 @@ func Execute() error {
|
||||
},
|
||||
)
|
||||
|
||||
rootCmd.AddCommand(newCompletionCmd(rootCmd))
|
||||
|
||||
return rootCmd.Execute()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user