From 955267993934d057ed4c6e9785a446c2dbf012da Mon Sep 17 00:00:00 2001 From: Bruce Downs Date: Mon, 29 Jul 2019 23:34:42 -0700 Subject: [PATCH] Add idiomatic handling of go error in distinct main func --- cobra/cmd/root.go | 4 ++-- cobra/main.go | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 624c717..97f404b 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -36,8 +36,8 @@ to quickly create a Cobra application.`, ) // Execute executes the root command. -func Execute() { - rootCmd.Execute() +func Execute() error { + return rootCmd.Execute() } func init() { diff --git a/cobra/main.go b/cobra/main.go index c3a9d9c..8add69a 100644 --- a/cobra/main.go +++ b/cobra/main.go @@ -13,8 +13,18 @@ package main -import "github.com/spf13/cobra/cobra/cmd" +import ( + "os" + + "github.com/spf13/cobra/cobra/cmd" +) func main() { - cmd.Execute() + if err := runMain(); err != nil { + os.Exit(1) + } +} + +func runMain() error { + return cmd.Execute() }