fix: uneeded func + uneeded bogus note creation logic
This commit is contained in:
@ -7,7 +7,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
// "donniemarko/internal/note"
|
||||
)
|
||||
|
||||
type ChangeType int
|
||||
@ -50,28 +49,6 @@ func (s *ScannerService) SetHandler(handler ChangeHandler) {
|
||||
s.handler = handler
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Scan walks the root folder and update the states of each notes if
|
||||
// it has changed since the last time a scan occured
|
||||
func (s *ScannerService) Scan() ([]Change, error) {
|
||||
@ -86,6 +63,7 @@ func (s *ScannerService) Scan() ([]Change, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ignore anything that isn't a note
|
||||
if !isValidNoteFile(path, info) {
|
||||
return nil
|
||||
}
|
||||
@ -95,7 +73,7 @@ func (s *ScannerService) Scan() ([]Change, error) {
|
||||
lastMod, existed := s.LastStates[path]
|
||||
if !existed {
|
||||
// create the note if it didn't exist yet
|
||||
s.handler.HandleCreate(path)
|
||||
// s.handler.HandleCreate(path)
|
||||
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()})
|
||||
@ -115,8 +93,7 @@ func (s *ScannerService) Scan() ([]Change, error) {
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
// Monitor rescan the root folder at each new tick and handle state
|
||||
// modification
|
||||
// Monitor rescan the root folder at each new tick and handle state modifications
|
||||
func (s *ScannerService) Monitor(ctx context.Context) error {
|
||||
ticker := time.NewTicker(s.Interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
@ -7,36 +7,6 @@ import (
|
||||
"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)
|
||||
|
||||
Reference in New Issue
Block a user