-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.go
110 lines (95 loc) · 2.16 KB
/
dir.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
package flo
import (
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"github.com/toxyl/flo/errors"
"github.com/toxyl/flo/log"
"github.com/toxyl/flo/permissions"
)
type DirObj struct {
*FileObj
dirs []*DirObj
files []*FileObj
}
func (f *FileObj) Mkdir(mode fs.FileMode) error {
p := f.path
if f.Exists() && !f.Permissions().IsDir() {
p = filepath.Dir(p)
}
if err := os.MkdirAll(p, mode); err != nil && !os.IsExist(err) {
return err
}
defer f.updateInfo()
return nil
}
func (d *DirObj) Contents() *DirObj {
if !d.Permissions().IsDir() {
log.Error(errors.ErrIsNotDirectory(d.Path()), "Retrieving contents failed!")
return d
}
files := []*FileObj{}
dirs := []*DirObj{}
contents, err := os.ReadDir(d.Path())
if err != nil {
return d
}
for _, file := range contents {
path := filepath.Join(d.Path(), file.Name())
p := permissions.New(path)
if p.IsDir() {
dirs = append(dirs, Dir(path))
continue
}
files = append(files, File(path))
}
d.dirs = dirs
d.files = files
sort.Slice(d.dirs, func(i, j int) bool {
return strings.ToLower(d.dirs[i].Name()) < strings.ToLower(d.dirs[j].Name())
})
sort.Slice(d.files, func(i, j int) bool {
return strings.ToLower(d.files[i].Name()) < strings.ToLower(d.files[j].Name())
})
return d
}
func (d *DirObj) Dirs() []*DirObj {
return d.dirs
}
func (d *DirObj) Files() []*FileObj {
return d.files
}
func (d *DirObj) walk(fnFile func(f *FileObj), fnDir func(d *DirObj), depth, maxDepth int) {
if maxDepth >= 0 && depth > maxDepth {
return
}
contents := d.Contents()
for _, dir := range contents.Dirs() {
if fnDir != nil {
fnDir(dir)
}
dir.walk(fnFile, fnDir, depth+1, maxDepth)
}
for _, file := range contents.Files() {
if fnFile != nil {
fnFile(file)
}
}
}
func (d *DirObj) Each(fnFile func(f *FileObj), fnDir func(d *DirObj)) {
d.walk(fnFile, fnDir, 0, -1)
}
func (d *DirObj) EachLimit(fnFile func(f *FileObj), fnDir func(d *DirObj), maxDepth int) {
d.walk(fnFile, fnDir, 0, maxDepth)
}
func newDir(path string) *DirObj {
d := &DirObj{
FileObj: newFile(path),
dirs: []*DirObj{},
files: []*FileObj{},
}
return d
}
func Dir(path string) *DirObj { return newDir(path) }