-
Notifications
You must be signed in to change notification settings - Fork 129
/
check_updates.go
283 lines (231 loc) · 7.54 KB
/
check_updates.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package commands
import (
"encoding/json"
"io"
"math/rand"
"net/http"
"os"
"runtime"
"sort"
"strings"
"time"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/tenderly/tenderly-cli/userError"
)
type releaseResult struct {
Name string `json:"name"`
NodeId string `json:"node_id"`
Assets []releaseAsset `json:"assets"`
}
type releaseAsset struct {
Name string `json:"name"`
BrowserDownloadUrl string `json:"browser_download_url"`
}
type releaseCliMessage struct {
Format string `json:"format"`
Parts []releaseCliMessagePart `json:"parts"`
}
type releaseCliMessagePart struct {
Text string `json:"text"`
Formatting []string `json:"formatting"`
}
var versionAlreadyChecked bool
func init() {
RootCmd.AddCommand(checkUpdatesCmd)
}
var checkUpdatesCmd = &cobra.Command{
Use: "update-check",
Short: "Checks whether there is an update for the CLI",
Run: func(cmd *cobra.Command, args []string) {
CheckVersion(true, false)
},
}
func CheckVersion(force bool, encounteredError bool) {
if versionAlreadyChecked {
return
}
rand.Seed(time.Now().UnixNano())
randInt := rand.Intn(25)
if !force && randInt != 24 {
return
}
versionAlreadyChecked = true
response, err := http.Get("https://api.github.com/repos/tenderly/tenderly-cli/releases")
if err != nil || (response != nil && response.StatusCode != 200) {
if force {
if response != nil {
logrus.Debugf("Status code: %d", response.StatusCode)
}
userError.LogErrorf("failed creating github releases request: %s", userError.NewUserError(
err,
"\nFailed fetching newest releases from GitHub. Please try again.\n",
))
}
return
}
defer response.Body.Close()
contents, err := io.ReadAll(response.Body)
if err != nil {
if force && !encounteredError {
userError.LogErrorf("failed reading github releases request: %s", userError.NewUserError(
err,
Colorizer.Sprintf(
"\nFailed parsing latest releases from GitHub. Please try again. If the problem persists, please follow the installation steps described at %s to re-install the CLI.\n",
Colorizer.Bold(Colorizer.Green("https://github.com/Tenderly/tenderly-cli#installation")),
),
))
}
return
}
var result []releaseResult
err = json.Unmarshal(contents, &result)
if err != nil || len(result) == 0 {
if force && !encounteredError {
userError.LogErrorf("error unmarshaling github releases: %s", userError.NewUserError(
err,
Colorizer.Sprintf(
"\nFailed parsing latest releases from GitHub. Please try again. If the problem persists, please follow the installation steps described at %s to re-install the CLI.\n",
Colorizer.Bold(Colorizer.Green("https://github.com/Tenderly/tenderly-cli#installation")),
),
))
}
return
}
sort.Slice(result, func(i, j int) bool {
v1, err := version.NewVersion(result[i].Name)
if err != nil {
return false
}
v2, err := version.NewVersion(result[j].Name)
if err != nil {
return true
}
return v1.GreaterThan(v2)
})
currentVersion, err := version.NewVersion(CurrentCLIVersion)
if err != nil {
if force && !encounteredError {
userError.LogErrorf("cannot parse current cli version: %s", userError.NewUserError(
err,
Colorizer.Sprintf(
"\nCannot parse the current version of the Tenderly CLI. Please follow the installation steps described at %s to re-install the CLI.\n",
Colorizer.Bold(Colorizer.Green("https://github.com/Tenderly/tenderly-cli#installation")),
),
))
}
return
}
newestVersion, err := version.NewVersion(result[0].Name)
if err != nil {
if force && !encounteredError {
userError.LogErrorf("cannot parse newest cli version: %s", userError.NewUserError(
err,
Colorizer.Sprintf(
"\nCannot parse the newest version of the Tenderly CLI. Please follow the installation steps described at %s to re-install the CLI.\n",
Colorizer.Bold(Colorizer.Green("https://github.com/Tenderly/tenderly-cli#installation")),
),
))
}
return
}
if !newestVersion.GreaterThan(currentVersion) {
if force && !encounteredError {
logrus.Info(Colorizer.Sprintf(
"\nYou are already running the newest version of the Tenderly CLI: %s.\n",
Colorizer.Bold(Colorizer.Green(CurrentCLIVersion)),
))
}
return
}
var updateCommand string
switch runtime.GOOS {
case "darwin":
updateCommand = getMacOSInstallationCommand()
case "linux":
fallthrough
default:
updateCommand = Colorizer.Sprintf("%s", Colorizer.Bold(Colorizer.Green("curl https://raw.githubusercontent.com/Tenderly/tenderly-cli/master/scripts/install-linux.sh | sh")))
}
logrus.Info(
Colorizer.Sprintf("\nYou are running version %s of the Tenderly CLI. To update to the newest version (%s) please follow the instructions below:\n\n%s\n",
Colorizer.Bold(Colorizer.Green(CurrentCLIVersion)),
Colorizer.Bold(Colorizer.Green(result[0].Name)),
updateCommand,
),
)
cliMessage, err := getCliMessage(result[0])
if err != nil || cliMessage == "" {
logrus.Debugf("error fetching cli message: %s", err)
return
}
logrus.Infof("Below are the notes for this release:\n\n%s\n", cliMessage)
}
func getMacOSInstallationCommand() string {
path, err := os.Executable()
defaultMessage := Colorizer.Sprintf("If you installed the CLI via Homebrew you can update it by running:\n\n%s\n\n"+
"Alternatively, if you installed the CLI via the installation script, you can update your installation by running the same script again:\n\n%s",
Colorizer.Bold(Colorizer.Green("brew update && brew upgrade tenderly")),
Colorizer.Bold(Colorizer.Green("curl https://raw.githubusercontent.com/Tenderly/tenderly-cli/master/scripts/install-macos.sh | sh")),
)
if err != nil {
return defaultMessage
}
link, err := os.Readlink(path)
if err != nil {
return defaultMessage
}
if strings.Contains(link, "Cellar") {
return Colorizer.Sprintf("It seems you installed the CLI via Homebrew, so you can update it by running:\n\n%s",
Colorizer.Bold(Colorizer.Green("brew update && brew upgrade tenderly")),
)
}
return Colorizer.Sprintf("It seems you installed the CLI via the installation script, so you can update it by running:\n\n%s",
Colorizer.Bold(Colorizer.Green("curl https://raw.githubusercontent.com/Tenderly/tenderly-cli/master/scripts/install-macos.sh | sh")),
)
}
func getCliMessage(release releaseResult) (string, error) {
var assetUrl string
for _, asset := range release.Assets {
if asset.Name == "cli-message.json" {
assetUrl = asset.BrowserDownloadUrl
break
}
}
if assetUrl == "" {
return "", errors.New("couldn't find cli-message.json in asset list")
}
response, err := http.Get(assetUrl)
if err != nil {
return "", errors.Wrap(err, "get cli-message.json")
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return "", errors.Wrap(err, "read cli-message.json")
}
var cliMessage releaseCliMessage
err = json.Unmarshal(body, &cliMessage)
if err != nil {
return "", errors.Wrap(err, "unmarshal cli-message.json")
}
var args []interface{}
for _, part := range cliMessage.Parts {
formattedPart := Colorizer.White(part.Text)
for i := len(part.Formatting) - 1; i >= 0; i-- {
switch part.Formatting[i] {
case "bold":
formattedPart = Colorizer.Bold(formattedPart)
case "green":
formattedPart = Colorizer.Green(formattedPart)
case "red":
formattedPart = Colorizer.Red(formattedPart)
}
}
args = append(args, formattedPart)
}
message := Colorizer.Sprintf(cliMessage.Format, args...)
return message, nil
}