-
Notifications
You must be signed in to change notification settings - Fork 6
/
update.go
93 lines (77 loc) · 2.51 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
// update.go - Update check and download latest version.
// Copyright (c) 2018 - 2020 Richard Huang <rickypc@users.noreply.github.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package host
import (
"github.com/hashicorp/go-version"
"io/ioutil"
"log"
"strconv"
"time"
)
// AutoUpdateCheck downloads the latest update as necessary.
func (h *Host) AutoUpdateCheck() {
if h.AutoUpdate {
if needed, downloadUrl := h.needUpdate(); needed {
if err := h.downloadLatest(downloadUrl); err != nil {
log.Printf("Update download error: %v", err)
} else {
log.Print("Update is downloaded")
}
}
}
}
// getCheckTimestamp returns previous update check timestamp in Unix
// nanoseconds.
func (h *Host) getCheckTimestamp() time.Time {
buf, _ := ioutil.ReadFile(h.ExecName + ".chk")
nano, _ := strconv.ParseInt(string(buf), 10, 64)
return time.Unix(0, nano)
}
// isCheckedToday returns true if update check was done sometime today,
// otherwise false.
func (h *Host) isCheckedToday() bool {
y1, m1, d1 := h.getCheckTimestamp().Date()
y2, m2, d2 := time.Now().Date()
return y1 == y2 && m1 == m2 && d1 == d2
}
// needUpdate returns true if update is needed, otherwise false.
//
// Truthy criteria:
// - Update check wasn't already done sometime today.
// - Current running version is older than updates.xml's version.
func (h *Host) needUpdate() (bool, string) {
response := false
if h.isCheckedToday() {
log.Print("Update already checked today")
return response, ""
}
if err := h.writeCheckTimestamp(); err != nil {
log.Printf("Update timestamp error: %v", err)
}
localVersion := version.Must(version.NewVersion(h.Version))
downloadUrl, remoteRawVersion, err := h.getDownloadUrlAndVersion()
if err != nil {
log.Printf("Update check error: %v", err)
}
remoteVersion := version.Must(version.NewVersion(remoteRawVersion))
if localVersion.LessThan(remoteVersion) {
log.Print("Latest update is found")
response = true
} else {
log.Print("Already up to date")
}
return response, downloadUrl
}
// writeCheckTimestamp writes update check timestamp in Unix nanoseconds.
// It will return error when it unable to write to .chk file.
func (h *Host) writeCheckTimestamp() error {
timestamp := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
if err := ioutil.WriteFile(h.ExecName+".chk", timestamp, 0644); err != nil {
return err
}
return nil
}