draft shits

This commit is contained in:
2026-02-01 11:55:16 +01:00
parent d17ed8c650
commit e27aadc603
22 changed files with 1341 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package scanner
import (
"donniemarko/internal/note"
"donniemarko/internal/storage"
"os"
)
type NotesHandler struct {
storage storage.Storage
}
func (h *NotesHandler) HandleCreate(path string) error {
note, err := ParseNoteFile(path)
if err != nil {
return err
}
h.storage.Create(note)
return nil
}
func (h *NotesHandler) HandleModify(path string) error {
return h.HandleCreate(path)
}
func (h *NotesHandler) HandleDelete(path string) error {
id := note.GenerateNoteID(path)
h.storage.Delete(id)
return nil
}
func ParseNoteFile(path string) (*note.Note, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
id := note.GenerateNoteID(path)
nn := note.NewNote()
nn.ID = id
nn.Content = string(content)
return nn, nil
}

165
internal/scanner/scanner.go Normal file
View File

@ -0,0 +1,165 @@
package scanner
import (
"context"
"log"
"os"
"path/filepath"
"strings"
"time"
)
type ChangeType int
const (
Unchanged ChangeType = iota
Created
Modified
Deleted
)
type ChangeHandler interface {
HandleCreate(path string) error
HandleModify(path string) error
HandleDelete(path string) error
}
type Change struct {
Type ChangeType
Path string
ModTime time.Time
}
type ScannerService struct {
RootDir string
interval time.Duration
lastStates map[string]time.Time
handler ChangeHandler
}
func NewScanner(path string) *ScannerService {
return &ScannerService{RootDir: path}
}
func (s *ScannerService) FindAll() ([]string, error) {
var notePath []string
err := filepath.Walk(s.RootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// skip the root dir itself
if s.RootDir == path {
return nil
}
if !isValidNoteFile(path, info) {
return nil
}
notePath = append(notePath, path)
return nil
})
return notePath, err
}
func (s *ScannerService) Scan() ([]Change, error) {
var changes []Change
currentStates := make(map[string]time.Time)
// Walk filesystem
filepath.Walk(s.RootDir, func(path string, info os.FileInfo, err error) error {
// skip the root dir itself
if s.RootDir == path {
return nil
}
if !isValidNoteFile(path, info) {
return nil
}
currentStates[path] = info.ModTime()
lastMod, existed := s.lastStates[path]
if !existed {
changes = append(changes, Change{Type: Created, Path: path, ModTime: lastMod})
} else if info.ModTime().After(lastMod) {
changes = append(changes, Change{Type: Modified, Path: path, ModTime: info.ModTime()})
}
return nil
})
// Check for deletions
for path := range s.lastStates {
if _, exists := currentStates[path]; !exists {
changes = append(changes, Change{Type: Deleted, Path: path})
}
}
s.lastStates = currentStates
return changes, nil
}
func (s *ScannerService) Monitor(ctx context.Context) error {
ticker := time.NewTicker(s.interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
changes, err := s.Scan()
if err != nil {
log.Printf("scan error: %v", err)
continue
}
for _, change := range changes {
var err error
switch change.Type {
case Created:
err = s.handler.HandleCreate(change.Path)
case Modified:
err = s.handler.HandleModify(change.Path)
case Deleted:
err = s.handler.HandleDelete(change.Path)
}
if err != nil {
log.Printf("handler error for %s: %v", change.Path, err)
}
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func isValidNoteFile(path string, info os.FileInfo) bool {
// ignore temp and backup files
for _, nono := range []string{".#", "~"} {
if strings.Contains(path, nono) {
return false
}
}
// ignore files that are not markdown
if info.IsDir() || filepath.Ext(path) != ".md" {
return false
}
// ignore empty folder
if info.IsDir() {
files, err := os.ReadDir(path)
if err != nil {
log.Println(err.Error())
}
if len(files) == 0 {
return false
}
}
return true
}

View File

@ -0,0 +1,88 @@
package scanner
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestScanner_FindMarkdownFiles(t *testing.T) {
tmpDir := t.TempDir()
files := map[string]string{
"note1.md": "# Note 1",
"note2.markdown": "# Note 2",
"folder/note3.md": "# Note 3",
"folder/nested/note4.md": "# Note 4",
"readme.txt": "not markdown",
}
for path, content := range files {
fullPath := filepath.Join(tmpDir, path)
os.MkdirAll(filepath.Dir(fullPath), 0755)
os.WriteFile(fullPath, []byte(content), 0644)
}
scanner := NewScanner(tmpDir)
found, err := scanner.FindAll()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should only return markdown files
if len(found) != 3 {
t.Errorf("expected 4 files, got %d", len(found))
}
}
func TestScanner_DetectsNewFile(t *testing.T) {
tmpDir := t.TempDir()
scanner := NewScanner(tmpDir)
scanner.Scan() // Initial scan
os.WriteFile(filepath.Join(tmpDir, "new.md"), []byte("# New"), 0644)
changes, _ := scanner.Scan()
if len(changes) != 1 || changes[0].Type != Created {
t.Error("should detect new file")
}
}
func TestScanner_DetectChanges(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "test.md")
// Initial state
os.WriteFile(filePath, []byte("# Original"), 0644)
scanner := NewScanner(tmpDir)
changes, _ := scanner.Scan()
originalModTime := changes[0].ModTime
// Wait and modify
time.Sleep(10 * time.Millisecond)
os.WriteFile(filePath, []byte("# Modified"), 0644)
changes, _ = scanner.Scan()
newModTime := changes[0].ModTime
if !newModTime.After(originalModTime) {
t.Error("should detect file modification")
}
if changes[0].Type != Modified {
t.Errorf("Last state should be modified, got '%v'\n", changes[0].Type)
}
newPath := filepath.Join(tmpDir, "test_renamed.md")
os.Rename(filePath, newPath)
changes, _ = scanner.Scan()
if changes[0].Path != newPath {
t.Errorf("Should find renamed file '%s'. Got '%s'\n", newPath, changes[0].Path)
}
}