From c7f42bfcf4dca1dddfe30456bba5eafc7f8cc5cd Mon Sep 17 00:00:00 2001 From: Alexander Hughes Date: Mon, 10 Feb 2020 16:09:47 -0500 Subject: [PATCH] [#10] Perform early validation of completion cmd In the completion command validation was being done at runtime to determine if too many or too few args were being passed in. This approach required extra unit tests to validate the behavior. This patch seeks to streamline the code by specifying the number of args expected when initializing the cobra command, and allowing cobra to throw an error before doing any work in go if the number of args is incorrect. As a result the unit tests for too many and too few args are no longer needed for this package and have been removed. Change-Id: If0cf043713ef08333f17b010210352205d74c4ee --- cmd/completion/completion.go | 7 +------ cmd/completion/completion_test.go | 12 ------------ 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index bff609942..eaab16acb 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -36,6 +36,7 @@ func NewCompletionCommand() *cobra.Command { Use: "completion SHELL", Short: "Generate autocompletions script for the specified shell (bash or zsh)", Long: completionDesc, + Args: cobra.ExactArgs(1), RunE: runCompletion, ValidArgs: shells, } @@ -44,12 +45,6 @@ func NewCompletionCommand() *cobra.Command { } func runCompletion(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return fmt.Errorf("shell not specified") - } - if len(args) > 1 { - return fmt.Errorf("too many arguments, expected only the shell type") - } run, found := completionShells[args[0]] if !found { return fmt.Errorf("unsupported shell type %q", args[0]) diff --git a/cmd/completion/completion_test.go b/cmd/completion/completion_test.go index 7b56d05c5..0e4791384 100644 --- a/cmd/completion/completion_test.go +++ b/cmd/completion/completion_test.go @@ -22,18 +22,6 @@ func TestCompletion(t *testing.T) { CmdLine: "zsh", Cmd: cmd, }, - { - Name: "completion-no-args", - CmdLine: "", - Cmd: cmd, - Error: errors.New("shell not specified"), - }, - { - Name: "completion-too-many-args", - CmdLine: "bash zsh", - Cmd: cmd, - Error: errors.New("too many arguments, expected only the shell type"), - }, { Name: "completion-unknown-shell", CmdLine: "fish",