1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-11-20 19:22:26 +01:00
package system
import (
"os"
"path"
"regexp"
)
2022-11-05 07:42:28 +01:00
// nativeFilesystem represents the native file system implementation
type nativeFilesystem struct{}
2022-11-05 07:42:28 +01:00
// openFile calls os.OpenFile().
func (fs *nativeFilesystem) openFile(name string, flag int, perm os.FileMode) (File, error) {
return os.OpenFile(name, flag, perm)
}
2022-11-05 07:42:28 +01:00
// stat calls os.Stat()
func (fs *nativeFilesystem) stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
2022-11-05 07:42:28 +01: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 07:42:28 +01: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)
}