forked from labdao/plex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade.go
More file actions
350 lines (289 loc) · 7.98 KB
/
upgrade.go
File metadata and controls
350 lines (289 loc) · 7.98 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"archive/tar"
"compress/gzip"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/Masterminds/semver"
)
const (
CurrentPlexVersion = "v0.6.0"
ReleaseURL = "https://api.github.com/repos/labdao/plex/releases/latest"
ToolsURL = "https://api.github.com/repos/labdao/plex/contents/tools?ref=main"
)
func getLatestReleaseVersionStr() (string, error) {
resp, err := http.Get(ReleaseURL)
if err != nil {
return "", fmt.Errorf("Error getting latest release: %v", err)
}
defer resp.Body.Close()
var responseMap map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseMap)
if err != nil {
return "", fmt.Errorf("Error decoding latest release: %v", err)
}
htmlURL, ok := responseMap["html_url"].(string)
if !ok {
return "", fmt.Errorf("Error getting latest release html_url")
}
urlPartition := strings.Split(htmlURL, "/")
latestReleaseVersionStr := urlPartition[len(urlPartition)-1]
return latestReleaseVersionStr, nil
}
func getLocalFilesSHA(toolsFolderPath string) (map[string]string, error) {
localFilesSHA := make(map[string]string)
err := filepath.Walk(toolsFolderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
relPath, _ := filepath.Rel(toolsFolderPath, path)
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
h := sha1.New()
if _, err := io.Copy(h, file); err != nil {
return err
}
sha := fmt.Sprintf("%x", h.Sum(nil))
localFilesSHA[relPath] = sha
}
return nil
})
if err != nil {
return nil, err
}
return localFilesSHA, nil
}
func downloadFile(url, destination string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(destination)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func downloadAndDecompressBinary(url, destination string) error {
fmt.Printf("Downloading binary from: %s\n", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
gzipReader, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
var fileContent []byte
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if filepath.Base(header.Name) == "plex" {
fileContent, err = io.ReadAll(tarReader)
if err != nil {
return err
}
break
}
}
err = ioutil.WriteFile(destination, fileContent, 0755)
if err != nil {
return err
}
return nil
}
func readManifestFile(manifestPath string) (map[string]string, error) {
manifest := make(map[string]string)
fileBytes, err := ioutil.ReadFile(manifestPath)
if err != nil {
if os.IsNotExist(err) {
return manifest, nil
}
return nil, err
}
err = json.Unmarshal(fileBytes, &manifest)
if err != nil {
return nil, err
}
return manifest, nil
}
func writeManifestFile(manifestPath string, manifest map[string]string) error {
manifestBytes, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(manifestPath, manifestBytes, 0644)
if err != nil {
return err
}
return nil
}
func upgradeToolsFolder(latestReleaseVersionStr string) error {
toolsFolderPath := filepath.Join(".", "tools")
manifestPath := filepath.Join(toolsFolderPath, ".manifest.json")
// Check if the tools folder exists, and create it if it doesn't
if _, err := os.Stat(toolsFolderPath); os.IsNotExist(err) {
fmt.Println("Creating the tools folder...")
err := os.Mkdir(toolsFolderPath, 0755)
if err != nil {
return err
}
}
localFilesSHA, err := readManifestFile(manifestPath)
if err != nil {
return err
}
updatedManifest := make(map[string]string)
resp, err := http.Get(ToolsURL)
if err != nil {
return err
}
defer resp.Body.Close()
var files []map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&files); err != nil {
return err
}
for _, file := range files {
if fileType, ok := file["type"].(string); ok && fileType == "file" {
fileName, ok := file["name"].(string)
if !ok {
continue
}
fileSHA, ok := file["sha"].(string)
if !ok {
continue
}
downloadURL, ok := file["download_url"].(string)
if !ok {
continue
}
localFilePath := filepath.Join(toolsFolderPath, fileName)
_, err := os.Stat(localFilePath)
fileExists := !os.IsNotExist(err)
localSHA, exists := localFilesSHA[fileName]
if !fileExists || !exists || localSHA != fileSHA {
fmt.Printf("Downloading %s...\n", fileName)
destination := filepath.Join(toolsFolderPath, fileName)
if err := downloadFile(downloadURL, destination); err != nil {
return err
}
updatedManifest[fileName] = fileSHA
} else {
updatedManifest[fileName] = localSHA
}
}
}
return writeManifestFile(manifestPath, updatedManifest)
}
func upgradePlexVersion() error {
// Auto update to latest plex version
localReleaseVersion, err := semver.NewVersion(CurrentPlexVersion)
if err != nil {
fmt.Printf("Error parsing local release version: %v\n", err)
os.Exit(1)
}
latestReleaseVersionStr, err := getLatestReleaseVersionStr()
if err != nil {
fmt.Println("Error getting latest release version:", err)
os.Exit(1)
}
latestReleaseVersion, err := semver.NewVersion(latestReleaseVersionStr)
if err != nil {
fmt.Printf("Error parsing latest release version: %v\n", err)
os.Exit(1)
}
if localReleaseVersion.LessThan(latestReleaseVersion) {
fmt.Printf("The version of plex you are running (v%s) is outdated.\n", localReleaseVersion)
fmt.Printf("Updating to latest plex version (v%s)...\n", latestReleaseVersion)
userOS := runtime.GOOS
userArch := runtime.GOARCH
fmt.Printf("- Operating System detected: %s\n- Chip Architecture detected: %s\n", userOS, userArch)
// Upgrade tools folder
err := upgradeToolsFolder(latestReleaseVersionStr)
if err != nil {
fmt.Println("Error upgrading tools folder:", err)
os.Exit(1)
}
// Upgrade plex binary
oldBinaryPath := os.Args[0]
backupBinaryPath := fmt.Sprintf("%s.bak", oldBinaryPath)
err = os.Rename(oldBinaryPath, backupBinaryPath)
if err != nil {
fmt.Printf("Error renaming the current binary: %v\n", err)
os.Exit(1)
}
binaryURL := fmt.Sprintf("https://github.com/labdao/plex/releases/download/%s/plex_%s_%s_%s.tar.gz", latestReleaseVersionStr, strings.TrimPrefix(latestReleaseVersionStr, "v"), userOS, userArch)
tempFile, err := ioutil.TempFile("", "plex_*")
if err != nil {
fmt.Printf("Error creating temporary file: %v\n", err)
os.Exit(1)
}
defer os.Remove(tempFile.Name())
err = downloadAndDecompressBinary(binaryURL, tempFile.Name())
if err != nil {
fmt.Printf("Error downloading and decompressing latest binary: %v\n", err)
os.Exit(1)
}
out, err := os.Create("plex_new")
if err != nil {
fmt.Printf("Error creating new binary: %v\n", err)
os.Exit(1)
}
defer out.Close()
_, err = io.Copy(out, tempFile)
if err != nil {
fmt.Printf("Error writing to new binary: %v\n", err)
os.Exit(1)
}
err = os.Chmod("plex_new", 0755)
if err != nil {
fmt.Printf("Error setting permissions on new binary: %v\n", err)
// Rollback the rename operation by restoring the backup binary
if err := os.Rename(backupBinaryPath, oldBinaryPath); err != nil {
fmt.Printf("Error restoring the backup binary: %v\n", err)
}
os.Exit(1)
}
err = os.Rename("plex_new", "plex")
if err != nil {
fmt.Printf("Error renaming new binary: %v\n", err)
os.Exit(1)
}
err = os.Remove(backupBinaryPath)
if err != nil {
fmt.Printf("Error removing backup binary: %v\n", err)
os.Exit(1)
}
fmt.Printf("Plex updated successfully to (v%s). Please re-run your desired command.\n", latestReleaseVersion)
os.Exit(1)
} else {
fmt.Printf("Plex version (v%s) up to date.\n", localReleaseVersion)
}
return nil
}