Add idiomatic handling of go error in distinct main func

This commit is contained in:
Bruce Downs 2019-07-29 23:34:42 -07:00 committed by Albert Nigmatzianov
parent af29f95b81
commit 9552679939
2 changed files with 14 additions and 4 deletions

View file

@ -36,8 +36,8 @@ to quickly create a Cobra application.`,
) )
// Execute executes the root command. // Execute executes the root command.
func Execute() { func Execute() error {
rootCmd.Execute() return rootCmd.Execute()
} }
func init() { func init() {

View file

@ -13,8 +13,18 @@
package main package main
import "github.com/spf13/cobra/cobra/cmd" import (
"os"
"github.com/spf13/cobra/cobra/cmd"
)
func main() { func main() {
cmd.Execute() if err := runMain(); err != nil {
os.Exit(1)
}
}
func runMain() error {
return cmd.Execute()
} }