mirror of
https://github.com/Jguer/yay
synced 2024-10-31 04:12:51 +00: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
1 changed files with 24 additions and 9 deletions
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 a new issue