mirror of
https://github.com/Jguer/yay.git
synced 2024-11-06 09:07:21 +01:00
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:
parent
765b767333
commit
2b47a4d9f0
33
cmd.go
33
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user