From 2b47a4d9f09873b75e55ea829b2ad282819cce3d Mon Sep 17 00:00:00 2001 From: morganamilo Date: Wed, 3 Jan 2018 05:55:12 +0000 Subject: [PATCH] Refactor away os.Exit Try to minimise the useage of os.Exit Apart from init, os.Exit is only used once as the final function call. Now we can ensure there are no random exits hidding in the code. We can also allow part of the code to error and continue on while also remembering that we did error and return non 0 when we finally do reach the os.Exit. This comes in very handy for trying to save the vcs info after an error and ensuring that alpmHandle.Release is always called. --- cmd.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/cmd.go b/cmd.go index d3df1ba1..4113d82a 100644 --- a/cmd.go +++ b/cmd.go @@ -142,6 +142,18 @@ func init() { } func main() { + status := run() + + err := alpmHandle.Release() + if err != nil { + fmt.Println(err) + status = 1 + } + + os.Exit(status) +} + +func run() (status int) { var err error var changedConfig bool @@ -150,7 +162,8 @@ func main() { if err != nil { fmt.Println(err) - os.Exit(1) + status = 1 + return } if parser.existsArg("-") { @@ -158,16 +171,17 @@ func main() { if err != nil { fmt.Println(err) - os.Exit(1) + status = 1 + return } } - fmt.Println(parser) - changedConfig, err = handleCmd(parser) if err != nil { fmt.Println(err) + status = 1 + //try continue onward } if updated { @@ -175,6 +189,7 @@ func main() { if err != nil { fmt.Println(err) + status = 1 } } @@ -183,16 +198,16 @@ func main() { if err != nil { fmt.Println(err) + status = 1 } } - - err = alpmHandle.Release() - if err != nil { - fmt.Println(err) - } + + return + } + func handleCmd(parser *argParser) (changedConfig bool, err error) { var _changedConfig bool