forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
104 lines (91 loc) · 2.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"runtime"
"github.com/rivo/tview"
)
const githubURL = "https://api.github.com/repos/SoMuchForSubtlety/F1viewer/releases/latest"
type release struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
Body string `json:"body"`
}
func getRelease() (release, error) {
var re release
err := doGet(githubURL, &re)
return re, err
}
func (session *viewerSession) CheckUpdate() {
if !session.cfg.CheckUpdate {
return
}
release, err := getRelease()
if err != nil {
session.logError("could not check for release: ", err)
return
}
if release.TagName == version ||
release.TagName == "v"+version ||
version == "dev" {
return
}
session.logInfo("New version found!")
session.logInfo(release.TagName)
fmt.Fprintln(session.textWindow, "\n[blue::bu]"+release.Name+"[-::-]\n")
fmt.Fprintln(session.textWindow, release.Body+"\n")
updateNode := tview.NewTreeNode("UPDATE AVAILABLE").
SetColor(activeTheme.UpdateColor).
SetExpanded(false)
getUpdateNode := tview.NewTreeNode("download update").
SetColor(activeTheme.ActionNodeColor).
SetSelectedFunc(func() {
err := openbrowser("https://github.com/SoMuchForSubtlety/F1viewer/releases/latest")
if err != nil {
session.logError(err)
}
})
stopCheckingNode := tview.NewTreeNode("don't tell me about updates")
stopCheckingNode.SetColor(activeTheme.ActionNodeColor).
SetSelectedFunc(func() {
session.cfg.CheckUpdate = false
err := session.cfg.save()
if err != nil {
session.logError(err)
}
stopCheckingNode.SetText("update checks turned off")
})
updateNode.AddChild(getUpdateNode)
updateNode.AddChild(stopCheckingNode)
insertNodeAtTop(session.tree.GetRoot(), updateNode)
session.app.Draw()
}
func openbrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
return err
}
return nil
}
func doGet(url string, v interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(v)
}