Additions to domain/ and workspace/ chron-note packages
This commit is contained in:
52
chron-note/internal/workspace/snapshot.go
Normal file
52
chron-note/internal/workspace/snapshot.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package workspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.michelsen.id/phill/chron/chron-note/internal/domain"
|
||||
)
|
||||
|
||||
type SnapshotObject struct {
|
||||
ID domain.ObjectID
|
||||
Size int64
|
||||
ModNS int64
|
||||
}
|
||||
|
||||
func (ws *Workspace) Snapshot(ctx context.Context) ([]SnapshotObject, error) {
|
||||
entries, err := os.ReadDir(ws.Dir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("readdir workspace dir: %w", err)
|
||||
}
|
||||
|
||||
out := make([]SnapshotObject, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stat workspace entry: %w", err)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
|
||||
id, err := domain.ParseObjectID(e.Name())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
out = append(out, SnapshotObject{
|
||||
ID: id,
|
||||
Size: info.Size(),
|
||||
ModNS: info.ModTime().UnixNano(),
|
||||
})
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user