-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_dependencies.go
150 lines (132 loc) · 2.73 KB
/
install_dependencies.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
package main
import (
"archive/zip"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)
func main() {
baseUrl := "https://dxfeed.jfrog.io/artifactory/maven/com/dxfeed/graal-native-sdk/%s/graal-native-sdk-%s-%s-%s.zip"
version := "1.1.6"
archStr := ""
osStr := ""
switch os := runtime.GOOS; os {
case "darwin":
osStr = "osx"
case "linux":
osStr = "linux"
case "windows":
osStr = "windows"
default:
panic("Check OS value")
}
switch arch := runtime.GOARCH; arch {
case "arm64":
archStr = "aarch64"
case "amd64":
if osStr == "osx" {
archStr = "x86_64"
} else {
archStr = "amd64"
}
default:
panic("Check Architecture value")
}
fullPath := fmt.Sprintf(baseUrl, version, version, archStr, osStr)
fmt.Printf("Download from %s\n", fullPath)
fileName := "graal.zip"
defer os.Remove(fileName)
err := downloadFile(fileName, fullPath)
if err != nil {
panic(err)
}
destPath := "internal/native/graal"
fmt.Printf("Install graal to %s\n", destPath)
err = Unzip(fileName, destPath)
if err != nil {
panic(err)
}
}
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path)
}
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}