hybridgroup.gobot/system/fs.go

49 lines
1.2 KiB
Go
Raw Normal View History

2022-11-21 02:22:26 +08:00
package system
import (
"os"
"path"
"regexp"
)
2022-11-05 14:42:28 +08:00
// nativeFilesystem represents the native file system implementation
type nativeFilesystem struct{}
2022-11-05 14:42:28 +08:00
// 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)
}
2022-11-05 14:42:28 +08:00
// stat calls os.Stat()
func (fs *nativeFilesystem) stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
2022-11-05 14:42:28 +08:00
// 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
}
2022-11-05 14:42:28 +08:00
// 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)
}