diff --git a/README.md b/README.md index c2b4696..edf9aa9 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ go run main.go go run main.go search go run main.go add "Just a test" go run main.go archive /path/to/2026/01/11/17.28_wip +go run main.go search -a ``` ## Maintenance diff --git a/core/searching.go b/core/searching.go index 327df80..070db99 100644 --- a/core/searching.go +++ b/core/searching.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "io/fs" "os" "path/filepath" "regexp" @@ -19,11 +20,17 @@ var isoDateTimeBasicFormat = regexp.MustCompile(`^\d{4}\d{2}\d{2}T\d{2}\d{2}$`) func Search(baseDirectory, searchTerm string, from time.Time, to time.Time) []LogbookEntry { var result = make([]LogbookEntry, 0) - err := filepath.Walk(baseDirectory, - func(path string, info os.FileInfo, err error) error { + + maxDepth := strings.Count(baseDirectory, string(os.PathSeparator)) + 4 + + err := filepath.WalkDir(baseDirectory, + func(path string, d fs.DirEntry, err error) error { if err != nil { return err } + if d.IsDir() && strings.Count(path, string(os.PathSeparator)) > maxDepth { + return fs.SkipDir + } if !isLogEntryFile(path) { return nil } @@ -69,11 +76,12 @@ func isInRequestedTimeRange(datetime string, from time.Time, to time.Time) bool } func isLogEntryFile(path string) bool { - pathParts := strings.Split(path, string(os.PathSeparator)) - lastPathPart := pathParts[len(pathParts)-1] - if !strings.HasSuffix(lastPathPart, ".md") { + if !strings.HasSuffix(path, ".md") { return false } + + pathParts := strings.Split(path, string(os.PathSeparator)) + lastPathPart := pathParts[len(pathParts)-1] parentDirectory := pathParts[len(pathParts)-2] if !(regexp.MustCompile(`^\d{2}\.\d{2}_.*`).MatchString(parentDirectory)) { return false