github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/updater/main.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "runtime" 8 "runtime/debug" 9 "syscall" 10 11 "gopkg.in/src-d/go-git.v4" 12 ) 13 14 func main() { 15 scanner := bufio.NewScanner(os.Stdin) 16 17 // Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows 18 defer func() { 19 if r := recover(); r != nil { 20 fmt.Println(r) 21 debug.PrintStack() 22 pressAnyKey(scanner) 23 return 24 } 25 }() 26 27 updater(scanner) 28 } 29 30 func pressAnyKey(scanner *bufio.Scanner) { 31 fmt.Println("Please press enter to exit...") 32 for scanner.Scan() { 33 _ = scanner.Text() 34 return 35 } 36 } 37 38 // The bool return is a little trick to condense two lines onto one 39 func logError(err error) bool { 40 if err == nil { 41 return true 42 } 43 fmt.Println(err) 44 debug.PrintStack() 45 return false 46 } 47 48 func updater(scanner *bufio.Scanner) bool { 49 fmt.Println("Welcome to Gosora's Upgrader") 50 fmt.Println("We're going to check for new updates, please wait patiently") 51 52 repo, err := git.PlainOpen(".") 53 if err != nil { 54 return logError(err) 55 } 56 57 workTree, err := repo.Worktree() 58 if err != nil { 59 return logError(err) 60 } 61 62 err = workTree.Pull(&git.PullOptions{Force: true}) 63 if err == git.NoErrAlreadyUpToDate { 64 fmt.Println("You are already up-to-date") 65 return true 66 } else if err != nil && err != git.ErrUnstagedChanges { // fixes a bug in git where it refuses to update the files 67 return logError(err) 68 } 69 70 err = workTree.Reset(&git.ResetOptions{Mode: git.HardReset}) 71 if err != nil { 72 return logError(err) 73 } 74 75 fmt.Println("Updated to the latest commit") 76 headRef, err := repo.Head() 77 if err != nil { 78 return logError(err) 79 } 80 81 // Get information about the commit 82 commit, err := repo.CommitObject(headRef.Hash()) 83 if err != nil { 84 return logError(err) 85 } 86 fmt.Println("Commit details:", commit) 87 88 switch runtime.GOOS { 89 case "windows": 90 err = syscall.Exec("./patcher.bat", []string{}, os.Environ()) // doesn't work, need something for windows 91 default: //linux, etc. 92 err = syscall.Exec("./patcher-linux", []string{}, os.Environ()) 93 } 94 return logError(err) 95 }