From f443b0dd48ea438c17b2789d7abde6c0f18ac536 Mon Sep 17 00:00:00 2001 From: Sebastian Haas Date: Fri, 30 Jul 2021 19:56:38 +0200 Subject: [PATCH] Fix index out of range during shell completion validate number of parameters to prevent panic see https://github.com/spf13/cobra/blob/master/shell_completions.md --- cmd/signaldctl/cmd/completion.go | 49 ++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/signaldctl/cmd/completion.go b/cmd/signaldctl/cmd/completion.go index d4d284b..b162cbd 100644 --- a/cmd/signaldctl/cmd/completion.go +++ b/cmd/signaldctl/cmd/completion.go @@ -30,50 +30,55 @@ var completionCmd = &cobra.Command{ Bash: -$ source <(signaldctl completion bash) + $ source <(signaldctl completion bash) -# To load completions for each session, execute once: -Linux: - $ signaldctl completion bash > /etc/bash_completion.d/signaldctl -MacOS: - $ signaldctl completion bash > /usr/local/etc/bash_completion.d/signaldctl + # To load completions for each session, execute once: + # Linux: + $ signaldctl completion bash > /etc/bash_completion.d/signaldctl + # macOS: + $ signaldctl completion bash > /usr/local/etc/bash_completion.d/signaldctl Zsh: -# If shell completion is not already enabled in your environment you will need -# to enable it. You can execute the following once: + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: -$ echo "autoload -U compinit; compinit" >> ~/.zshrc + $ echo "autoload -U compinit; compinit" >> ~/.zshrc -# To load completions for each session, execute once: -$ signaldctl completion zsh > "${fpath[1]}/_signaldctl" + # To load completions for each session, execute once: + $ signaldctl completion zsh > "${fpath[1]}/_signaldctl" -# You will need to start a new shell for this setup to take effect. + # You will need to start a new shell for this setup to take effect. -Fish: +fish: -$ signaldctl completion fish | source + $ signaldctl completion fish | source -# To load completions for each session, execute once: -$ signaldctl completion fish > ~/.config/fish/completions/signaldctl.fish + # To load completions for each session, execute once: + $ signaldctl completion fish > ~/.config/fish/completions/signaldctl.fish -Powershell: +PowerShell: -PS> signaldctl completion powershell | Out-String | Invoke-Expression + PS> signaldctl completion powershell | Out-String | Invoke-Expression -# To load completions for every new session, run: -PS> signaldctl completion powershell > signaldctl.ps1 -# and source this file from your powershell profile. + # To load completions for every new session, run: + PS> signaldctl completion powershell > signaldctl.ps1 + # and source this file from your PowerShell profile. `, DisableFlagsInUseLine: true, Annotations: map[string]string{common.AnnotationNoSocketConnection: "true"}, - ValidArgs: []string{"bash", "zsh"}, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.ExactValidArgs(1), Run: func(cmd *cobra.Command, args []string) { switch args[0] { case "bash": common.Must(cmd.Root().GenBashCompletion(os.Stdout)) case "zsh": common.Must(cmd.Root().GenZshCompletion(os.Stdout)) + case "fish": + common.Must(cmd.Root().GenFishCompletion(os.Stdout, true)) + case "powershell": + common.Must(cmd.Root().GenPowerShellCompletion(os.Stdout)) } }, }