-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (201 loc) · 5.49 KB
/
main.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
package main
import (
"archive/zip"
"context"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
)
var version = "0.1.1"
func main() {
// Folder paths
documentsfolder, err := DocumentsFolder()
if err != nil {
log.Fatal(err)
}
var (
poePath = filepath.Join(documentsfolder, "My Games/Path of Exile")
dotFilePath = filepath.Join(poePath, ".neversink-updater")
)
// Flags
filterStyle := flag.String(
"style",
"",
"Style of filters. Can be one of: blue, purple, slick, streamsound.")
helpPtr := flag.Bool("help", false, "Prints help.")
versionPtr := flag.Bool("version", false, "Prints version.")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage:")
flag.PrintDefaults()
}
flag.Parse()
if *versionPtr {
fmt.Fprintf(os.Stdout, version)
os.Exit(0)
}
if *helpPtr {
flag.PrintDefaults()
os.Exit(0)
}
// Updater logic
if err := checkPoeDir(poePath); err != nil {
exit(1, err.Error())
}
release, err := getLatestRelease()
if err != nil {
exit(1, err.Error())
}
currentVersion := getCurrentVersion(dotFilePath)
if *release.TagName == currentVersion {
exit(0, "There no need to update.")
}
zipFile, err := downloadZip(release.GetZipballURL())
if err != nil {
exit(1, err.Error())
}
tmpArchivePath := createTmpArchive(zipFile)
unzippedFileCount := unzipArchive(tmpArchivePath, poePath, *filterStyle)
if unzippedFileCount > 0 {
fmt.Fprintf(os.Stdout, "%d files were unzipped.\n", unzippedFileCount)
} else {
exit(1, fmt.Sprintf("No files were unzipped. Is \"%s\" correct filter style?", *filterStyle))
}
writeToDotfile(dotFilePath, *release.TagName)
showReleaseNotes(release)
// Clean up
zipFile.Close()
os.Remove(tmpArchivePath)
exit(0, "")
}
func exit(code int, message string) {
output := os.Stdout
if code != 0 {
output = os.Stderr
}
if message != "" {
fmt.Fprintln(output, message)
}
fmt.Fprint(os.Stdout, "Press any key to continue...")
fmt.Scanln()
os.Exit(0)
}
func checkPoeDir(dirPath string) error {
if _, err := os.Stat(dirPath); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("Path of Exile folder does not seem to exist. "+
"It is expected to be at %s. Make sure the game is installed.", dirPath)
}
return err
}
return nil
}
func getLatestRelease() (*github.RepositoryRelease, error) {
fmt.Fprint(os.Stdout, "Fetching the latest release... ")
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(
context.Background(), "NeverSinkDev", "NeverSink-Filter")
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stdout, "It is: %s\n", *release.TagName)
return release, nil
}
func downloadZip(url string) (io.ReadCloser, error) {
fmt.Fprint(os.Stdout, "Downloading the archive... ")
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Zipfile couldn't be downloaded. This isn't probably your fault. Try again later.")
}
fmt.Fprintln(os.Stdout, "Done.")
return resp.Body, nil
}
func createTmpArchive(content io.ReadCloser) string {
tmpfile, err := ioutil.TempFile("", "neversink-updater.zip")
if err != nil {
log.Fatal(err)
}
defer tmpfile.Close()
_, err = io.Copy(tmpfile, content)
if err != nil {
fmt.Println(err)
}
return tmpfile.Name()
}
func unzipArchive(archivePath string, targetPath string, filterStyle string) int {
archiveReader, err := zip.OpenReader(archivePath)
if err != nil {
log.Fatal(err)
}
defer archiveReader.Close()
var fileFilter func(*zip.File) bool
if filterStyle == "" {
fileFilter = func(file *zip.File) bool {
return strings.Count(file.Name, "/") == 1
}
} else {
fileFilter = func(file *zip.File) bool {
return strings.Split(file.Name, "/")[1] == filterStyleToFolder(filterStyle)
}
}
copiedFiles := 0
for _, archiveFile := range archiveReader.File {
if strings.HasSuffix(archiveFile.Name, ".filter") && fileFilter(archiveFile) {
copyFileContent(archiveFile, targetPath)
copiedFiles++
}
}
return copiedFiles
}
func filterStyleToFolder(filterStyle string) string {
return fmt.Sprintf("(STYLE) %s", strings.ToUpper(filterStyle))
}
func copyFileContent(file *zip.File, path string) {
rc, err := file.Open()
if err != nil {
log.Fatal(err)
}
fileNameParts := strings.Split(file.Name, "/")
f, err := os.OpenFile(
filepath.Join(path, fileNameParts[len(fileNameParts)-1]),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
file.Mode(),
)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(f, rc)
if err != nil {
log.Fatal(err)
}
rc.Close()
}
func getCurrentVersion(dotFilePath string) string {
content, err := ioutil.ReadFile(dotFilePath)
if err != nil {
fmt.Fprintln(os.Stdout, "Couldn't determine the latest installed version.")
return ""
}
fmt.Fprintf(os.Stdout, "Your current version is %s. ", content)
return string(content)
}
func writeToDotfile(dotFilePath string, version string) {
content := []byte(version)
err := ioutil.WriteFile(dotFilePath, content, 0644)
if err != nil {
log.Fatal(err)
}
}
func showReleaseNotes(release *github.RepositoryRelease) {
fmt.Fprintf(os.Stdout, "\nRelease notes (%s):\n\n%s\n\n", *release.TagName, *release.Body)
}