-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.go
145 lines (128 loc) · 3.23 KB
/
generate.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
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
func main() {
download("https://github.com/igoogolx/lux-rules/releases/download/v2.0.1/rules.tar.gz", filepath.Join("internal", "cfg", "distribution", "ruleEngine", "rules.tar.gz"))
download("https://github.com/igoogolx/lux-client/releases/download/v0.18.0/dist-ui.tar.gz", filepath.Join("api", "routes", "dist.tar.gz"))
}
func download(url string, outputPath string) {
err := downloadFile(url, outputPath)
if err != nil {
fmt.Println("Error downloading file:", err)
return
}
err = unarchiveFile(outputPath)
if err != nil {
fmt.Println("Error unarchiving file:", err)
return
}
fmt.Println("File downloaded and unarchived successfully")
}
func downloadFile(url string, filePath string) error {
// Create the file to which the archive will be downloaded
file, err := os.Create(filePath)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("fail to close file: %v\n", filePath)
}
}(file)
// Download the archive
response, err := http.Get(url)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Printf("fail to close body: %v\n", filePath)
}
}(response.Body)
// Write the archive to the file
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}
func unarchiveFile(archiveFilePath string) error {
// Open the archive file
file, err := os.Open(archiveFilePath)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("fail to close file: %v\n", archiveFilePath)
}
err = os.Remove(archiveFilePath)
if err != nil {
fmt.Printf("fail to remove file: %v\n", archiveFilePath)
}
}(file)
// Create a gzip reader to read the compressed data
gzipReader, err := gzip.NewReader(file)
if err != nil {
return err
}
defer func(gzipReader *gzip.Reader) {
err := gzipReader.Close()
if err != nil {
fmt.Printf("fail to close gzip reader: %v\n", archiveFilePath)
}
}(gzipReader)
// Create a tar reader to read the contents of the archive
tarReader := tar.NewReader(gzipReader)
parentDir := filepath.Dir(archiveFilePath)
// Extract each file in the archive
for {
header, err := tarReader.Next()
if err == io.EOF {
// End of archive
break
}
if err != nil {
return err
}
outputPath := filepath.Join(parentDir, header.Name)
err = os.RemoveAll(outputPath)
if err != nil {
fmt.Printf("fail to remove path: %s", outputPath)
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(outputPath, 0755); err != nil {
fmt.Printf("ExtractTarGz: Mkdir() failed: %s", err.Error())
}
case tar.TypeReg:
outFile, err := os.Create(outputPath)
if err != nil {
fmt.Printf("ExtractTarGz: Create() failed: %s", err.Error())
}
if _, err := io.Copy(outFile, tarReader); err != nil {
fmt.Printf("ExtractTarGz: Copy() failed: %s", err.Error())
}
err = outFile.Close()
if err != nil {
fmt.Printf("fail to close file: %v\n", outputPath)
}
default:
fmt.Printf(
"ExtractTarGz: uknown type: %v in %s",
header.Typeflag,
header.Name)
}
}
return nil
}