diff --git a/pkg/kn/commands/plugin/handler.go b/pkg/kn/commands/plugin/handler.go index 5f8b86a065..fb30a6f1ca 100644 --- a/pkg/kn/commands/plugin/handler.go +++ b/pkg/kn/commands/plugin/handler.go @@ -15,7 +15,6 @@ package plugin import ( - "errors" "fmt" "os" "os/exec" @@ -127,10 +126,11 @@ func HandlePluginCommand(pluginHandler PluginHandler, cmdArgs []string) error { remainingArgs := []string{} for idx := range cmdArgs { - if strings.HasPrefix(cmdArgs[idx], "-") { + cmdArg := cmdArgs[idx] + if strings.HasPrefix(cmdArg, "-") { continue } - remainingArgs = append(remainingArgs, strings.Replace(cmdArgs[idx], "-", "_", -1)) + remainingArgs = append(remainingArgs, strings.Replace(cmdArg, "-", "_", -1)) } foundBinaryPath := "" @@ -147,16 +147,14 @@ func HandlePluginCommand(pluginHandler PluginHandler, cmdArgs []string) error { break } - if len(foundBinaryPath) == 0 { - return errors.New("Could not find plugin to execute") - } - // invoke cmd binary relaying the current environment and args given // remainingArgs will always have at least one element. // execve will make remainingArgs[0] the "binary name". - err := pluginHandler.Execute(foundBinaryPath, append([]string{foundBinaryPath}, cmdArgs[len(remainingArgs):]...), os.Environ()) - if err != nil { - return err + if len(foundBinaryPath) != 0 { + err := pluginHandler.Execute(foundBinaryPath, append([]string{foundBinaryPath}, cmdArgs[len(remainingArgs):]...), os.Environ()) + if err != nil { + return err + } } return nil diff --git a/pkg/kn/commands/plugin/handler_test.go b/pkg/kn/commands/plugin/handler_test.go index 5757a7eb3c..27086a36eb 100644 --- a/pkg/kn/commands/plugin/handler_test.go +++ b/pkg/kn/commands/plugin/handler_test.go @@ -165,6 +165,14 @@ func TestPluginHandler(t *testing.T) { err = HandlePluginCommand(tPluginHandler, []string{"bogus"}) assert.Assert(t, err != nil, fmt.Sprintf("test plugin %s expected to fail executing", "bogus")) }) + + t.Run("doesn't return error with -h", func(t *testing.T) { + setup(t) + defer cleanup(t) + + err = HandlePluginCommand(tPluginHandler, []string{"source", "-h"}) + assert.Assert(t, err == nil, fmt.Sprintf("test plugin command with -h failed executing: %s", err.Error())) + }) }) } diff --git a/pkg/kn/core/root.go b/pkg/kn/core/root.go index 1693fcef03..d614283d42 100644 --- a/pkg/kn/core/root.go +++ b/pkg/kn/core/root.go @@ -87,7 +87,8 @@ func NewDefaultKnCommandWithArgs(rootCmd *cobra.Command, fmt.Fprintf(rootCmd.OutOrStderr(), "Error: unknown command '%s' \nRun 'kn --help' for usage.\n", args[1]) os.Exit(1) } - } else if foundCmd.HasSubCommands() { + } + if foundCmd.HasSubCommands() { if _, _, err := rootCmd.Find(innerArgs); err != nil { fmt.Fprintf(rootCmd.OutOrStderr(), showSubcommands(foundCmd, cmdPathPieces, innerArgs[0])) os.Exit(1)