-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
44 lines (40 loc) · 1.14 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"log"
"os"
"path/filepath"
"time"
"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/skratchdot/open-golang/open"
)
const version = "0.0.5"
// SelfUpdate updates the application (thx blackshark for this function <3)
func SelfUpdate() {
log.Println("Checking Updates...")
name, err := os.Executable()
if err != nil {
log.Fatalln(err)
}
v := semver.MustParse(version)
latest, err := selfupdate.UpdateSelf(v, "kdancybot/np-client")
if err != nil {
log.Println("Binary update failed:", err)
return
}
if latest.Version.Equals(v) {
// latest version is the same as current version. It means current binary is up to date.
log.Println("Current binary is the latest version", version)
log.Println("Release notes:\n", latest.ReleaseNotes)
full, _ := os.Executable()
path, executable := filepath.Split(full)
oldName := filepath.Join(path, "."+executable+".old")
os.Remove(oldName)
} else {
log.Println("Successfully updated to version", latest.Version)
log.Println("Release notes:\n", latest.ReleaseNotes)
time.Sleep(3 * time.Second)
open.Start(name)
os.Exit(0)
}
}