From 7bc94bfa837c6c3e688a42829575ae0f183eca57 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 4 Apr 2018 08:20:01 +0100 Subject: [PATCH] Improved the error handling logic in the updater. --- updater/main.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/updater/main.go b/updater/main.go index ae6eaed5..05fe063f 100644 --- a/updater/main.go +++ b/updater/main.go @@ -25,10 +25,7 @@ func main() { } }() - err := updater(scanner) - if err != nil { - fmt.Println(err) - } + updater(scanner) } func pressAnyKey(scanner *bufio.Scanner) { @@ -39,34 +36,44 @@ func pressAnyKey(scanner *bufio.Scanner) { } } -func updater(scanner *bufio.Scanner) error { +// The bool return is a little trick to condense two lines onto one +func logError(err error) bool { + if err == nil { + return true + } + fmt.Println(err) + debug.PrintStack() + return false +} + +func updater(scanner *bufio.Scanner) bool { fmt.Println("Welcome to Gosora's Upgrader") fmt.Println("We're going to check for new updates, please wait patiently") repo, err := git.PlainOpen(".") if err != nil { - return err + return logError(err) } workTree, err := repo.Worktree() if err != nil { - return err + return logError(err) } err = workTree.Pull(&git.PullOptions{RemoteName: "origin"}) if err != nil { - return err + return logError(err) } fmt.Println("Updated to the latest commit") headRef, err := repo.Head() if err != nil { - return err + return logError(err) } commit, err := repo.CommitObject(headRef.Hash()) if err != nil { - return err + return logError(err) } fmt.Println("Commit details:", commit) @@ -76,5 +83,5 @@ func updater(scanner *bufio.Scanner) error { default: //linux, etc. err = syscall.Exec("./patcher-linux", []string{}, os.Environ()) } - return err + return logError(err) }