forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
upgrade.go
151 lines (125 loc) · 3.49 KB
/
upgrade.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
/*
Copyright 2017 WALLIX
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strconv"
"strings"
"time"
"github.com/thunderbird86/awless/database"
)
const (
lastUpgradeCheckDbKey = "upgrade.lastcheck"
)
func VerifyNewVersionAvailable(url string, messaging io.Writer) error {
return database.Execute(func(db *database.DB) error {
last, err := db.GetTimeValue(lastUpgradeCheckDbKey)
if err != nil {
return err
}
upgradeFreq := getCheckUpgradeFrequency()
if upgradeFreq < 0 {
return nil
}
if time.Since(last) > upgradeFreq {
notifyIfUpgrade(url, messaging)
}
return db.SetTimeValue(lastUpgradeCheckDbKey, time.Now())
})
}
func notifyIfUpgrade(url string, messaging io.Writer) error {
client := &http.Client{Timeout: 1500 * time.Millisecond}
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("User-Agent", "awless-client-"+Version)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
latest := struct {
Version, URL string
}{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&latest); err == nil {
if IsSemverUpgrade(Version, latest.Version) {
var install string
switch BuildFor {
case "brew":
install = "Run `brew upgrade awless`"
case "zip", "targz":
ext := "tar.gz"
if runtime.GOOS == "windows" {
ext = "zip"
}
install = fmt.Sprintf("Run `wget -O awless-%s.%s https://github.com/thunderbird86/awless/releases/download/%s/awless-%s-%s.%s`", latest.Version, ext, latest.Version, runtime.GOOS, runtime.GOARCH, ext)
default:
install = "Run `go get -u github.com/thunderbird86/awless`"
}
fmt.Fprintf(messaging, "New version %s available. Checkout the latest features at https://github.com/thunderbird86/awless/blob/master/CHANGELOG.md\n%s\n", latest.Version, install)
}
}
return nil
}
const semverLen = 3
type semver [semverLen]int
var SemverInvalidFormatErr = errors.New("semver invalid format")
func IsSemverUpgrade(current, latest string) bool {
i, err := CompareSemver(current, latest)
if err != nil {
return false
}
return i < 0
}
func CompareSemver(current, latest string) (int, error) {
current = strings.TrimPrefix(current, "v")
latest = strings.TrimPrefix(latest, "v")
dot := func(r rune) bool {
return r == '.'
}
cFields := strings.FieldsFunc(current, dot)
lFields := strings.FieldsFunc(latest, dot)
if len(cFields) != semverLen || len(lFields) != semverLen {
return 0, SemverInvalidFormatErr
}
currents := new(semver)
for i, f := range cFields {
num, err := strconv.Atoi(f)
if err != nil {
return 0, SemverInvalidFormatErr
}
currents[i] = num
}
latests := new(semver)
for i, f := range lFields {
num, err := strconv.Atoi(f)
if err != nil {
return 0, SemverInvalidFormatErr
}
latests[i] = num
}
for i := 0; i < semverLen; i++ {
if latests[i] > currents[i] {
return -1, nil
} else if latests[i] == currents[i] {
continue
} else {
return 1, nil
}
}
return 0, nil
}