2018-03-21 05:56:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-04-03 04:34:07 +00:00
|
|
|
"runtime"
|
2018-03-21 05:56:33 +00:00
|
|
|
"runtime/debug"
|
2018-04-03 04:34:07 +00:00
|
|
|
"syscall"
|
2018-03-21 05:56:33 +00:00
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
|
|
|
|
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r != nil {
|
|
|
|
fmt.Println(r)
|
|
|
|
debug.PrintStack()
|
|
|
|
pressAnyKey(scanner)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-04-04 07:20:01 +00:00
|
|
|
updater(scanner)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pressAnyKey(scanner *bufio.Scanner) {
|
|
|
|
fmt.Println("Please press enter to exit...")
|
|
|
|
for scanner.Scan() {
|
|
|
|
_ = scanner.Text()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:20:01 +00:00
|
|
|
// 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 {
|
2018-03-21 05:56:33 +00:00
|
|
|
fmt.Println("Welcome to Gosora's Upgrader")
|
2018-04-04 07:07:24 +00:00
|
|
|
fmt.Println("We're going to check for new updates, please wait patiently")
|
2018-03-21 05:56:33 +00:00
|
|
|
|
2018-04-04 07:07:24 +00:00
|
|
|
repo, err := git.PlainOpen(".")
|
2018-03-21 05:56:33 +00:00
|
|
|
if err != nil {
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
workTree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 07:49:54 +00:00
|
|
|
err = workTree.Pull(&git.PullOptions{Force: true})
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
fmt.Println("You are already up-to-date")
|
|
|
|
return true
|
2018-04-04 08:45:07 +00:00
|
|
|
} else if err != nil && err != git.ErrUnstagedChanges { // fixes a bug in git where it refuses to update the files
|
|
|
|
return logError(err)
|
|
|
|
}
|
|
|
|
|
2018-04-04 09:19:38 +00:00
|
|
|
// The unstaged files are particularly resistant, so blast them away at full force
|
2018-04-04 09:01:55 +00:00
|
|
|
err = workTree.Reset(&git.ResetOptions{Mode: git.HardReset})
|
2018-04-04 08:45:07 +00:00
|
|
|
if err != nil {
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Updated to the latest commit")
|
|
|
|
headRef, err := repo.Head()
|
|
|
|
if err != nil {
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 08:56:10 +00:00
|
|
|
// Get information about the commit
|
2018-03-21 05:56:33 +00:00
|
|
|
commit, err := repo.CommitObject(headRef.Hash())
|
2018-04-03 04:34:07 +00:00
|
|
|
if err != nil {
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-04-03 04:34:07 +00:00
|
|
|
}
|
|
|
|
fmt.Println("Commit details:", commit)
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
err = syscall.Exec("./patcher.bat", []string{}, os.Environ())
|
|
|
|
default: //linux, etc.
|
|
|
|
err = syscall.Exec("./patcher-linux", []string{}, os.Environ())
|
|
|
|
}
|
2018-04-04 07:20:01 +00:00
|
|
|
return logError(err)
|
2018-03-21 05:56:33 +00:00
|
|
|
}
|