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.
This commit is contained in:
morganamilo 2018-01-03 05:55:12 +00:00
parent 765b767333
commit 2b47a4d9f0
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E

33
cmd.go
View file

@ -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