Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions core/management.go → core/archiving.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package core

import (
"errors"
"os"
"regexp"
"strings"

"github.com/experimental-software/logbook2/config"
Expand Down Expand Up @@ -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
}
56 changes: 0 additions & 56 deletions core/management_test.go → core/archiving_test.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions core/deleting.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 54 additions & 0 deletions core/deleting_test.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 0 additions & 7 deletions core/domain.go

This file was deleted.

25 changes: 25 additions & 0 deletions core/shared.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions core/shared_test.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 0 additions & 11 deletions core/utils_test.go

This file was deleted.