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
This commit is contained in:
Sebastian Haas 2021-07-30 19:56:38 +02:00 committed by Finn
parent 86f23e72c9
commit f443b0dd48

View file

@ -30,50 +30,55 @@ var completionCmd = &cobra.Command{
Bash: Bash:
$ source <(signaldctl completion bash) $ source <(signaldctl completion bash)
# To load completions for each session, execute once: # To load completions for each session, execute once:
Linux: # Linux:
$ signaldctl completion bash > /etc/bash_completion.d/signaldctl $ signaldctl completion bash > /etc/bash_completion.d/signaldctl
MacOS: # macOS:
$ signaldctl completion bash > /usr/local/etc/bash_completion.d/signaldctl $ signaldctl completion bash > /usr/local/etc/bash_completion.d/signaldctl
Zsh: Zsh:
# If shell completion is not already enabled in your environment you will need # If shell completion is not already enabled in your environment,
# to enable it. You can execute the following once: # 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: # To load completions for each session, execute once:
$ signaldctl completion zsh > "${fpath[1]}/_signaldctl" $ 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: # To load completions for each session, execute once:
$ signaldctl completion fish > ~/.config/fish/completions/signaldctl.fish $ 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: # To load completions for every new session, run:
PS> signaldctl completion powershell > signaldctl.ps1 PS> signaldctl completion powershell > signaldctl.ps1
# and source this file from your powershell profile. # and source this file from your PowerShell profile.
`, `,
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Annotations: map[string]string{common.AnnotationNoSocketConnection: "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) { Run: func(cmd *cobra.Command, args []string) {
switch args[0] { switch args[0] {
case "bash": case "bash":
common.Must(cmd.Root().GenBashCompletion(os.Stdout)) common.Must(cmd.Root().GenBashCompletion(os.Stdout))
case "zsh": case "zsh":
common.Must(cmd.Root().GenZshCompletion(os.Stdout)) 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))
} }
}, },
} }