-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
316 lines (253 loc) · 6.58 KB
/
helper.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright © 2021 Borko Djurkovic <borkod@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package pkg is for implementing commands
package pkg
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v2"
)
// GetYaml downloads the yaml and configures the KindlyStruct struct
func getYaml(ctx context.Context, arg string) (KindlyStruct, error) {
const ConnectMaxWaitTime = 1 * time.Second
const RequestMaxWaitTime = 5 * time.Second
client := http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: ConnectMaxWaitTime,
}).DialContext,
},
}
var yc KindlyStruct
buf := new(bytes.Buffer)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, arg, nil)
if err != nil {
return yc, err
}
resp, err := client.Do(req)
if err != nil {
return yc, err
}
defer resp.Body.Close()
if _, err = buf.ReadFrom(resp.Body); err != nil {
//fmt.Printf("Error downloading file: %s\n", arg)
return yc, err
}
yaml.Unmarshal(buf.Bytes(), &yc)
if err != nil {
//fmt.Printf("Error parsing YAML file: %s\n", arg)
return yc, err
}
return yc, nil
}
// decompress decompresses a file
func decompress(dst string, path string) error {
//if cfg.Verbose {
// k.logger.Println("Decompressing file:\t\t", path)
//}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)
// the following switch could also be done using fi.Mode(), not sure if there
// a benefit of using one vs. the other.
// fi := header.FileInfo()
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
// if it's a file create it
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
//if cfg.Verbose {
// k.logger.Println("Writing file:\t\t\t", target)
//}
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
}
}
}
// copyFile copies file to dst from src
func copyFile(dst string, src string, binName string) error {
//if cfg.Verbose {
// k.logger.Println("Copying file:\t\t", binName)
//}
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if info.Name() == binName && !info.IsDir() {
sourceFile, err := os.Open(path)
if err != nil {
return err
}
defer sourceFile.Close()
// Create the output directory
//if cfg.UniqueDir {
// dirPath := filepath.Join(dst, info.Name())
// if f, err := os.Stat(dirPath); os.IsNotExist(err) || !f.IsDir() {
// os.Mkdir(dirPath, os.ModePerm)
// }
// dst = dirPath
//}
if _, err := os.Stat(dst); os.IsNotExist(err) {
if err2 := os.MkdirAll(dst, os.ModePerm); err2 != nil {
return err2
}
} else if err != nil {
return err
}
newFile, err := os.Create(filepath.Join(dst, info.Name()))
if err != nil {
return err
}
defer newFile.Close()
_, err = io.Copy(newFile, sourceFile)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func writeManifest(src pkgManifest, dst string) error {
filename := filepath.Join(dst, src.Name+".yaml")
file, err := yaml.Marshal(src)
if err != nil {
return err
}
if _, err := os.Stat(dst); os.IsNotExist(err) {
if err2 := os.MkdirAll(dst, os.ModePerm); err2 != nil {
return err2
}
} else if err != nil {
return err
}
if err = ioutil.WriteFile(filename, file, 0644); err != nil {
return err
}
return nil
}
// Unzip will decompress a zip archive, moving all files and folders within the zip file (parameter 1) to an output directory (parameter 2)
func unzip(src string, dest string) ([]string, error) {
//if cfg.Verbose {
// k.logger.Println("Unzipping file:\t\t", src)
//}
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
continue
}
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
rc, err := f.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
return filenames, nil
}
// ExpandPath is helper function to expand file location
func expandPath(path string) string {
if filepath.IsAbs(path) {
return path
}
if strings.HasPrefix(path, "~/") {
user, err := user.Current()
if err != nil {
return path
}
return filepath.Join(user.HomeDir, path[2:])
}
abspath, err := filepath.Abs(path)
if err != nil {
return path
}
return abspath
}