-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
226 lines (185 loc) · 4.35 KB
/
utils.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
/*
(c) Copyright 2018, Gemalto. All rights reserved.
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 utils
import (
"archive/tar"
"archive/zip"
"bufio"
"compress/gzip"
"io"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
)
// EnvVar ...
type EnvVar struct {
Name string
Value string
}
// CreateDirs ...
func CreateDirs(dirPath string) {
// Check if file already exist
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
panic(err)
}
}
}
// MoveFile ...
func MoveFile(oldPath string, newPath string) {
err := os.Rename(oldPath, newPath)
if err != nil {
panic(err)
}
}
// CleanDir ...
func CleanDir(dirPath string) {
dir, err := ioutil.ReadDir(dirPath)
if err == nil {
for _, e := range dir {
err := os.RemoveAll(path.Join([]string{dirPath, e.Name()}...))
if err != nil {
panic(err)
}
}
}
}
// RemoveDir ...
func RemoveDir(dirPath string) {
err := os.RemoveAll(dirPath)
if err != nil {
panic(err)
}
}
// RemoveFile ...
func RemoveFile(filePath string) {
err := os.RemoveAll(filePath)
if err != nil {
panic(err)
}
}
// Untar takes a destination path and a reader; a tar reader loops over the tarfile
// creating the file structure at 'dst' along the way, and writing any files
func Untar(src string, dst string) error {
file, err := os.Open(src)
defer file.Close()
if err != nil {
panic(err)
}
bufio.NewReader(file)
gzr, err := gzip.NewReader(bufio.NewReader(file))
defer gzr.Close()
if err != nil {
return err
}
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:
if _, err := os.Stat(filepath.Dir(target)); err != nil {
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
return err
}
}
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
defer f.Close()
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
}
}
}
// Unzip ...
func Unzip(src string, dest string) error {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
} else {
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
if err != nil {
return err
}
}
}
return nil
}
// GetAppDataHome ...
func GetAppDataHome() string {
return os.Getenv("APPDATA")
}
// GetUserHome ...
func GetUserHome() string {
user, err := user.Current()
if err != nil {
panic(err)
}
return user.HomeDir
}