From 93243e75e1c37b9ae0f5969ceaeba3b1c9fb371d Mon Sep 17 00:00:00 2001 From: JY Tan Date: Sun, 25 Jan 2026 10:57:22 -0800 Subject: [PATCH] feat: shell completion script generation (#22) --- cmd/fence/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/cmd/fence/main.go b/cmd/fence/main.go index c806bb3..7ce6969 100644 --- a/cmd/fence/main.go +++ b/cmd/fence/main.go @@ -102,6 +102,7 @@ Configuration file format (~/.fence.json): rootCmd.Flags().SetInterspersed(true) rootCmd.AddCommand(newImportCmd()) + rootCmd.AddCommand(newCompletionCmd(rootCmd)) if err := rootCmd.Execute(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) @@ -391,6 +392,51 @@ Examples: 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. func printTemplates() { fmt.Println("Available templates:")