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
3 changes: 3 additions & 0 deletions .gremlins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
unleash:
output-statuses: "lc"
exclude-files: ["cmd/*"]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ The following snippet shows the configuration options with their default values:
```yaml
# The directory where new log entries are added.
logDirectory: ~/Logs

# The directory where log entries are moved when they are archived.
archiveDirectory: ~/Archive
```

## Usage
Expand All @@ -43,6 +46,12 @@ logbook2 search $SEARCH_TERM
go test ./... -coverprofile=./cov.out
```

With the help of the [gremlins](https://gremlins.dev/) program, the tests can be executed with mutations:

```sh
gremlins unleash
```

### Component integration test

```sh
Expand Down
5 changes: 3 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

var addCmd = &cobra.Command{
Use: "add [flags] title",
Args: cobra.ExactArgs(1),
Use: "add [flags] title",
Args: cobra.ExactArgs(1),
Short: "Add new logbook entries",

Run: func(cmd *cobra.Command, args []string) {
title := args[0]
Expand Down
28 changes: 28 additions & 0 deletions cmd/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"os"

"github.com/experimental-software/logbook2/core"
"github.com/experimental-software/logbook2/logging"
"github.com/spf13/cobra"
)

var removeCmd = &cobra.Command{
Use: "remove [flags] path [path...]",
Short: "Deletes log entries",
Args: cobra.MinimumNArgs(1),
Aliases: []string{"rm"},

Run: func(cmd *cobra.Command, args []string) {
for _, path := range args {
err := core.Remove(path)
if err != nil {
logging.Error("Removing logbook entry failed for path: "+path, err)
os.Exit(1)
}
fmt.Println(path)
}
},
}
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package cmd

import (
"fmt"
"os"

"github.com/experimental-software/logbook2/config"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "logbook2",
Use: "logbook2",
Short: "A markdown-based engineering logbook",

Run: func(cmd *cobra.Command, args []string) {
fmt.Println("A markdown-based engineering logbook")
// noop
},
}

func Execute() {
rootCmd.AddCommand(searchCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(archiveCmd)
rootCmd.AddCommand(removeCmd)

rootCmd.CompletionOptions.DisableDefaultCmd = true

Expand Down
5 changes: 3 additions & 2 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var searchCmd = &cobra.Command{
Use: "search [flags] search_term",
Aliases: []string{"s"},
Short: "Search for logbook entries",

Run: func(cmd *cobra.Command, args []string) {
searchTerm := ""
Expand All @@ -24,8 +25,8 @@ var searchCmd = &cobra.Command{
t.SetHeaders("Date / Time", "Title", "Path")
for _, entry := range logEntries {
title := entry.Title
if len(title) > 45 {
title = title[:45]
if len(title) > 50 {
title = title[:50]
title += " (...)"
}
t.AddRow(strings.Replace(entry.DateTime, "T", " ", 1), title, entry.Directory)
Expand Down
16 changes: 7 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@ type Configuration struct {
}

var defaultConfiguration = Configuration{
LogDirectory: filepath.Join(userHomeDir(), "Logs"),
LogDirectory: filepath.Join(userHomeDir(), "Logs"),
ArchiveDirectory: filepath.Join(userHomeDir(), "Archive"),
}

func LoadConfiguration(configurationFilePath string) Configuration {
var result Configuration
result := defaultConfiguration

configurationBytes, err := os.ReadFile(configurationFilePath)
if err != nil {
logging.Warn("Failed to read Configuration file: " + configurationFilePath)
result = defaultConfiguration
return result
}

err = yaml.Unmarshal(configurationBytes, &result)
if err != nil {
logging.Warn("Failed to unmarshal Configuration file: " + configurationFilePath)
result = defaultConfiguration
return result
}

result.LogDirectory = strings.Replace(result.LogDirectory, "~", userHomeDir(), -1)
result.LogDirectory = strings.TrimSpace(result.LogDirectory)

result.ArchiveDirectory = strings.Replace(result.ArchiveDirectory, "~", userHomeDir(), -1)
result.ArchiveDirectory = strings.TrimSpace(result.ArchiveDirectory)
result.LogDirectory = strings.ReplaceAll(result.LogDirectory, "~", userHomeDir())
result.ArchiveDirectory = strings.ReplaceAll(result.ArchiveDirectory, "~", userHomeDir())

return result
}
Expand Down
23 changes: 21 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import "testing"
import (
"strings"
"testing"
)

func TestLoadConfiguration(t *testing.T) {
func Test_LoadConfiguration_happy_path(t *testing.T) {
result := LoadConfiguration("./t/config.yaml")

if result.LogDirectory != "/path/for/tests" {
Expand All @@ -12,3 +15,19 @@ func TestLoadConfiguration(t *testing.T) {
t.Errorf("Unexpected archive directory: got %s, expected /archive/path/for/tests", result.ArchiveDirectory)
}
}

func Test_LoadConfiguration_config_file_not_existent(t *testing.T) {
result := LoadConfiguration("./t/67b2070e-8b21-44d1-8a02-21909d8037f9.yaml")

if !strings.HasSuffix(result.LogDirectory, "Logs") {
t.Errorf("Unexpected log directory: got '%s', expected default value", result.LogDirectory)
}
}

func Test_LoadConfiguration_config_file_invalid(t *testing.T) {
result := LoadConfiguration("./t/invalid_syntax_config.yaml")

if !strings.HasSuffix(result.LogDirectory, "Logs") {
t.Errorf("Unexpected log directory: got '%s', expected default value", result.LogDirectory)
}
}
2 changes: 2 additions & 0 deletions config/t/invalid_syntax_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logDirectory:::::::: - ~, 2938873294 /path/for/tests

8 changes: 4 additions & 4 deletions core/adding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"
)

func AddLogEntry(baseDirectory, title string) (LogEntry, error) {
func AddLogEntry(baseDirectory, title string) (LogbookEntry, error) {
currentTime := time.Now()
slug := slugify(title)
dateTime := fmt.Sprintf("%d-0%d-0%dT0%d:0%d",
Expand All @@ -28,16 +28,16 @@ func AddLogEntry(baseDirectory, title string) (LogEntry, error) {
)
err := os.MkdirAll(logDirectoryPath, 0777)
if err != nil {
return LogEntry{}, err
return LogbookEntry{}, err
}

logFilePath := filepath.Join(logDirectoryPath, slug+".md")
err = os.WriteFile(logFilePath, []byte(fmt.Sprintf("# %s\n\n", title)), 0777)
if err != nil {
return LogEntry{}, err
return LogbookEntry{}, err
}

return LogEntry{DateTime: dateTime, Title: title, Directory: logDirectoryPath}, nil
return LogbookEntry{DateTime: dateTime, Title: title, Directory: logDirectoryPath}, nil
}

func slugify(s string) string {
Expand Down
37 changes: 0 additions & 37 deletions core/archiving.go

This file was deleted.

61 changes: 0 additions & 61 deletions core/archiving_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/domain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core

type LogEntry struct {
type LogbookEntry struct {
DateTime string
Title string
Directory string
Expand Down
54 changes: 54 additions & 0 deletions core/management.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package core

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

"github.com/experimental-software/logbook2/config"
"github.com/plus3it/gorecurcopy"
)

func Archive(configuration config.Configuration, sourcePath string) error {
sourceDirectoryPath, err := logbookEntryRootPath(sourcePath)
if err != nil {
return err
}
targetDirectoryPath := strings.Replace(
sourceDirectoryPath, configuration.LogDirectory, configuration.ArchiveDirectory, 1,
)

err = os.MkdirAll(targetDirectoryPath, 0777)
if err != nil {
return err
}

err = gorecurcopy.CopyDirectory(sourceDirectoryPath, targetDirectoryPath)
if err != nil {
return err
}
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
}
Loading