Skip to content

Commit

Permalink
fixes(issue knative#111) generically for all command groups
Browse files Browse the repository at this point in the history
Explores all sub-commands from root and adds a RunE for all
commands that are groups with child commands and without a RunE.
The added RunE will return the correct errors when the command
is called with empty sub-command or with an unknown sub-command.
It will also print the command help message first.
  • Loading branch information
maximilien committed Jul 1, 2019
1 parent 7f8fdf7 commit 9828fa2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 155 deletions.
11 changes: 0 additions & 11 deletions pkg/kn/commands/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package revision

import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -26,14 +23,6 @@ func NewRevisionCommand(p *commands.KnParams) *cobra.Command {
revisionCmd := &cobra.Command{
Use: "revision",
Short: "Revision command group",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
if len(args) == 0 {
return errors.New("please provide a valid sub-command for \"kn revision\"")
} else {
return errors.New(fmt.Sprintf("unknown command \"%s\" for \"kn revision\"", args[0]))
}
},
}
revisionCmd.AddCommand(NewRevisionListCommand(p))
revisionCmd.AddCommand(NewRevisionDescribeCommand(p))
Expand Down
66 changes: 0 additions & 66 deletions pkg/kn/commands/revision/revision_test.go

This file was deleted.

12 changes: 0 additions & 12 deletions pkg/kn/commands/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package service

import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -26,15 +23,6 @@ func NewServiceCommand(p *commands.KnParams) *cobra.Command {
serviceCmd := &cobra.Command{
Use: "service",
Short: "Service command group",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
if len(args) == 0 {
return errors.New("please provide a valid sub-command for \"kn service\"")
} else {
return errors.New(fmt.Sprintf("unknown command \"%s\" for \"kn service\"", args[0]))
}
return nil
},
}
serviceCmd.AddCommand(NewServiceListCommand(p))
serviceCmd.AddCommand(NewServiceDescribeCommand(p))
Expand Down
66 changes: 0 additions & 66 deletions pkg/kn/commands/service/service_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core

import (
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -71,11 +72,33 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`,
rootCmd.AddCommand(commands.NewCompletionCommand(p))
rootCmd.AddCommand(commands.NewVersionCommand(p))

// Deal with empty and unknown sub command groups
emptyAndUnknownSubcommands(rootCmd)

// For glog parse error.
flag.CommandLine.Parse([]string{})
return rootCmd
}

// emptyAndUnknownSubcommands adds a RunE to all commands that are groups to
// deal with errors when called with empty or unknown sub command
func emptyAndUnknownSubcommands(cmd *cobra.Command) {
for _, childCmd := range cmd.Commands() {
if childCmd.HasSubCommands() && childCmd.RunE == nil {
childCmd.RunE = func(aCmd *cobra.Command, args []string) error {
aCmd.Help()
if len(args) == 0 {
return errors.New(fmt.Sprintf("please provide a valid sub-command for \"kn %s\"", aCmd.Name()))
} else {
return errors.New(fmt.Sprintf("unknown command \"%s\" for \"kn %s\"", args[0], aCmd.Name()))
}
}
// recurse to deal with child commands that are themselves command groups
emptyAndUnknownSubcommands(childCmd)
}
}
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if commands.CfgFile != "" {
Expand Down

0 comments on commit 9828fa2

Please sign in to comment.