generated from AlexanderMac/go-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosu.go
293 lines (249 loc) · 7.29 KB
/
gosu.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
284
285
286
287
288
289
290
291
292
293
package gosu
import (
_ "embed"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
semver "github.com/Masterminds/semver/v3"
"github.com/go-resty/resty/v2"
"golang.org/x/exp/slices"
)
//go:embed scripts/linux.txt
var linuxScript string
//go:embed scripts/windows.txt
var windowsScript string
const (
_LINUX_SCRIPT_NAME = "update.sh"
_WIN_SCRIPT_NAME = "update.cmd"
)
const (
CODE_LATEST_VERSION_IS_USED_ALREADY = "LATEST_VERSION_IS_USED_ALREADY"
CODE_UNRELEASED_VERSION_IS_USED = "UNRELEASED_VERSION_IS_USED"
CODE_UPGRADE_CONFIRMATION = "UPGRADE_CONFIRMATION"
CODE_ERROR = "ERROR"
)
type Updater struct {
ReleasesUrl string
ChangelogUrl string
LocalVersion string
GhAccessToken string
DownloadChangelog bool
}
type _CheckUpdatesResult struct {
Code string
Message string
Details string
}
type _GhReleaseAsset struct {
Name string `json:"name"`
Url string `json:"url"`
updateScriptName string
updateScriptBody string
}
type _GhRelease struct {
TagName string `json:"tag_name"`
CreatedAt string `json:"created_at"`
Assets []_GhReleaseAsset `json:"assets"`
Body string `json:"body"`
}
func New(orgRepoName, ghAccessToken, localVersion string) *Updater {
return &Updater{
ReleasesUrl: fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", orgRepoName),
ChangelogUrl: fmt.Sprintf("https://api.github.com/repos/%s/contents/CHANGELOG.md", orgRepoName),
LocalVersion: localVersion,
GhAccessToken: ghAccessToken,
}
}
func (updater *Updater) CheckUpdates() (_CheckUpdatesResult, error) {
logger.Info("Checking for updates")
lastRelease, err := updater.getLastRelease()
if err != nil {
return _CheckUpdatesResult{
Code: CODE_ERROR,
Message: fmt.Sprintf("Unable to get updates. %s.", updater.parseHttpError(err)),
}, nil
}
remoteSemver := updater.parseSemVer(lastRelease.TagName)
localSemver := updater.parseSemVer(updater.LocalVersion)
if remoteSemver == nil || localSemver == nil {
return _CheckUpdatesResult{
Code: CODE_ERROR,
Message: "Unable to get updates. The semvver is invalid.",
}, nil
}
// up-to-date
if remoteSemver.Equal(localSemver) {
logger.Info("The latest version is already used")
return _CheckUpdatesResult{
Code: CODE_LATEST_VERSION_IS_USED_ALREADY,
Message: "You already use the latest version.",
}, nil
}
// local version is higher
if remoteSemver.LessThan(localSemver) {
logger.Info("The local version is higher than remote")
return _CheckUpdatesResult{
Code: CODE_UNRELEASED_VERSION_IS_USED,
Message: "You use the unreleased version.",
}, nil
}
// new version detected
lastReleaseDetails := lastRelease.Body
if updater.DownloadChangelog {
changelog, err := updater.getChangelog()
if err != nil {
logger.Warnf("Unable to download changelog, error: %s", err.Error())
}
if changelog != "" {
lastReleaseDetails = changelog
}
}
logger.Infof("New version detected %s", lastRelease.TagName)
return _CheckUpdatesResult{
Code: CODE_UPGRADE_CONFIRMATION,
Message: fmt.Sprintf(
"Upgrade to a new version? Current version is %s, new version is %s.",
updater.LocalVersion,
lastRelease.TagName,
),
Details: lastReleaseDetails,
}, nil
}
func (updater *Updater) UpgradeApp() error {
lastRelease, err := updater.getLastRelease()
if err != nil {
return err
}
var asset _GhReleaseAsset
if strings.Contains(runtime.GOOS, "linux") {
assetIndex := slices.IndexFunc(lastRelease.Assets, func(asset _GhReleaseAsset) bool {
return strings.Contains(asset.Name, "-linux")
})
asset = lastRelease.Assets[assetIndex]
asset.updateScriptName = _LINUX_SCRIPT_NAME
asset.updateScriptBody = linuxScript
} else if strings.Contains(runtime.GOOS, "windows") {
assetIndex := slices.IndexFunc(lastRelease.Assets, func(asset _GhReleaseAsset) bool {
return strings.Contains(asset.Name, "-win")
})
asset = lastRelease.Assets[assetIndex]
asset.updateScriptName = _WIN_SCRIPT_NAME
asset.updateScriptBody = windowsScript
} else {
err := errors.New("Unsupported OS: " + runtime.GOOS)
if err != nil {
return err
}
}
err = updater.downloadAsset(asset)
if err != nil {
return err
}
err = updater.createAndRunExtractor(asset)
if err != nil {
return err
}
logger.Info("Terminating the app")
os.Exit(0)
return nil
}
func (updater *Updater) getLastRelease() (_GhRelease, error) {
logger.Info("Getting the last release")
var ghRelease _GhRelease
res, err := updater.getHttpClient().
R().
SetResult(&ghRelease).
Get(updater.ReleasesUrl)
if err != nil {
return _GhRelease{}, err
}
if err := updater.checkHttpResponse(res); err != nil {
return _GhRelease{}, err
}
logger.Infof("Got the last release: %v successfully", ghRelease)
return ghRelease, nil
}
func (updater *Updater) getChangelog() (string, error) {
logger.Info("Getting the changelog")
res, err := updater.getHttpClient().
R().
SetHeader("Accept", "application/vnd.github.raw").
Get(updater.ChangelogUrl)
if err != nil {
return "", err
}
if err := updater.checkHttpResponse(res); err != nil {
return "", err
}
logger.Info("Got the changelog successfully")
return string(res.Body()), nil
}
func (updater *Updater) downloadAsset(asset _GhReleaseAsset) error {
logger.Infof("Downloading the asset %s from %s", asset.Name, asset.Url)
res, err := updater.getHttpClient().
R().
SetHeader("Accept", "application/octet-stream").
SetOutput(asset.Name).
Get(asset.Url)
if err != nil {
return err
}
if err := updater.checkHttpResponse(res); err != nil {
return err
}
logger.Infof("Downloaded the asset %s successfully", asset.Name)
return nil
}
func (updater *Updater) createAndRunExtractor(asset _GhReleaseAsset) error {
scriptName := fmt.Sprintf(".%s%s", string(os.PathSeparator), asset.updateScriptName)
logger.Infof("Creating an extractor script %s", scriptName)
err := os.WriteFile(scriptName, []byte(asset.updateScriptBody), 0777)
if err != nil {
return err
}
logger.Infof("Running the extractor script %s", scriptName)
cmd := exec.Command(scriptName)
err = cmd.Start()
if err != nil {
return err
}
// don't call cmd.Wait because we need to close the app immediately
logger.Infof("The extractor script run successfully")
return nil
}
func (updater *Updater) getHttpClient() *resty.Client {
client := resty.New()
client.Header.Set("Accept", "application/vnd.github+json")
client.Header.Set("X-GitHub-Api-Version", "2022-11-28")
if updater.GhAccessToken != "" {
client.Header.Set("Authorization", fmt.Sprintf("Bearer %s", updater.GhAccessToken))
}
return client
}
func (updater *Updater) checkHttpResponse(res *resty.Response) error {
if res.IsError() {
return fmt.Errorf("Request failed with status: %d. %s", res.StatusCode(), res.String())
}
return nil
}
func (updater *Updater) parseHttpError(err error) string {
if strings.Contains(err.Error(), "connection refused") {
return "Error on connect to remote server"
}
return err.Error()
}
func (updater *Updater) parseSemVer(version string) *semver.Version {
ret, err := semver.NewVersion(version)
if err != nil {
if strings.Contains(err.Error(), "Error parsing version segment") {
logger.Warnf("Unable to parse version, version=%s", version)
} else {
logger.Error(err)
}
return nil
}
return ret
}