feat: shell completion script generation (#22)
This commit is contained in:
@@ -102,6 +102,7 @@ Configuration file format (~/.fence.json):
|
|||||||
rootCmd.Flags().SetInterspersed(true)
|
rootCmd.Flags().SetInterspersed(true)
|
||||||
|
|
||||||
rootCmd.AddCommand(newImportCmd())
|
rootCmd.AddCommand(newImportCmd())
|
||||||
|
rootCmd.AddCommand(newCompletionCmd(rootCmd))
|
||||||
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||||
@@ -391,6 +392,51 @@ Examples:
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newCompletionCmd creates the completion subcommand for shell completions.
|
||||||
|
func newCompletionCmd(rootCmd *cobra.Command) *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "completion [bash|zsh|fish|powershell]",
|
||||||
|
Short: "Generate shell completion scripts",
|
||||||
|
Long: `Generate shell completion scripts for fence.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
# Bash (load in current session)
|
||||||
|
source <(fence completion bash)
|
||||||
|
|
||||||
|
# Zsh (load in current session)
|
||||||
|
source <(fence completion zsh)
|
||||||
|
|
||||||
|
# Fish (load in current session)
|
||||||
|
fence completion fish | source
|
||||||
|
|
||||||
|
# PowerShell (load in current session)
|
||||||
|
fence completion powershell | Out-String | Invoke-Expression
|
||||||
|
|
||||||
|
To persist completions, redirect output to the appropriate completions
|
||||||
|
directory for your shell (e.g., /etc/bash_completion.d/ for bash,
|
||||||
|
${fpath[1]}/_fence for zsh, ~/.config/fish/completions/fence.fish for fish).
|
||||||
|
`,
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
switch args[0] {
|
||||||
|
case "bash":
|
||||||
|
return rootCmd.GenBashCompletionV2(os.Stdout, true)
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
// printTemplates prints all available templates to stdout.
|
// printTemplates prints all available templates to stdout.
|
||||||
func printTemplates() {
|
func printTemplates() {
|
||||||
fmt.Println("Available templates:")
|
fmt.Println("Available templates:")
|
||||||
|
|||||||
Reference in New Issue
Block a user