-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathversion.go
73 lines (57 loc) · 1.38 KB
/
version.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
_ "embed"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
)
//go:embed VERSION
var AppVersion string
var latestRelease = "https://github.com/dub-flow/subsnipe/releases/latest"
func NotifyOfUpdates() {
client := &http.Client{}
req, err := http.NewRequest("GET", latestRelease, nil)
if err != nil {
return
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
return
}
vCurrent, err := version.NewVersion(AppVersion)
if err != nil {
fmt.Print(err)
}
vLatest, err := version.NewVersion(response["tag_name"].(string))
if err != nil {
fmt.Print(err)
}
// check if a newer version exists in the GitHub Releases
if vCurrent.LessThan(vLatest) {
color.Red(fmt.Sprintf("Please upgrade to the latest version of this tool (%s) by visiting %s or pulling the latest docker image\n\n", response["tag_name"], latestRelease))
}
}
func printAppVersion() {
// this should never happen, since we embed the version inside the
// application.
if AppVersion == "" {
AppVersion = "0.0.0-unknown"
}
color.Yellow("Current version: %s\n\n", AppVersion)
}