forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
47 lines (39 loc) · 1.24 KB
/
fs.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
package system
import (
"os"
"path"
"regexp"
)
// nativeFilesystem represents the native file system implementation
type nativeFilesystem struct{}
// openFile calls os.OpenFile().
func (fs *nativeFilesystem) openFile(name string, flag int, perm os.FileMode) (file File, err error) {
return os.OpenFile(name, flag, perm)
}
// stat calls os.Stat()
func (fs *nativeFilesystem) stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
// find returns all items (files or folders) below the given directory matching the given pattern.
func (fs *nativeFilesystem) find(baseDir string, pattern string) ([]string, error) {
reg, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
items, err := os.ReadDir(baseDir)
if err != nil {
return nil, err
}
var found []string
for _, item := range items {
if reg.MatchString(item.Name()) {
found = append(found, path.Join(baseDir, item.Name()))
}
}
return found, nil
}
// readFile reads the named file and returns the contents. A successful call returns err == nil, not err == EOF.
// Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
func (fs *nativeFilesystem) readFile(name string) ([]byte, error) {
return os.ReadFile(name)
}