summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-25 23:17:38 +0300
committerPaul Buetow <paul@buetow.org>2024-05-25 23:17:38 +0300
commit6e631b4ccbe71299137469a69900b874d709fe35 (patch)
tree727962808911d945ab903725d450d5101b8135da
parentbee82ce126846acd815cdc51f39f5c344ca432d2 (diff)
refactor vfs into its own package
-rw-r--r--internal/server/repository/repository.go8
-rw-r--r--internal/types/entry.go10
-rw-r--r--internal/vfs/memoryfs.go33
-rw-r--r--internal/vfs/realfs.go (renamed from internal/vfs.go)20
-rw-r--r--internal/vfs/vfs.go8
5 files changed, 53 insertions, 26 deletions
diff --git a/internal/server/repository/repository.go b/internal/server/repository/repository.go
index 029ca53..fa9a5f4 100644
--- a/internal/server/repository/repository.go
+++ b/internal/server/repository/repository.go
@@ -6,8 +6,8 @@ import (
"sync"
"time"
- "codeberg.org/snonux/gos/internal"
"codeberg.org/snonux/gos/internal/types"
+ "codeberg.org/snonux/gos/internal/vfs"
)
var (
@@ -24,7 +24,7 @@ type Repository struct {
dataDir string
entries map[string]types.Entry
mu *sync.Mutex
- vfs internal.VFS
+ vfs vfs.VFS
}
func Instance(dataDir string) *Repository {
@@ -33,7 +33,7 @@ func Instance(dataDir string) *Repository {
dataDir: dataDir,
entries: make(map[string]types.Entry),
mu: &sync.Mutex{},
- vfs: internal.RealFS{},
+ vfs: vfs.RealFS{},
}
})
return instance
@@ -47,7 +47,7 @@ func (r Repository) add(entry types.Entry) {
// Load repository into memory
func (r Repository) load() error {
- filePaths, err := r.vfs.FindFiles(r.dataDir)
+ filePaths, err := r.vfs.FindFiles(r.dataDir, ".json")
if err != nil {
return err
}
diff --git a/internal/types/entry.go b/internal/types/entry.go
index 55391de..b4a05e4 100644
--- a/internal/types/entry.go
+++ b/internal/types/entry.go
@@ -7,7 +7,7 @@ import (
"strings"
"sync"
- "codeberg.org/snonux/gos/internal"
+ "codeberg.org/snonux/gos/internal/vfs"
)
// Tells me whether the entry was shared to the sm platform named Name
@@ -37,7 +37,7 @@ type Entry struct {
Body string `json:"body"`
Shared []Shared `json:"shared,omitempty"`
Epoch int `json:"epoch,omitempty"`
- vfs internal.VFS
+ vfs vfs.VFS
// The checksum of the whole entry, can change depending on the state.
checksum string
@@ -57,11 +57,11 @@ func NewEntry(bytes []byte) (Entry, error) {
return e, nil
}
-func NewEntryFromFile(filePath string, vfsToUse ...internal.VFS) (Entry, error) {
+func NewEntryFromFile(filePath string, vfsToUse ...vfs.VFS) (Entry, error) {
var (
bytes []byte
err error
- vfs internal.VFS = internal.RealFS{}
+ vfs vfs.VFS = vfs.RealFS{}
)
if len(vfsToUse) > 0 {
@@ -84,7 +84,7 @@ func NewEntryFromCopy(other Entry) (Entry, error) {
func (e *Entry) initialize() {
e.mu = &sync.Mutex{}
e.checksumDirty = true
- e.vfs = internal.RealFS{}
+ e.vfs = vfs.RealFS{}
}
func (e Entry) Equals(other Entry) bool {
diff --git a/internal/vfs/memoryfs.go b/internal/vfs/memoryfs.go
new file mode 100644
index 0000000..22b1611
--- /dev/null
+++ b/internal/vfs/memoryfs.go
@@ -0,0 +1,33 @@
+package vfs
+
+import (
+ "fmt"
+ "strings"
+)
+
+type MemoryFS map[string][]byte
+
+func (fs MemoryFS) ReadFile(filePath string) ([]byte, error) {
+ if bytes, ok := fs[filePath]; ok {
+ return bytes, nil
+ }
+ return []byte{}, fmt.Errorf("no such file path: %s", filePath)
+}
+
+func (fs MemoryFS) SaveFile(filePath string, bytes []byte) error {
+ fs[filePath] = bytes
+ return nil
+}
+
+func (fs MemoryFS) FindFiles(dataDir, suffix string) ([]string, error) {
+ var filePaths []string
+
+ for filePath := range fs {
+ if !strings.HasSuffix(filePath, suffix) {
+ continue
+ }
+ filePaths = append(filePaths, filePath)
+ }
+
+ return filePaths, nil
+}
diff --git a/internal/vfs.go b/internal/vfs/realfs.go
index 8b58a91..8b9c9de 100644
--- a/internal/vfs.go
+++ b/internal/vfs/realfs.go
@@ -1,4 +1,4 @@
-package internal
+package vfs
import (
"log"
@@ -7,13 +7,6 @@ import (
"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) {
@@ -30,7 +23,7 @@ func (RealFS) SaveFile(filePath string, bytes []byte) error {
return os.WriteFile(filePath, bytes, 0644)
}
-func (RealFS) FindFiles(dataDir string) ([]string, error) {
+func (RealFS) FindFiles(dataDir, suffix string) ([]string, error) {
var filePaths []string
visit := func() filepath.WalkFunc {
@@ -39,17 +32,10 @@ func (RealFS) FindFiles(dataDir string) ([]string, error) {
log.Println(err)
return nil
}
- if info.IsDir() || !strings.HasSuffix(path, ".json") {
+ if info.IsDir() || !strings.HasSuffix(path, suffix) {
return nil
}
filePaths = append(filePaths, path)
- /*
- entry, err := types.NewEntryFromFile(path)
- if err != err {
- return err
- }
- r.add(entry)
- */
return nil
}
}
diff --git a/internal/vfs/vfs.go b/internal/vfs/vfs.go
new file mode 100644
index 0000000..0297ff9
--- /dev/null
+++ b/internal/vfs/vfs.go
@@ -0,0 +1,8 @@
+package vfs
+
+// virtual file system - useful for testing as well
+type VFS interface {
+ ReadFile(name string) ([]byte, error)
+ SaveFile(filePath string, bytes []byte) error
+ FindFiles(dataPath, suffix string) ([]string, error)
+}