diff options
| author | Paul Buetow <paul@buetow.org> | 2024-05-25 22:56:35 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-05-25 22:56:35 +0300 |
| commit | bee82ce126846acd815cdc51f39f5c344ca432d2 (patch) | |
| tree | f8684c3b225af8a1c2fb07d1872bbaa5cfade615 /internal | |
| parent | b3bddfd992c33a02628d4a5d0586774228776dc9 (diff) | |
initial VFS
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/vfs.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/vfs.go b/internal/vfs.go new file mode 100644 index 0000000..8b58a91 --- /dev/null +++ b/internal/vfs.go @@ -0,0 +1,58 @@ +package internal + +import ( + "log" + "os" + "path/filepath" + "strings" +) + +// virtual file system - useful for testing as well +type VFS interface { + ReadFile(name string) ([]byte, error) + SaveFile(filePath string, bytes []byte) error + FindFiles(dataPath string) ([]string, error) +} + +type RealFS struct{} + +func (RealFS) ReadFile(filePath string) ([]byte, error) { + return os.ReadFile(filePath) +} + +func (RealFS) SaveFile(filePath string, bytes []byte) error { + dir := filepath.Dir(filePath) + if _, err := os.Stat(dir); os.IsNotExist(err) { + if err := os.MkdirAll(dir, 0755); err != nil { + return err + } + } + return os.WriteFile(filePath, bytes, 0644) +} + +func (RealFS) FindFiles(dataDir string) ([]string, error) { + var filePaths []string + + visit := func() filepath.WalkFunc { + return func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Println(err) + return nil + } + if info.IsDir() || !strings.HasSuffix(path, ".json") { + return nil + } + filePaths = append(filePaths, path) + /* + entry, err := types.NewEntryFromFile(path) + if err != err { + return err + } + r.add(entry) + */ + return nil + } + } + + return filePaths, filepath.Walk(dataDir, visit()) +} |
