-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.go
280 lines (236 loc) · 6.88 KB
/
github.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
package main
import (
"archive/tar"
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/ulikunitz/xz"
)
type GhRelease struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
Author GhAuthor `json:"author"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []GhAsset `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}
type GhAuthor struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
type GhAsset struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader GhAuthor `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadURL string `json:"browser_download_url"`
}
func ParseReleaseData(data []byte) (GhRelease, error) {
var release GhRelease
err := json.Unmarshal(data, &release)
if err != nil {
return GhRelease{}, err
}
return release, nil
}
// taken from https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
func extractTar(tr *tar.Reader, dst string) error {
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)
// the following switch could also be done using fi.Mode(), not sure if there
// a benefit of using one vs. the other.
// fi := header.FileInfo()
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
// if it's a file create it
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
}
}
}
func DownloadExtractArchive(url string, dest string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// body is a .tar.xz file
xzr, err := xz.NewReader(bytes.NewReader(body))
if err != nil {
return err
}
tr := tar.NewReader(xzr)
err = extractTar(tr, dest)
return err
}
func (a *App) GetLatestReleaseData(repo string) (*GhRelease, error) {
for _, release := range a.latest_release_json {
if release.repo == repo {
return &release.content, nil
}
}
latest_release_url := "https://api.github.com/repos/" + repo + "/releases/latest"
resp, err := http.Get(latest_release_url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
content, err := ParseReleaseData(body)
if err != nil {
return nil, err
}
a.latest_release_json = append(a.latest_release_json, RawReleaseInfo{repo, content})
return &a.latest_release_json[len(a.latest_release_json)-1].content, nil
}
func (a *App) UpdateBotpack(repo string, installPath string, currentTag string) (string, error) {
latest_release, err := a.GetLatestReleaseData(repo)
if err != nil {
return "", err
}
newest_tag := latest_release.TagName
if newest_tag == currentTag {
return "", errors.New("already up to date")
}
current_version := strings.Split(currentTag, "-")[1]
current_version_num, err := strconv.Atoi(current_version)
if err != nil {
return "", err
}
newest_version := strings.Split(newest_tag, "-")[1]
newest_version_num, err := strconv.Atoi(newest_version)
if err != nil {
return "", err
}
var file_name string
// todo: check platform (x86_64, etc)
if runtime.GOOS == "windows" {
file_name = "patch_x86_64-windows.bobdiff"
} else {
file_name = "patch_x86_64-linux.bobdiff"
}
var lastest_download_url string
for _, asset := range latest_release.Assets {
if asset.Name == file_name {
lastest_download_url = asset.BrowserDownloadURL
break
}
}
for i := current_version_num + 1; i <= newest_version_num; i++ {
tag_i := strings.Replace(currentTag, current_version, strconv.Itoa(i), 1)
download_url := strings.Replace(lastest_download_url, newest_tag, tag_i, 1)
resp, err := http.Get(download_url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
bytes, err := io.ReadAll(bytes.NewReader(body))
if err != nil {
return "", err
}
files, err := os.ReadDir(installPath)
if err != nil {
return "", err
}
var dir string
for _, file := range files {
if file.IsDir() {
dir = file.Name()
break
}
}
if dir == "" {
return "", errors.New("no directory found")
}
err = diff_apply(filepath.Join(installPath, dir), bytes)
if err != nil {
return "", err
}
}
return latest_release.TagName, nil
}