Skip to content

Commit

Permalink
Fix misleading error message when verb is bad (#589)
Browse files Browse the repository at this point in the history
* validate sub command verb (#557)

* fix review comments for pr #555

* validate sub commands

* update error message

* use inner args returned by find func

* return appropriate error message for #557

* return appropriate error message for #557

* Revert "fix review comments for pr #555"

This reverts commit 14ea8d2.

* review comments for pr 589

* misleading error message when verb is bad (#557)

* misleading error message when verb is bad (#557)

* misleading error message when verb is bad (#557)
  • Loading branch information
itsmurugappan authored and knative-prow-robot committed Jan 20, 2020
1 parent f26fdb4 commit de7e388
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ func NewDefaultKnCommandWithArgs(rootCmd *cobra.Command,
if pluginHandler == nil {
return rootCmd
}

if len(args) > 1 {
cmdPathPieces := args[1:]
cmdPathPieces = removeKnPluginFlags(cmdPathPieces) // Plugin does not need these flags

// only look for suitable extension executables if
// the specified command does not already exist
if _, _, err := rootCmd.Find(cmdPathPieces); err != nil {
foundCmd, innerArgs, err := rootCmd.Find(cmdPathPieces)
if err != nil {
err := plugin.HandlePluginCommand(pluginHandler, cmdPathPieces)
if err != nil {
rootCmd.Help()
fmt.Fprintf(rootCmd.OutOrStderr(), "Unknown command or plugin '%s'.\n", args[1])
fmt.Fprintf(rootCmd.OutOrStderr(), "Error: unknown command '%s' \nRun 'kn --help' for usage.\n", args[1])
os.Exit(1)
}
} else if foundCmd.HasSubCommands() {
if _, _, err := rootCmd.Find(innerArgs); err != nil {
fmt.Fprintf(rootCmd.OutOrStderr(), showSubcommands(foundCmd, cmdPathPieces, innerArgs[0]))
os.Exit(1)
}
}
Expand Down Expand Up @@ -288,3 +292,22 @@ func width() (int, error) {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
return width, err
}

func getCommands(args []string, innerArg string) string {
commands := []string{"kn"}
for _, arg := range args {
if arg == innerArg {
return strings.Join(commands, " ")
}
commands = append(commands, arg)
}
return ""
}

func showSubcommands(cmd *cobra.Command, args []string, innerArg string) string {
var strs []string
for _, subcmd := range cmd.Commands() {
strs = append(strs, subcmd.Name())
}
return fmt.Sprintf("Error: unknown subcommand '%s' for '%s'. Available subcommands: %s\nRun 'kn --help' for usage.\n", innerArg, getCommands(args, innerArg), strings.Join(strs, ", "))
}
16 changes: 16 additions & 0 deletions test/e2e/basic_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func TestBasicWorkflow(t *testing.T) {
t.Run("return no service after completing tests", func(t *testing.T) {
test.serviceListEmpty(t)
})
t.Run("error out of wrong commands and subcommands", func(t *testing.T) {
test.wrongSubCommand(t)
test.wrongCommand(t)
})
}

func (test *e2eTest) serviceListEmpty(t *testing.T) {
Expand Down Expand Up @@ -136,3 +140,15 @@ func (test *e2eTest) revisionDescribe(t *testing.T, serviceName string) {
assert.NilError(t, err)
assert.Check(t, util.ContainsAll(out, revName, test.kn.namespace, serviceName, "++ Ready", "TARGET=kn"))
}

func (test *e2eTest) wrongSubCommand(t *testing.T) {

_, err := test.kn.RunWithOpts([]string{"source", "apiserver", "noverb", "--tag=0.13"}, runOpts{AllowError: true})
assert.ErrorContains(t, err, "Error: unknown subcommand 'noverb' for 'kn source apiserver'. Available subcommands: create, delete, describe, list, update")
}

func (test *e2eTest) wrongCommand(t *testing.T) {

_, err := test.kn.RunWithOpts([]string{"rev"}, runOpts{AllowError: true})
assert.ErrorContains(t, err, "Error: unknown command 'rev'", "Run 'kn --help' for usage.")
}

0 comments on commit de7e388

Please sign in to comment.