-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile.go
123 lines (102 loc) · 2.08 KB
/
file.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
package fir
import (
"embed"
"io/fs"
"os"
"path"
"path/filepath"
"slices"
)
type readFileFunc func(string) (string, []byte, error)
type existFileFunc func(string) bool
func find(path string, extensions []string, embedfs *embed.FS) []string {
var files []string
var fi fs.FileInfo
var err error
if embedfs != nil {
fi, err = fs.Stat(*embedfs, path)
if err != nil {
return files
}
} else {
fi, err = os.Stat(path)
if err != nil {
return files
}
}
if !fi.IsDir() {
if !slices.Contains(extensions, filepath.Ext(path)) {
return files
}
files = append(files, path)
return files
}
if embedfs != nil {
err = fs.WalkDir(*embedfs, path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if slices.Contains(extensions, filepath.Ext(d.Name())) {
files = append(files, path)
}
return nil
})
if err != nil {
panic(err)
}
} else {
err = filepath.WalkDir(path, func(fpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if slices.Contains(extensions, filepath.Ext(d.Name())) {
files = append(files, fpath)
}
return nil
})
if err != nil {
panic(err)
}
}
return files
}
func isDir(path string, embedfs *embed.FS) bool {
if embedfs != nil {
fileInfo, err := fs.Stat(*embedfs, path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
func readFileOS(file string) (name string, b []byte, err error) {
name = filepath.Base(file)
b, err = os.ReadFile(file)
return
}
func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
return func(file string) (name string, b []byte, err error) {
name = path.Base(file)
b, err = fs.ReadFile(fsys, file)
return
}
}
func existFileOS(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
func existFileFS(fsys fs.FS) func(string) bool {
return func(path string) bool {
if _, err := fs.Stat(fsys, path); err != nil {
return false
}
return true
}
}