From 8828f0f5e0fcec87eb920239773abb04f6bf724c Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 12 Jul 2024 17:00:00 -0500 Subject: [PATCH] chore(cmd): Add consts for shells --- cmd/cmd.go | 2 +- cmd/completion.go | 19 +++++++++++++++---- docs/cli-of-life.md | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index d8515e7..6a14d9a 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -24,7 +24,7 @@ func New() *cobra.Command { cmd.Flags().StringP(config.FileFlag, "f", "", "Loads a pattern file on startup") cmd.Flags().String(config.FileFormatFlag, "auto", "File format (one of: "+strings.Join(pattern.FormatStrings(), ", ")+")") - cmd.Flags().String(config.CompletionFlag, "", "Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.") + cmd.Flags().String(config.CompletionFlag, "", "Output command-line completion code for the specified shell (one of: "+strings.Join(shells(), ", ")+")") if err := errors.Join( cmd.RegisterFlagCompletionFunc(config.FileFlag, diff --git a/cmd/completion.go b/cmd/completion.go index f77c84a..a9483eb 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -7,17 +7,28 @@ import ( "github.com/spf13/cobra" ) +const ( + ShellBash = "bash" + ShellZsh = "zsh" + ShellFish = "fish" + ShellPowerShell = "powershell" +) + +func shells() []string { + return []string{ShellBash, ShellZsh, ShellFish, ShellPowerShell} +} + var ErrInvalidShell = errors.New("invalid shell") func completion(cmd *cobra.Command, shell string) error { switch shell { - case "bash": + case ShellBash: return cmd.Root().GenBashCompletion(cmd.OutOrStdout()) - case "zsh": + case ShellZsh: return cmd.Root().GenZshCompletion(cmd.OutOrStdout()) - case "fish": + case ShellFish: return cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true) - case "powershell": + case ShellPowerShell: return cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout()) default: return fmt.Errorf("%w: %s", ErrInvalidShell, shell) diff --git a/docs/cli-of-life.md b/docs/cli-of-life.md index d49281d..decb657 100644 --- a/docs/cli-of-life.md +++ b/docs/cli-of-life.md @@ -9,7 +9,7 @@ cli-of-life [flags] ### Options ``` - --completion string Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'. + --completion string Output command-line completion code for the specified shell (one of: bash, zsh, fish, powershell) -f, --file string Loads a pattern file on startup --file-format string File format (one of: auto, rle, plaintext) (default "auto") -h, --help help for cli-of-life