diff --git a/core/management.go b/core/archiving.go similarity index 55% rename from core/management.go rename to core/archiving.go index a96be1a..df786d4 100644 --- a/core/management.go +++ b/core/archiving.go @@ -1,9 +1,7 @@ package core import ( - "errors" "os" - "regexp" "strings" "github.com/experimental-software/logbook2/config" @@ -31,24 +29,3 @@ func Archive(configuration config.Configuration, sourcePath string) error { err = os.RemoveAll(sourceDirectoryPath) return err } - -func Remove(sourcePath string) error { - sourceDirectoryPath, err := logbookEntryRootPath(sourcePath) - if err != nil { - return err - } - err = os.RemoveAll(sourceDirectoryPath) - return err -} - -func logbookEntryRootPath(path string) (string, error) { - if !strings.HasSuffix(path, "/") { - path += "/" - } - re := regexp.MustCompile(`(.*[/\\]\d{4}[/\\]\d{2}[/\\]\d{2}[/\\]\d{2}\.\d{2}_.*?[/\\]).*`) - m := re.FindStringSubmatch(path) - if len(m) != 2 { - return "", errors.New("invalid logbook entry path: " + path) - } - return m[1], nil -} diff --git a/core/management_test.go b/core/archiving_test.go similarity index 58% rename from core/management_test.go rename to core/archiving_test.go index 32d762f..8a6f574 100644 --- a/core/management_test.go +++ b/core/archiving_test.go @@ -1,46 +1,13 @@ package core import ( - "fmt" "os" - "path/filepath" "testing" "time" "github.com/experimental-software/logbook2/config" ) -func Test_Delete_happy_path(t *testing.T) { - // Arrange - logBaseDir := createTempDir() - archiveBaseDir := createTempDir() - defer func(path string) { - _ = os.RemoveAll(logBaseDir) - _ = os.RemoveAll(archiveBaseDir) - }(logBaseDir) - - logEntry, err := AddLogEntry(logBaseDir, "Log entry for archive test", time.Now()) - if err != nil { - t.Fatal(err) - } - searchResultForArchiveBaseDir := Search(logBaseDir, "", epoc, nextCentury) - if len(searchResultForArchiveBaseDir) != 1 { - t.Fatal("Expected 1 search result") - } - - // Act - err = Remove(logEntry.Directory) - if err != nil { - t.Fatal(err) - } - - // Assert - searchResultForLogBaseDir := Search(logBaseDir, "", epoc, nextCentury) - if len(searchResultForLogBaseDir) != 0 { - t.Fatal("Expected empty search result") - } -} - func Test_Archive_happy_path(t *testing.T) { // Arrange logBaseDir := createTempDir() @@ -113,26 +80,3 @@ func Test_Archive_path_in_subdirectory(t *testing.T) { t.Fatal("Expected 1 search result") } } - -func Test_logbookEntryRootPath_invalid_path(t *testing.T) { - _, err := logbookEntryRootPath("/Users/jdoe/Notes/2025/12/21/just-a-test") - if err == nil { - t.Fatal("Expected error") - } - fmt.Println(err.Error()) -} - -func createFileInSubdirectory(entry LogbookEntry) string { - subdirectory := filepath.Join(entry.Directory, "foo") - err := os.MkdirAll(subdirectory, 0755) - if err != nil { - panic(err) - } - filePath := filepath.Join(subdirectory, "bar.txt") - data := []byte("Hello, World!") - err = os.WriteFile(filePath, data, 0644) - if err != nil { - panic(err) - } - return filePath -} diff --git a/core/deleting.go b/core/deleting.go new file mode 100644 index 0000000..d365aa5 --- /dev/null +++ b/core/deleting.go @@ -0,0 +1,14 @@ +package core + +import ( + "os" +) + +func Remove(sourcePath string) error { + sourceDirectoryPath, err := logbookEntryRootPath(sourcePath) + if err != nil { + return err + } + err = os.RemoveAll(sourceDirectoryPath) + return err +} diff --git a/core/deleting_test.go b/core/deleting_test.go new file mode 100644 index 0000000..bf36362 --- /dev/null +++ b/core/deleting_test.go @@ -0,0 +1,54 @@ +package core + +import ( + "os" + "path/filepath" + "testing" + "time" +) + +func Test_Delete_happy_path(t *testing.T) { + // Arrange + logBaseDir := createTempDir() + archiveBaseDir := createTempDir() + defer func(path string) { + _ = os.RemoveAll(logBaseDir) + _ = os.RemoveAll(archiveBaseDir) + }(logBaseDir) + + logEntry, err := AddLogEntry(logBaseDir, "Log entry for archive test", time.Now()) + if err != nil { + t.Fatal(err) + } + searchResultForArchiveBaseDir := Search(logBaseDir, "", epoc, nextCentury) + if len(searchResultForArchiveBaseDir) != 1 { + t.Fatal("Expected 1 search result") + } + + // Act + err = Remove(logEntry.Directory) + if err != nil { + t.Fatal(err) + } + + // Assert + searchResultForLogBaseDir := Search(logBaseDir, "", epoc, nextCentury) + if len(searchResultForLogBaseDir) != 0 { + t.Fatal("Expected empty search result") + } +} + +func createFileInSubdirectory(entry LogbookEntry) string { + subdirectory := filepath.Join(entry.Directory, "foo") + err := os.MkdirAll(subdirectory, 0755) + if err != nil { + panic(err) + } + filePath := filepath.Join(subdirectory, "bar.txt") + data := []byte("Hello, World!") + err = os.WriteFile(filePath, data, 0644) + if err != nil { + panic(err) + } + return filePath +} diff --git a/core/domain.go b/core/domain.go deleted file mode 100644 index 22a8482..0000000 --- a/core/domain.go +++ /dev/null @@ -1,7 +0,0 @@ -package core - -type LogbookEntry struct { - DateTime string `json:"dateTime"` - Title string `json:"title"` - Directory string `json:"directory"` -} diff --git a/core/shared.go b/core/shared.go new file mode 100644 index 0000000..f2aabfa --- /dev/null +++ b/core/shared.go @@ -0,0 +1,25 @@ +package core + +import ( + "errors" + "regexp" + "strings" +) + +type LogbookEntry struct { + DateTime string `json:"dateTime"` + Title string `json:"title"` + Directory string `json:"directory"` +} + +func logbookEntryRootPath(path string) (string, error) { + if !strings.HasSuffix(path, "/") { + path += "/" + } + re := regexp.MustCompile(`(.*[/\\]\d{4}[/\\]\d{2}[/\\]\d{2}[/\\]\d{2}\.\d{2}_.*?[/\\]).*`) + m := re.FindStringSubmatch(path) + if len(m) != 2 { + return "", errors.New("invalid logbook entry path: " + path) + } + return m[1], nil +} diff --git a/core/shared_test.go b/core/shared_test.go new file mode 100644 index 0000000..db98a7c --- /dev/null +++ b/core/shared_test.go @@ -0,0 +1,23 @@ +package core + +import ( + "fmt" + "os" + "testing" +) + +func Test_logbookEntryRootPath_invalid_path(t *testing.T) { + _, err := logbookEntryRootPath("/Users/jdoe/Notes/2025/12/21/just-a-test") + if err == nil { + t.Fatal("Expected error") + } + fmt.Println(err.Error()) +} + +func createTempDir() string { + tempDir, err := os.MkdirTemp("", "test") + if err != nil { + panic(err) + } + return tempDir +} diff --git a/core/utils_test.go b/core/utils_test.go deleted file mode 100644 index a1e0080..0000000 --- a/core/utils_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package core - -import "os" - -func createTempDir() string { - tempDir, err := os.MkdirTemp("", "test") - if err != nil { - panic(err) - } - return tempDir -}