forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
121 lines (112 loc) · 2.66 KB
/
Copy pathupdate.go
File metadata and controls
121 lines (112 loc) · 2.66 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
type release struct {
Name string `json:"name"`
Body string `json:"body"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}
func getRelease() (release, error) {
var re release
jsonString, err := getJSON("https://api.github.com/repos/SoMuchForSubtlety/F1viewer/releases/latest")
if err != nil {
return re, err
}
err = json.Unmarshal([]byte(jsonString), &re)
if err != nil {
return re, err
}
return re, nil
}
func updateAvailable() (bool, error) {
url, err := getHashfileLink()
if err != nil {
return false, err
}
checksums, _, err := downloadData(url)
if err != nil {
return false, err
}
hash, err := calculateHash()
if err != nil {
return false, err
}
re1 := regexp.MustCompile(`^[a-z0-9]+`)
for _, line := range checksums {
match := re1.FindString(line)
if strings.ToLower(match) == strings.ToLower(hash) {
return false, nil
}
}
return true, nil
}
func getHashfileLink() (string, error) {
re, err := getRelease()
if err != nil {
return "", err
}
for _, asset := range re.Assets {
if asset.Name == "checksums.txt" {
return asset.BrowserDownloadURL, nil
}
}
return "", errors.New("no checksums.txt found")
}
func calculateHash() (string, error) {
hasher := sha256.New()
f, err := os.Open(os.Args[0])
if err != nil {
return "", err
}
if _, err := io.Copy(hasher, f); err != nil {
return "", err
}
f.Close()
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func getUpdateNode() (*tview.TreeNode, error) {
hasUpdate, err := updateAvailable()
if !hasUpdate {
err = errors.New("no update available")
}
re, _ := getRelease()
updateNode := tview.NewTreeNode("UPDATE AVAILABLE").SetColor(tcell.ColorRed).SetExpanded(false).SetReference(re)
getUpdateNode := tview.NewTreeNode("download update").SetColor(tcell.ColorRed).SetReference("update")
stopCheckingNode := tview.NewTreeNode("don't tell me about updates").SetColor(tcell.ColorRed).SetReference("don't check")
updateNode.AddChild(getUpdateNode)
updateNode.AddChild(stopCheckingNode)
return updateNode, err
}
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
}