-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
215 lines (171 loc) · 3.72 KB
/
util.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
package main
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/net/html"
)
func GetFirstHeading(markdown string) string {
head := strings.Split(markdown, "<div align=\"center\">")[1]
head = strings.Split(head, "</div>")[0]
r := strings.NewReader(head)
z := html.NewTokenizer(r)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
// End of the document, we're done
return ""
case tt == html.StartTagToken:
t := z.Token()
if t.Data == "h3" {
z.Next()
return strings.TrimSpace(z.Token().Data)
}
}
}
}
func WriteToJson(file string, property string, value string, subproperty ...string) {
f, err := os.Open(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
bb, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
doc := make(map[string]interface{})
if err := json.Unmarshal(bb, &doc); err != nil {
fmt.Println(err)
os.Exit(1)
}
doc[property] = value
data, err := json.MarshalIndent(doc, "", "\t")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = os.WriteFile(file, data, os.ModeAppend)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func PkgInstallCommand(manager string) string {
if manager != "yarn" {
return "install"
} else {
return "add"
}
}
func Copy(dir, project string) {
os.Rename(".disploy/create-disploy-app-main/assets/"+dir, project)
if strings.Contains(project, "framework") {
WriteToJson(project+"/disploy.json", "name", project)
}
if !strings.Contains(project, "deno") {
WriteToJson(project+"/package.json", "name", project)
m, ok := PackageManagerChoiceModel().(PackageManagerOptionStruct)
if !ok {
fmt.Println("Error: PackageManagerChoiceModel() is not PackageManagerOptionStruct")
os.Exit(1)
}
cmd := exec.Command(m.choice, PkgInstallCommand(m.choice), "disploy@dev")
cmd.Dir = project
err := cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
os.RemoveAll(".disploy")
os.Remove(".disploy.zip")
}
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 UnzipFile(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
}