forked from zpsean/go4api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
336 lines (270 loc) · 7.36 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* go4api - an api testing tool written in Go
* Created by: Ping Zhu 2018
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package utils
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"io"
"strings"
"strconv"
"path/filepath"
"encoding/csv"
"encoding/base64"
)
func GetCurrentDir() string{
// get current dir,
// Note: here may be a bug if run the main.go on other path, need to use abs path
currentDir, err := os.Getwd()
if err != nil {
panic(err)
}
return currentDir
}
func GetCsvFromFile(filePath string) [][]string {
fi, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("!! Error: ", filePath)
panic(err)
}
reader := csv.NewReader(strings.NewReader(string(fi)))
// the csv may contain row for comments, which is leading with # (rune), to ignore
reader.Comment = '#'
csvRows, err := reader.ReadAll()
if err != nil {
fmt.Println("!! Error: ", filePath)
panic(err)
}
return csvRows
}
func GetContentFromFile(filePath string) []byte {
fi,err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
// contents := strings.NewReader(string(fi))
return fi
}
func GetJsonFromFile(filePath string) string {
fi, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer fi.Close()
fd, err := ioutil.ReadAll(fi)
if err != nil {
panic(err)
}
return string(fd)
}
// for path contains ~
func GetAbsPath (path string) string {
var absPath string
u, _ := user.Current()
if path == "~" {
absPath = u.HomeDir
} else if strings.HasPrefix(path, "~/") {
absPath = filepath.Join(u.HomeDir, path[2:])
} else {
absPath = path
}
return absPath
}
func GetAbsPaths (paths []string) []string {
var absPaths []string
for _, path := range paths {
u, _ := user.Current()
if path == "~" {
p := u.HomeDir
absPaths = append(absPaths, p)
} else if strings.HasPrefix(path, "~/") {
p := filepath.Join(u.HomeDir, path[2:])
absPaths = append(absPaths, p)
} else {
absPaths = append(absPaths, path)
}
}
return absPaths
}
//
func GetOsEnviron () map[string]string {
envMap := map[string]string{}
var envArray []string
envArray = os.Environ()
for _, env := range envArray {
// find out the first = position, to get the key
env_k := strings.Split(env, "=")[0]
envMap[env_k] = os.Getenv(env_k)
}
// incase the env variables have the prefix "go4_*"
// for _, env := range envArray {
// // find out the first = position, to get the key
// env_k := strings.Split(env, "=")[0]
// if strings.HasPrefix(env_k, "go4_") {
// if strings.TrimLeft(env_k, "go4_") != "" {
// envMap[strings.TrimLeft(env_k, "go4_")] = os.Getenv(env_k)
// }
// }
// }
return envMap
}
// for the dir and sub-dir
func WalkPath(searchDir string, extension string) ([]string, error) {
fileList := make([]string, 0)
e := filepath.Walk(searchDir, func(subPath string, f os.FileInfo, err error) error {
if filepath.Ext(subPath) == extension {
fileList = append(fileList, subPath)
}
return err
})
if e != nil {
fmt.Println("Err, search files under ", searchDir, " failed")
panic(e)
}
// for _, file := range fileList {
// fmt.Println(file)
// }
return fileList, nil
}
func FileCopy(src string, dest string, info os.FileInfo) error {
f, err := os.Create(dest)
if err != nil {
return err
}
defer f.Close()
if err = os.Chmod(f.Name(), info.Mode())
err != nil {
return err
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
_, err = io.Copy(f, s)
return err
}
func DirCopy(src string, dest string, info os.FileInfo) error {
if err := os.MkdirAll(dest, info.Mode())
err != nil {
return err
}
infos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, info := range infos {
if err := FileCopy(filepath.Join(src, info.Name()), filepath.Join(dest, info.Name()), info)
err != nil {
return err
}
}
return nil
}
func ConvertIntArrayToStringArray(intArray []int) []string {
var stringArray []string
for _, k := range intArray{
ii := strconv.Itoa(k)
stringArray = append(stringArray, ii)
}
return stringArray
}
func ConvertStringArrayToIntArray(stringArray []string) []int {
var intArray []int
for _, k := range stringArray{
ii, _ := strconv.Atoi(k)
intArray = append(intArray, ii)
}
return intArray
}
func GenerateFileBasedOnVarAppend(strVar string, filePath string) {
outFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
defer outFile.Close()
outFile.WriteString(strVar)
}
func GenerateFileBasedOnVarOverride(strVar string, filePath string) {
outFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer outFile.Close()
outFile.WriteString(strVar)
}
func GenerateCsvFileBasedOnVarAppend(strVarSlice []string, filePath string) {
outFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
defer outFile.Close()
// UTF-8 BOM
// outFile.WriteString("\xEF\xBB\xBF")
w := csv.NewWriter(outFile)
w.Write(strVarSlice)
//
w.Flush()
}
func GenerateCsvFileBasedOnVarOverride(strVarSlice []string, filePath string) {
outFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer outFile.Close()
// UTF-8 BOM
// outFile.WriteString("\xEF\xBB\xBF")
w := csv.NewWriter(outFile)
w.Write(strVarSlice)
//
w.Flush()
}
func GeneratePicture(bytesVar []byte, filePath string) {
outFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer outFile.Close()
outFile.Write(bytesVar)
}
func DecodeBase64(b64 string) []byte {
sDec, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
panic(err)
}
return sDec
}
func CreateTempDir (filePath string) string {
err := os.MkdirAll(filepath.Dir(filePath) + "/temp", 0777)
if err != nil {
panic(err)
}
return filepath.Dir(filePath) + "/temp"
}
func CheckFilesExistence(fileList []string) bool {
ifExist := true
for _, filePath := range fileList {
_, err := os.Stat(filePath)
if err != nil {
ifExist = false
break
}
}
return ifExist
}
func CheckFileExistence(filePath string) bool {
ifExist := true
_, err := os.Stat(filePath)
if err != nil {
ifExist = false
}
return ifExist
}