Skip to content

Commit

Permalink
do not check for plugin executable if none on path
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand committed May 19, 2020
1 parent 0df54bc commit 287c591
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
18 changes: 8 additions & 10 deletions pkg/kn/commands/plugin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package plugin

import (
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/kn/commands/plugin/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
})
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 287c591

Please sign in to comment.