-
Notifications
You must be signed in to change notification settings - Fork 8
/
ioutilmore.go
456 lines (414 loc) · 10 KB
/
ioutilmore.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package ioutilmore
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/grokify/simplego/encoding/jsonutil"
"github.com/grokify/simplego/type/maputil"
"github.com/pkg/errors"
)
type FileType int
const (
File FileType = iota
Directory
Any
)
func CopyFile(src, dst string) (err error) {
r, err := os.Open(src)
if err != nil {
return
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return
}
defer func() {
if e := w.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(w, r)
if err != nil {
return
}
err = w.Sync()
if err != nil {
return
}
si, err := os.Stat(src)
if err != nil {
return
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return
}
return
}
func ReadDirSplit(dirname string, inclDotDirs bool) ([]os.FileInfo, []os.FileInfo, error) {
all, err := ioutil.ReadDir(dirname)
if err != nil {
return []os.FileInfo{}, []os.FileInfo{}, err
}
subdirs, regular := FileInfosSplit(all, inclDotDirs)
return subdirs, regular, nil
}
func FileInfosSplit(all []os.FileInfo, inclDotDirs bool) ([]os.FileInfo, []os.FileInfo) {
subdirs := []os.FileInfo{}
regular := []os.FileInfo{}
for _, f := range all {
if f.Mode().IsDir() {
if f.Name() == "." && f.Name() == ".." {
if inclDotDirs {
subdirs = append(subdirs, f)
}
} else {
subdirs = append(subdirs, f)
}
} else {
regular = append(regular, f)
}
}
return subdirs, regular
}
// DirEntriesNameRxVarFirsts returns a slice of the first
// regexp match encountered.
func DirEntriesNameRxVarFirsts(dir string, rx1 *regexp.Regexp) ([]string, error) {
vars := map[string]int{}
varsMatch := []string{}
filesAll, err := ioutil.ReadDir(dir)
if err != nil {
return varsMatch, err
}
for _, f := range filesAll {
if f.Name() == "." || f.Name() == ".." {
continue
}
if f.Size() > int64(0) {
rs1 := rx1.FindStringSubmatch(f.Name())
if len(rs1) > 1 { // len = 2+
vars[rs1[1]] = 1
//filesMatch = append(filesMatch, f)
}
}
}
for varVal := range vars {
varsMatch = append(varsMatch, varVal)
}
return varsMatch, nil
}
func ReadDirRx(dir string, rx *regexp.Regexp, skipEmpty bool) ([]os.FileInfo, []string, error) {
filesMatch := []os.FileInfo{}
filenames := []string{}
filesAll, err := ioutil.ReadDir(dir)
if err != nil {
return filesMatch, filenames, err
}
for _, f := range filesAll {
if f.Name() == "." || f.Name() == ".." {
continue
}
if (skipEmpty && f.Size() > int64(0)) || !skipEmpty {
rs := rx.FindStringSubmatch(f.Name())
if len(rs) > 0 {
filesMatch = append(filesMatch, f)
filenames = append(filenames, filepath.Join(dir, f.Name()))
}
}
}
return filesMatch, filenames, nil
}
func DirEntriesRxSizeGt0Filepaths(dir string, fileFilter FileType, rx *regexp.Regexp) ([]string, error) {
fileinfos, err := DirEntriesRxSizeGt0(dir, fileFilter, rx)
if err != nil {
return []string{}, err
}
filepaths := []string{}
for _, fi := range fileinfos {
filepaths = append(filepaths, filepath.Join(dir, fi.Name()))
}
return filepaths, nil
}
func DirEntriesRxSizeGt0(dir string, fileFilter FileType, rx1 *regexp.Regexp) ([]os.FileInfo, error) {
filesMatch := []os.FileInfo{}
filesAll, err := ioutil.ReadDir(dir)
if err != nil {
return filesMatch, err
}
for _, fi := range filesAll {
if fi.Name() == "." || fi.Name() == ".." {
continue
} else if fileFilter == Directory && !fi.Mode().IsDir() {
continue
} else if fileFilter == File && !fi.Mode().IsRegular() {
continue
} else if fi.Size() <= int64(0) {
continue
}
rs1 := rx1.FindStringSubmatch(fi.Name())
if len(rs1) > 0 {
filesMatch = append(filesMatch, fi)
}
}
return filesMatch, nil
}
// DirEntriesRegexpGreatest takes a directory, regular expression and boolean to indicate
// whether to include zero size files and returns the greatest of a single match in the
// regular expression.
func DirFilesRegexpSubmatchGreatest(dir string, rx1 *regexp.Regexp, nonZeroFilesOnly bool) ([]os.FileInfo, error) {
files := map[string][]os.FileInfo{}
filesAll, e := ioutil.ReadDir(dir)
if e != nil {
return []os.FileInfo{}, e
}
for _, f := range filesAll {
if f.Name() == "." || f.Name() == ".." ||
(nonZeroFilesOnly && f.Size() <= int64(0)) {
continue
}
if rs1 := rx1.FindStringSubmatch(f.Name()); len(rs1) > 1 {
extract := rs1[1]
if _, ok := files[extract]; !ok {
files[extract] = []os.FileInfo{}
}
files[extract] = append(files[extract], f)
}
}
keysSorted := maputil.StringKeysSorted(files)
greatest := keysSorted[len(keysSorted)-1]
return files[greatest], nil
}
// DirFilesRegexpSubmatchGreatestSubmatch takes a directory, regular expression and boolean to indicate
// whether to include zero size files and returns the greatest of a single match in the
// regular expression.
func DirFilesRegexpSubmatchGreatestSubmatch(dir string, rx1 *regexp.Regexp, nonZeroFilesOnly bool) (string, error) {
filesAll, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
strs := []string{}
for _, f := range filesAll {
if nonZeroFilesOnly && f.Size() <= int64(0) {
continue
}
rs1 := rx1.FindStringSubmatch(f.Name())
if len(rs1) > 1 {
strs = append(strs, rs1[1])
}
}
sort.Strings(strs)
if len(strs) == 0 {
return "", fmt.Errorf("No matches found")
}
return strs[len(strs)-1], nil
}
func DirFromPath(path string) (string, error) {
path = strings.TrimRight(path, "/\\")
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return "", err
}
isFile := false
switch mode := fi.Mode(); {
case mode.IsDir():
return path, nil
case mode.IsRegular():
isFile = true
}
if isFile == false {
return "", nil
}
rx1 := regexp.MustCompile(`^(.+)[/\\][^/\\]+`)
rs1 := rx1.FindStringSubmatch(path)
dir := ""
if len(rs1) > 1 {
dir = rs1[1]
}
return dir, nil
}
func IsDir(name string) (bool, error) {
if fi, err := os.Stat(name); err != nil {
return false, err
} else if !fi.Mode().IsDir() {
return false, nil
}
return true, nil
}
func IsFile(name string) (bool, error) {
if fi, err := os.Stat(name); err != nil {
return false, err
} else if !fi.Mode().IsRegular() {
return false, nil
}
return true, nil
}
func Exists(name string) (bool, error) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
return err != nil, err
}
// IsFileWithSizeGtZero verifies a path exists, is a file and is not empty,
// returning an error otherwise. An os file not exists check can be done
// with os.IsNotExist(err) which acts on error from os.Stat()
func IsFileWithSizeGtZero(name string) (bool, error) {
if fi, err := os.Stat(name); err != nil {
return false, err
} else if !fi.Mode().IsRegular() {
return false, nil
// return fmt.Errorf("Filepath [%v] exists but is not a file.", name)
} else if fi.Size() <= 0 {
return false, nil
// return fmt.Errorf("Filepath [%v] exists but is empty with size [%v].", name, fi.Size())
}
return true, nil
}
func SplitBetter(path string) (dir, file string) {
isDir, err := IsDir(path)
if err != nil && isDir {
return dir, ""
}
return filepath.Split(path)
}
func SplitBest(path string) (dir, file string, err error) {
isDir, err := IsDir(path)
if err != nil {
return "", "", err
} else if isDir {
return path, "", nil
}
isFile, err := IsFile(path)
if err != nil {
return "", "", err
} else if isFile {
dir, file := filepath.Split(path)
return dir, file, nil
}
return "", "", fmt.Errorf("Path is valid but not file or directory: [%v]", path)
}
func FileinfosToFilepaths(dir string, fileInfos []os.FileInfo) []string {
dir = strings.TrimSpace(dir)
paths := []string{}
for _, fi := range fileInfos {
if len(dir) > 0 {
paths = append(paths, filepath.Join(dir, fi.Name()))
} else {
paths = append(paths, fi.Name())
}
}
return paths
}
func FilterFilenamesSizeGtZero(filepaths ...string) []string {
filepathsExist := []string{}
for _, envPathVal := range filepaths {
envPathVals := strings.Split(envPathVal, ",")
for _, envPath := range envPathVals {
envPath = strings.TrimSpace(envPath)
if isFile, err := IsFileWithSizeGtZero(envPath); isFile && err == nil {
filepathsExist = append(filepathsExist, envPath)
}
}
}
return filepathsExist
}
func RemoveAllChildren(dir string) error {
isDir, err := IsDir(dir)
if err != nil {
return err
}
if isDir == false {
err = errors.New("400: Path Is Not Directory")
return err
}
filesAll, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range filesAll {
if fi.Name() == "." || fi.Name() == ".." {
continue
}
filepath := path.Join(dir, fi.Name())
if fi.IsDir() {
err = os.RemoveAll(filepath)
if err != nil {
return err
}
} else {
err = os.Remove(filepath)
if err != nil {
return err
}
}
}
return nil
}
func FileinfosNames(fis []os.FileInfo) []string {
s := []string{}
for _, e := range fis {
s = append(s, e.Name())
}
return s
}
// ReaderToBytes reads from an io.Reader, e.g. io.ReadCloser
func ReaderToBytes(ior io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(ior)
return buf.Bytes()
}
// ReadFileJSON reads and unmarshals a file.
func ReadFileJSON(file string, v interface{}) error {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return json.Unmarshal(bytes, v)
}
func WriteFileJSON(filepath string, data interface{}, perm os.FileMode, prefix, indent string) error {
bytes, err := jsonutil.MarshalSimple(data, prefix, indent)
if err != nil {
return err
}
return ioutil.WriteFile(filepath, bytes, perm)
}
func CloseFileWithError(file *os.File, err error) error {
errFile := file.Close()
if err != nil {
return errors.Wrap(err, errFile.Error())
}
return err
}
type FileWriter struct {
File *os.File
Writer *bufio.Writer
}
func NewFileWriter(path string) (FileWriter, error) {
fw := FileWriter{}
file, err := os.Create(path)
if err != nil {
return fw, err
}
fw.File = file
fw.Writer = bufio.NewWriter(file)
return fw, nil
}
func (f *FileWriter) Close() {
f.Writer.Flush()
f.File.Close()
}