-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.go
169 lines (150 loc) · 4.03 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package version
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/DelineaXPM/dsv-cli/store"
)
const (
versionsURL = "https://dsv.secretsvaultcloud.com/cli-version.json"
checkFrequencyDays = 3
cacheFileName = ".update"
dateLayout = "2006-Jan-02"
updatePatternMessage = "Consider upgrading the CLI to version %s - download available at %s"
)
type update struct {
latest string
link string
}
type latestInfo struct {
Latest string `json:"latest"`
Links map[string]string `json:"links"`
}
func CheckLatestVersion() (*update, error) {
thyDir, err := store.GetDefaultPath()
if err != nil {
return nil, err
}
cacheFilePath := filepath.Join(thyDir, cacheFileName)
platform := runtime.GOOS + "/" + runtime.GOARCH
latest := readCache(cacheFilePath)
if latest == nil {
pageContent, err := fetchContent(versionsURL)
if err != nil {
return nil, err
}
latest = &latestInfo{}
err = json.Unmarshal(pageContent, latest)
if err != nil {
return nil, err
}
actualLink, ok := latest.Links[platform]
if !ok {
return nil, errors.New("links malformed")
}
latest.Links = map[string]string{platform: actualLink}
pageContent, err = json.Marshal(latest)
if err != nil {
return nil, err
}
updateCache(cacheFilePath, pageContent)
}
if isVersionOutdated(Version, latest.Latest) {
actualLink, ok := latest.Links[platform]
if !ok {
return nil, errors.New("links malformed")
}
return &update{latest: latest.Latest, link: actualLink}, nil
}
return nil, nil
}
// readCache returns parsed info from cache file
func readCache(updateFilePath string) *latestInfo {
_, err := os.Stat(updateFilePath)
if errors.Is(err, os.ErrNotExist) {
return nil
}
fileContent, err := os.ReadFile(updateFilePath)
if err != nil {
log.Printf("Failed to read content from file %s (%s) ", updateFilePath, err.Error())
return nil
}
const fileParts int = 2
contents := strings.SplitN(string(fileContent), "\n", fileParts)
if len(contents) < fileParts {
log.Printf("Wrong file %s format", updateFilePath)
return nil
}
date, err := time.Parse(dateLayout, contents[0])
if err != nil {
log.Printf("Wrong date format: '%s'", contents[0])
return nil
}
if time.Now().After(date.AddDate(0, 0, checkFrequencyDays)) {
log.Println("Cached content too old")
return nil
}
versions := &latestInfo{}
err = json.Unmarshal([]byte(contents[1]), versions)
if err != nil {
log.Printf("Wrong file content: '%s'", contents[1])
return nil
}
return versions
}
// updateCache updates content in the cache file
func updateCache(cacheFilePath string, content []byte) {
fileContent := fmt.Sprintf("%s\n%s", time.Now().Format(dateLayout), string(content))
err := os.WriteFile(cacheFilePath, []byte(fileContent), os.FileMode(0o644))
if err != nil {
// Only log error and continue
log.Printf("Unsuccessfully update of the cache file %s (%s) ", cacheFilePath, err)
}
}
// fetchContent retrieves content of the URL
func fetchContent(urlToFetch string) ([]byte, error) {
log.Println("Attempting to query the download server for a CLI update.")
resp, err := http.Get(urlToFetch)
if err != nil {
return nil, err
}
pageContent, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
_ = resp.Body.Close()
return pageContent, nil
}
func (e update) String() string {
return fmt.Sprintf(updatePatternMessage, e.latest, e.link)
}
// isVersionOutdated semantically compares target and latest versions
// to check if the target version is outdated.
func isVersionOutdated(target, latest string) bool {
// Only interested in the leading part before the first dash.
target = strings.Split(target, "-")[0]
latest = strings.Split(latest, "-")[0]
if target == "undefined" || target == latest {
return false
}
t := strings.Split(target, ".")
l := strings.Split(latest, ".")
for i := range t {
if t[i] == l[i] {
continue
}
n1, _ := strconv.Atoi(t[i])
n2, _ := strconv.Atoi(l[i])
return n1 < n2
}
return false
}