loam

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package loam is the Composition Root for the Loam application.

It connects the core business logic (Domain Layer) with the infrastructure adapters (Persistence Layer) using the Hexagonal Architecture pattern.

Philosophy:

Loam is a "Headless CMS" for toolmakers. It treats a collection of notes as a transactional database, abstracting the underlying storage mechanism. While the default implementation uses the File System and Git, Loam's core is agnostic, allowing for future adapters (e.g., S3, SQLite).

Features:

  • **Hexagonal Architecture**: Core domain is isolated from persistence details.
  • **Transactional Safe**: Atomic operations regardless of the underlying storage.
  • **Metadata First**: Native support for Frontmatter parsing and indexing.
  • **Default Adapter (FS + Git)**: Out-of-the-box support for local Markdown files with Git versioning.
  • **Extensible**: Designed to support other backends (SQL, S3, NoSQL) via `core.Repository`.

Usage:

// Initialize service with functional options
svc, err := loam.New("./vault",
	loam.WithAutoInit(true),
	loam.WithLogger(logger),
)

// Save a note
err := svc.SaveNote(ctx, "my-note", "content", nil)
Example (Advanced)

Example_advanced demonstrates how to use Loam in "Gitless" mode (no version history) and strict configuration.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/aretw0/loam/pkg/loam"
)

func main() {
	tmpDir, err := os.MkdirTemp("", "loam-gitless-*")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	// Initialize with explicit options:
	// - WithVersioning(false): Disables Git integration (behaves like a normal file store).
	// - WithAutoInit(true): Creates directories if they don't exist.
	vault, err := loam.New(tmpDir,
		loam.WithVersioning(false),
		loam.WithAutoInit(true),
	)
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.TODO()

	// Save a config file
	err = vault.SaveNote(ctx, "config", "debug_mode: true", nil)
	if err != nil {
		log.Fatal(err)
	}

	// List notes
	notes, err := vault.ListNotes(ctx)
	if err != nil {
		log.Fatal(err)
	}

	for _, n := range notes {
		fmt.Printf("Found note: %s\n", n.ID)
	}

}
Output:
Found note: config
Example (Basic)

Example_basic demonstrates how to initialize a Vault, save a note, and read it back.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/aretw0/loam/pkg/core"
	"github.com/aretw0/loam/pkg/loam"
)

func main() {
	// Create a temporary directory for the example
	tmpDir, err := os.MkdirTemp("", "loam-example-*")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	// Initialize the Loam service (Vault) targeting the temporary directory.
	// WithAutoInit(true) ensures the underlying storage (git repo) is initialized.
	vault, err := loam.New(tmpDir, loam.WithAutoInit(true))
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.TODO()

	// 1. Save a Note
	// ID: "hello-world"
	// Content: "This is my first note in Loam."
	// Metadata: Tags=["example"], Author="Gopher"
	err = vault.SaveNote(ctx, "hello-world", "This is my first note in Loam.", core.Metadata{
		"tags":   []string{"example"},
		"author": "Gopher",
	})
	if err != nil {
		log.Fatal(err)
	}

	// 2. Read the Note
	note, err := vault.GetNote(ctx, "hello-world")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("ID: %s\n", note.ID)
	fmt.Printf("Content: %s\n", note.Content)
	fmt.Printf("Author: %s\n", note.Metadata["author"])

}
Output:
ID: hello-world
Content: This is my first note in Loam.
Author: Gopher

Index

Examples

Constants

View Source
const (
	CommitTypeFeat     = "feat"
	CommitTypeFix      = "fix"
	CommitTypeDocs     = "docs"
	CommitTypeStyle    = "style"
	CommitTypeRefactor = "refactor"
	CommitTypePerf     = "perf"
	CommitTypeTest     = "test"
	CommitTypeChore    = "chore"
)

CommitType constants for semantic commits

Variables

This section is empty.

Functions

func AppendFooter added in v0.4.0

func AppendFooter(msg string) string

AppendFooter appends the Loam footer to an arbitrary message if not present. Used for free-form -m "msg" commits.

func FormatChangeReason added in v0.5.0

func FormatChangeReason(ctype, scope, subject, body string) string

FormatChangeReason builds a Conventional Commit message (used as Change Reason). logic:

<type>(<scope>): <subject>

<body>

Powered-by: Loam

func Init added in v0.5.0

func Init(uri string, opts ...Option) (core.Repository, error)

Init initializes a new Loam vault based on the provided configuration. The 'uri' argument is adapter-specific (e.g., file path for 'fs', connection string for others).

It returns the configured core.Repository.

func IsDevRun added in v0.3.0

func IsDevRun() bool

IsDevRun checks if the current process is running via `go run` or `go test`. It relies on the fact that these commands build binaries in temporary directories.

func New added in v0.5.0

func New(uri string, opts ...Option) (*core.Service, error)

svc, err := loam.New("./path/to/vault", loam.WithVersioning(false)) The URI argument is adapter-specific (e.g., file path for 'fs', connection string for others).

func ResolveVaultPath added in v0.3.0

func ResolveVaultPath(userPath string, forceTemp bool) string

ResolveVaultPath determines the actual path for the vault based on safety rules. If isDev is true (or forced), it re-roots the path into a temporary directory to avoid polluting the user's workspace/host repo.

func Sync added in v0.5.0

func Sync(uri string, opts ...Option) error

Sync synchronizes the vault at the given URI with its remote.

Types

type Option added in v0.3.0

type Option func(*options)

Option defines a functional option for configuring Loam.

func WithAdapter added in v0.5.0

func WithAdapter(name string) Option

WithAdapter allows specifying the storage adapter to use by name (e.g. "fs"). Defaults to "fs".

func WithAutoInit added in v0.3.0

func WithAutoInit(auto bool) Option

WithAutoInit enables automatic initialization of the vault (git init).

func WithForceTemp added in v0.5.0

func WithForceTemp(force bool) Option

WithForceTemp forces the use of a temporary directory (useful for testing).

func WithLogger added in v0.5.0

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the service.

func WithMustExist added in v0.3.0

func WithMustExist(must bool) Option

WithMustExist ensures the vault directory must already exist.

func WithRepository added in v0.5.0

func WithRepository(repo core.Repository) Option

WithRepository allows injecting a custom storage adapter (e.g. mock, s3). If provided, the default filesystem adapter will be skipped.

func WithVersioning added in v0.5.0

func WithVersioning(enabled bool) Option

WithVersioning enables or disables version control (e.g. Git). By default, versioning is enabled (gitless = false). Passing false will enable gitless mode (gitless = true).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL