From 309cd2364468a5d033342b17b2a4c2729cfac993 Mon Sep 17 00:00:00 2001 From: leggetter Date: Wed, 3 Jun 2026 19:54:56 +0000 Subject: [PATCH] feat(completion): output completion script to stdout --- .goreleaser/mac.yml | 2 +- REFERENCE.md | 50 ++++++++++++++++++--- pkg/cmd/completion.go | 100 ++++++++++++----------------------------- scripts/completions.sh | 11 ++--- 4 files changed, 78 insertions(+), 85 deletions(-) diff --git a/.goreleaser/mac.yml b/.goreleaser/mac.yml index 24b8d046..67060b12 100644 --- a/.goreleaser/mac.yml +++ b/.goreleaser/mac.yml @@ -13,7 +13,7 @@ builds: - -s -w -X github.com/hookdeck/hookdeck-cli/pkg/version.Version={{.Version}} binary: hookdeck env: - - CGO_ENABLED=1 + - CGO_ENABLED=0 main: ./main.go goos: - darwin diff --git a/REFERENCE.md b/REFERENCE.md index 4c268fe7..b8cc6b86 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -694,14 +694,24 @@ The connection will queue incoming events until unpaused. **Usage:** ```bash -hookdeck gateway connection pause +hookdeck gateway connection pause ``` **Arguments:** | Argument | Type | Description | |----------|------|-------------| -| `connection-id` | `string` | **Required.** Connection ID | +| `connection-id-or-name` | `string` | **Required.** Connection ID or name | + +**Examples:** + +```bash +# Pause by connection ID +hookdeck gateway connection pause web_abc123 + +# Pause by connection name +hookdeck gateway connection pause my-connection +``` ### hookdeck gateway connection unpause Resume a paused connection. @@ -711,14 +721,24 @@ The connection will start processing queued events. **Usage:** ```bash -hookdeck gateway connection unpause +hookdeck gateway connection unpause ``` **Arguments:** | Argument | Type | Description | |----------|------|-------------| -| `connection-id` | `string` | **Required.** Connection ID | +| `connection-id-or-name` | `string` | **Required.** Connection ID or name | + +**Examples:** + +```bash +# Unpause by connection ID +hookdeck gateway connection unpause web_abc123 + +# Unpause by connection name +hookdeck gateway connection unpause my-connection +``` ## Sources @@ -1872,7 +1892,26 @@ Query Event Gateway metrics (events, requests, attempts, queue depth, pending ev ## Completion -Generate bash and zsh completion scripts. This command runs on install when using Homebrew or Scoop. You can optionally run it when using binaries directly or without a package manager. +Generate bash and zsh completion scripts. + +The completion script is written to stdout. Source it directly in your current +shell session, or redirect it to a file loaded by your shell's startup config. + +To load completions in your current session: + + source <(hookdeck completion `--shell` bash) + source <(hookdeck completion `--shell` zsh) + +To load completions for every session, write the script to the location your +shell loads completions from, for example: + + # bash (Linux) + hookdeck completion `--shell` bash > /etc/bash_completion.d/hookdeck + + # zsh + hookdeck completion `--shell` zsh > "${fpath[1]}/_hookdeck" + +This command also runs on install when using Homebrew or Scoop. **Usage:** @@ -1891,6 +1930,7 @@ hookdeck completion [flags] ```bash $ hookdeck completion --shell zsh $ hookdeck completion --shell bash +$ source <(hookdeck completion --shell bash) ``` ## CI diff --git a/pkg/cmd/completion.go b/pkg/cmd/completion.go index d17293dc..1f503ab9 100644 --- a/pkg/cmd/completion.go +++ b/pkg/cmd/completion.go @@ -2,13 +2,12 @@ package cmd import ( "fmt" + "io" "os" "strings" "github.com/spf13/cobra" - "runtime" - "github.com/hookdeck/hookdeck-cli/pkg/validators" ) @@ -24,70 +23,41 @@ func newCompletionCmd() *completionCmd { cc.cmd = &cobra.Command{ Use: "completion", Short: "Generate bash and zsh completion scripts", - Long: "Generate bash and zsh completion scripts. This command runs on install when using Homebrew or Scoop. You can optionally run it when using binaries directly or without a package manager.", - Args: validators.NoArgs, - Example: ` $ hookdeck completion --shell zsh - $ hookdeck completion --shell bash`, - RunE: func(cmd *cobra.Command, args []string) error { - return selectShell(cc.shell) - }, - } - - cc.cmd.Flags().StringVar(&cc.shell, "shell", "", "The shell to generate completion commands for. Supports \"bash\" or \"zsh\"") - - return cc -} - -const ( - instructionsHeader = ` -Suggested next steps: ----------------------` + Long: `Generate bash and zsh completion scripts. - zshCompletionInstructions = ` -1. Move ` + "`hookdeck-completion.zsh`" + ` to the correct location: - mkdir -p ~/.hookdeck - mv hookdeck-completion.zsh ~/.hookdeck +The completion script is written to stdout. Source it directly in your current +shell session, or redirect it to a file loaded by your shell's startup config. -2. Add the following lines to your ` + "`.zshrc`" + ` enabling shell completion for Hookdeck: - fpath=(~/.hookdeck $fpath) - autoload -Uz compinit && compinit -i +To load completions in your current session: -3. Source your ` + "`.zshrc`" + ` or open a new terminal session: - source ~/.zshrc` + source <(hookdeck completion --shell bash) + source <(hookdeck completion --shell zsh) - bashCompletionInstructionsMac = ` -Set up bash autocompletion on your system: -1. Install the bash autocompletion package: - brew install bash-completion -2. Follow the post-install instructions displayed by Homebrew; add a line like the following to your bash profile: - [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" +To load completions for every session, write the script to the location your +shell loads completions from, for example: -Set up Hookdeck autocompletion: -3. Move ` + "`hookdeck-completion.bash`" + ` to the correct location: - mkdir -p ~/.hookdeck - mv hookdeck-completion.bash ~/.hookdeck + # bash (Linux) + hookdeck completion --shell bash > /etc/bash_completion.d/hookdeck -4. Add the following line to your bash profile, so that Hookdeck autocompletion will be enabled every time you start a new terminal session: - source ~/.hookdeck/hookdeck-completion.bash + # zsh + hookdeck completion --shell zsh > "${fpath[1]}/_hookdeck" -5. Either restart your terminal, or run the following command in your current session to enable immediately: - source ~/.hookdeck/hookdeck-completion.bash` - - bashCompletionInstructionsLinux = ` -1. Ensure bash autocompletion is installed on your system. Often, this means verifying that ` + "`/etc/profile.d/bash_completion.sh`" + ` exists, and is sourced by your bash profile; the location of this file varies across distributions of Linux. - -2. Move ` + "`hookdeck-completion.bash`" + ` to the correct location: - mkdir -p ~/.hookdeck - mv hookdeck-completion.bash ~/.hookdeck +This command also runs on install when using Homebrew or Scoop.`, + Args: validators.NoArgs, + Example: ` $ hookdeck completion --shell zsh + $ hookdeck completion --shell bash + $ source <(hookdeck completion --shell bash)`, + RunE: func(cmd *cobra.Command, args []string) error { + return selectShell(cc.shell, cmd.OutOrStdout()) + }, + } -3. Add the following line to your bash profile, so that Hookdeck autocompletion will be enabled every time you start a new terminal session: - source ~/.hookdeck/hookdeck-completion.bash + cc.cmd.Flags().StringVar(&cc.shell, "shell", "", "The shell to generate completion commands for. Supports \"bash\" or \"zsh\"") -4. Either restart your terminal, or run the following command in your current session to enable immediately: - source ~/.hookdeck/hookdeck-completion.bash` -) + return cc +} -func selectShell(shell string) error { +func selectShell(shell string, out io.Writer) error { selected := shell if selected == "" { selected = detectShell() @@ -95,23 +65,9 @@ func selectShell(shell string) error { switch { case selected == "zsh": - fmt.Println("Detected `zsh`, generating zsh completion file: hookdeck-completion.zsh") - err := rootCmd.GenZshCompletionFile("hookdeck-completion.zsh") - if err == nil { - fmt.Printf("%s%s\n", instructionsHeader, zshCompletionInstructions) - } - return err + return rootCmd.GenZshCompletion(out) case selected == "bash": - fmt.Println("Detected `bash`, generating bash completion file: hookdeck-completion.bash") - err := rootCmd.GenBashCompletionFile("hookdeck-completion.bash") - if err == nil { - if runtime.GOOS == "darwin" { - fmt.Printf("%s%s\n", instructionsHeader, bashCompletionInstructionsMac) - } else if runtime.GOOS == "linux" { - fmt.Printf("%s%s\n", instructionsHeader, bashCompletionInstructionsLinux) - } - } - return err + return rootCmd.GenBashCompletion(out) default: return fmt.Errorf("could not automatically detect your shell. Please run the command with the `--shell` flag for either bash or zsh") } diff --git a/scripts/completions.sh b/scripts/completions.sh index e735cc52..7df9ba8c 100755 --- a/scripts/completions.sh +++ b/scripts/completions.sh @@ -10,16 +10,13 @@ mkdir completions # Use 'go run .' to compile and run the CLI to generate completions # This works on any platform that can build Go code -# The completion command writes files to the current directory, so we cd into completions/ +# The completion command writes the script to stdout; redirect it to the +# filenames GoReleaser expects. echo "Generating bash completion..." -(cd completions && go run .. completion --shell bash) +go run . completion --shell bash > completions/hookdeck.bash echo "Generating zsh completion..." -(cd completions && go run .. completion --shell zsh) - -# Rename the generated files to match GoReleaser expectations -mv completions/hookdeck-completion.bash completions/hookdeck.bash -mv completions/hookdeck-completion.zsh completions/_hookdeck +go run . completion --shell zsh > completions/_hookdeck # Fish completion is not currently supported by the CLI # If it gets added in the future, uncomment this: