Skip to content

Commit

Permalink
handle errors and Exit(1) properly
Browse files Browse the repository at this point in the history
- return exit code 1 instead of -1
- don't print usage on errors
- don't call cmd.Help() on RunE callbacks
  • Loading branch information
neolit123 committed Feb 4, 2019
1 parent fae1161 commit 60932b1
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 36 deletions.
3 changes: 0 additions & 3 deletions cmd/kind/build/build.go
Expand Up @@ -32,9 +32,6 @@ func NewCommand() *cobra.Command {
Use: "build",
Short: "Build one of [base-image, node-image]",
Long: "Build the base node image (base-image) or the node image (node-image)",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// add subcommands
cmd.AddCommand(baseimage.NewCommand())
Expand Down
10 changes: 0 additions & 10 deletions cmd/kind/create/create.go
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
package create

import (
"fmt"

"github.com/spf13/cobra"

createcluster "sigs.k8s.io/kind/cmd/kind/create/cluster"
Expand All @@ -32,15 +30,7 @@ func NewCommand() *cobra.Command {
Use: "create",
Short: "Creates one of [cluster]",
Long: "Creates one of local Kubernetes cluster (cluster)",
RunE: run,
}
cmd.AddCommand(createcluster.NewCommand())
return cmd
}

func run(cmd *cobra.Command, args []string) error {
fmt.Println("You likely want `kind create cluster`, please migrate!")
fmt.Println()
cmd.Usage()
return nil
}
10 changes: 0 additions & 10 deletions cmd/kind/delete/delete.go
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
package delete

import (
"fmt"

"github.com/spf13/cobra"

deletecluster "sigs.k8s.io/kind/cmd/kind/delete/cluster"
Expand All @@ -33,15 +31,7 @@ func NewCommand() *cobra.Command {
Use: "delete",
Short: "Deletes one of [cluster]",
Long: "Deletes one of [cluster]",
RunE: run,
}
cmd.AddCommand(deletecluster.NewCommand())
return cmd
}

func run(cmd *cobra.Command, args []string) error {
fmt.Println("You likely want `kind delete cluster`, please migrate!")
fmt.Println()
cmd.Usage()
return nil
}
3 changes: 0 additions & 3 deletions cmd/kind/export/export.go
Expand Up @@ -31,9 +31,6 @@ func NewCommand() *cobra.Command {
Use: "export",
Short: "exports one of [logs]",
Long: "exports one of [logs]",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// add subcommands
cmd.AddCommand(logs.NewCommand())
Expand Down
3 changes: 0 additions & 3 deletions cmd/kind/get/get.go
Expand Up @@ -32,9 +32,6 @@ func NewCommand() *cobra.Command {
Use: "get",
Short: "Gets one of [clusters, kubeconfig-path]",
Long: "Gets one of [clusters, kubeconfig-path]",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// add subcommands
cmd.AddCommand(clusters.NewCommand())
Expand Down
11 changes: 4 additions & 7 deletions cmd/kind/kind.go
Expand Up @@ -52,10 +52,8 @@ func NewCommand() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return runE(flags, cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Version: version.Version,
SilenceUsage: true,
Version: version.Version,
}
cmd.PersistentFlags().StringVar(
&flags.LogLevel,
Expand Down Expand Up @@ -90,7 +88,7 @@ func Run() error {
return NewCommand().Execute()
}

// Main wraps Run, adding a log.Fatal(err) on error, and setting the log formatter
// Main wraps Run and sets the log formatter
func Main() {
// let's explicitly set stdout
log.SetOutput(os.Stdout)
Expand All @@ -105,7 +103,6 @@ func Main() {
ForceColors: logutil.IsTerminal(log.StandardLogger().Out),
})
if err := Run(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(-1)
os.Exit(1)
}
}

0 comments on commit 60932b1

Please sign in to comment.