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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := updater(scanner)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pressAnyKey(scanner *bufio.Scanner) {
|
|
|
|
fmt.Println("Please press enter to exit...")
|
|
|
|
for scanner.Scan() {
|
|
|
|
_ = scanner.Text()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updater(scanner *bufio.Scanner) error {
|
|
|
|
fmt.Println("Welcome to Gosora's Upgrader")
|
|
|
|
fmt.Print("We're going to check for new updates, please wait patiently")
|
|
|
|
|
|
|
|
repo, err := git.PlainOpen("./.git")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
workTree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = workTree.Pull(&git.PullOptions{RemoteName: "origin"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Updated to the latest commit")
|
|
|
|
headRef, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := repo.CommitObject(headRef.Hash())
|
2018-04-03 04:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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-03-21 05:56:33 +00:00
|
|
|
return err
|
|
|
|
}
|