-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (176 loc) · 4.69 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
package main
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
type Tag struct {
Name string `json:"name"`
}
func main() {
apiURL := "https://api.github.com/repos/tukui-org/ElvUI/tags"
resp, err := http.Get(apiURL)
if err != nil {
fmt.Printf("Error while receive tags : %s\n", err)
time.Sleep(3 * time.Second)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Error while receive tags. Code statut: %d\n", resp.StatusCode)
time.Sleep(3 * time.Second)
return
}
var tags []Tag
err = json.NewDecoder(resp.Body).Decode(&tags)
if err != nil {
fmt.Printf("Error on reading JSON response : %s\n", err)
time.Sleep(3 * time.Second)
return
}
if len(tags) == 0 {
fmt.Println("No tag found on repo.")
time.Sleep(3 * time.Second)
return
}
lastTag := tags[0].Name
zipURL := fmt.Sprintf("https://github.com/tukui-org/ElvUI/archive/%s.zip", lastTag)
zipResp, err := http.Get(zipURL)
if err != nil {
fmt.Printf("Error while downloading zip file : %s\n", err)
time.Sleep(3 * time.Second)
return
}
defer zipResp.Body.Close()
file, err := os.Create(fmt.Sprintf("%s.zip", lastTag))
if err != nil {
fmt.Printf("Error while creating local file : %s\n", err)
time.Sleep(3 * time.Second)
return
}
defer file.Close()
_, err = io.Copy(file, zipResp.Body)
if err != nil {
fmt.Printf("Error while copying zip files : %s\n", err)
time.Sleep(3 * time.Second)
return
}
fmt.Printf("Zip file on latest tag (%s) downloaded with success.\n", lastTag)
fmt.Println("Starting decompressing file ...")
err = unzip(fmt.Sprintf("%s.zip", lastTag), "./AddOns/")
if err != nil {
fmt.Printf("Error while decompressing zip file: %s\n", err)
time.Sleep(3 * time.Second)
return
}
fmt.Println("Latest version of ElvUI installation is succed !")
time.Sleep(3 * time.Second)
}
func unzip(zipFile, dest string) error {
reader, err := zip.OpenReader(zipFile)
if err != nil {
return fmt.Errorf("error while openning zip file: %s", err)
}
defer reader.Close()
commonPrefix := findCommonPrefix(reader.File)
for _, file := range reader.File {
if !strings.HasPrefix(file.Name, commonPrefix) {
continue
}
// Build destination path with common prefix removed
relPath, err := filepath.Rel(commonPrefix, file.Name)
if err != nil {
return fmt.Errorf("error when writing relative path %s : %s", file.Name, err)
}
path := filepath.Join(dest, relPath)
if file.FileInfo().IsDir() {
// Create recursively dir if not exist
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return fmt.Errorf("errror while creating directory %s : %s", path, err)
}
continue
}
fileReader, err := file.Open()
if err != nil {
return fmt.Errorf("error while openning file %s in : %s", file.Name, err)
}
defer fileReader.Close()
dir := filepath.Dir(path)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return fmt.Errorf("error while creating file %s : %s", path, err)
}
defer targetFile.Close()
_, err = io.Copy(targetFile, fileReader)
if err != nil {
return fmt.Errorf("%s : %s", file.Name, err)
}
targetFile.Close()
}
time.Sleep(3 * time.Second)
// Remove all files and dirs in ElvUI project default files
toDelete := []string{".github", ".git", ".gitignore", ".pkgmeta", "CHANGELOG.md", "LICENSE.md", "Makefile", "README.md", "ThirdPartyNotices.md"}
if err := cleanUpExcept(dest, toDelete); err != nil {
return fmt.Errorf("error while deleting files : %s", err)
}
// Remove zip file
err = os.Remove(zipFile)
if err != nil {
return nil
}
return nil
}
func cleanUpExcept(dir string, toDelete []string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
fullPath := filepath.Join(dir, file.Name())
if contains(toDelete, file.Name()) {
if file.IsDir() {
if err := os.RemoveAll(fullPath); err != nil {
return fmt.Errorf("error while deleting directories %s : %s", fullPath, err)
}
} else {
if err := os.Remove(fullPath); err != nil {
return fmt.Errorf("error while deleting files %s : %s", fullPath, err)
}
}
}
}
return nil
}
func contains(list []string, item string) bool {
for _, val := range list {
if val == item {
return true
}
}
return false
}
func findCommonPrefix(files []*zip.File) string {
if len(files) == 0 {
return ""
}
prefix := files[0].Name
for _, file := range files[1:] {
for i := 0; i < len(prefix) && i < len(file.Name); i++ {
if prefix[i] != file.Name[i] {
prefix = prefix[:i]
break
}
}
}
return prefix
}