storage

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWrite

func AtomicWrite(path string, data []byte) error

AtomicWrite writes data to path via a temp file + rename for crash safety. Sets explicit 0644 permissions on the resulting file. Overwrites if exists.

func AtomicWriteExclusive

func AtomicWriteExclusive(path string, data []byte) error

AtomicWriteExclusive writes data to path via a temp file + hard link. Unlike AtomicWrite, it fails if path already exists (returns an error where os.IsExist reports true). This avoids the TOCTOU race inherent in Stat-then-Rename patterns.

func DeleteDoc

func DeleteDoc(docsDir, filename string) error

DeleteDoc removes a document from docsDir and regenerates the README index. Returns an error if the file does not exist or cannot be removed. Index regeneration errors are returned separately (wrapped with context).

func ExtractSlug

func ExtractSlug(filename string) string

ExtractSlug extracts the slug from a filename with pattern {type}-{slug}-{date}.md. Example: "decision-auth-strategy-2026-03-07.md" → "auth-strategy"

func FindReferencingDocs

func FindReferencingDocs(docsDir, filename string) ([]string, error)

FindReferencingDocs returns filenames of documents whose `related:` field references the given filename (compared without .md extension).

func Marshal

func Marshal(meta domain.DocMeta, body string) ([]byte, error)

Marshal produces "---\n{yaml}\n---\n{body}" from DocMeta and body.

func NormalizeAfter

func NormalizeAfter(after string) string

NormalizeAfter converts a YYYY-MM date to YYYY-MM-01 for lexicographic comparison. YYYY-MM-DD values pass through unchanged.

func QuickHealthCheck

func QuickHealthCheck(docsDir string) (int, error)

QuickHealthCheck performs a fast health check on the docs directory. Returns the number of issues found:

  • orphan .tmp files (interrupted writes)
  • missing or empty README.md index

This is NOT the full diagnostic — see lore doctor (Story 5.1).

func ReadDocContent

func ReadDocContent(path string) (string, error)

ReadDocContent returns the full file content (front matter + body) for display. The path argument is expected to come from SearchResult.Path (produced by SearchDocs), which constructs paths via filepath.Join(dir, entry.Name()) — never from user input.

func RegenerateIndex

func RegenerateIndex(docsDir string) error

RegenerateIndex creates or updates the README.md index in docsDir. It scans all .md files (except README.md), parses their front matter, and generates a sorted table. The result is written atomically.

func Unmarshal

func Unmarshal(data []byte) (domain.DocMeta, string, error)

Unmarshal parses front matter + body from a document.

func ValidateFilename

func ValidateFilename(filename string) error

ValidateFilename rejects path traversal attempts in user-supplied filenames.

func ValidateMeta

func ValidateMeta(meta domain.DocMeta) error

ValidateMeta checks that required fields (type, date, status) are present, that type is a recognized DocType constant, and that date is in YYYY-MM-DD format.

Types

type CorpusStore

type CorpusStore struct {
	Dir string // path to .lore/docs/
}

CorpusStore implements domain.CorpusReader for the local filesystem.

func (*CorpusStore) ListDocs

func (s *CorpusStore) ListDocs(filter domain.DocFilter) ([]domain.DocMeta, error)

ListDocs scans the directory for .md files and returns their metadata. Skips README.md (auto-generated index). Returns partial results alongside a combined error if any files could not be read or parsed.

func (*CorpusStore) ReadDoc

func (s *CorpusStore) ReadDoc(id string) (string, error)

ReadDoc reads a single document by ID (filename without extension).

type DiagnosticReport

type DiagnosticReport struct {
	Issues   []Issue
	DocCount int // number of valid .md documents scanned
	Checked  int // total number of checks performed
}

DiagnosticReport holds the results of a full corpus health check.

func Diagnose

func Diagnose(docsDir string) (*DiagnosticReport, error)

Diagnose performs a comprehensive health check on the docs directory. It checks for orphan .tmp files, broken references, stale index, stale cache (metadata.json), and invalid front matter.

type FixReport

type FixReport struct {
	Fixed     int      // number of issues successfully fixed
	Remaining int      // number of issues that still need manual attention
	Errors    int      // number of errors during fix (permission, etc.)
	Details   []string // descriptions of actions taken
}

FixReport holds the results of automatic repairs.

func Fix

func Fix(docsDir string, report *DiagnosticReport) (*FixReport, error)

Fix applies automatic repairs based on the diagnostic report. Only issues with AutoFix=true are attempted. Individual fix failures are logged but do not stop other repairs.

type Issue

type Issue struct {
	Category string // "orphan-tmp", "broken-ref", "stale-index", "stale-cache", "invalid-frontmatter"
	File     string // file concerned
	Detail   string // human-readable description
	AutoFix  bool   // repairable automatically?
}

Issue represents a single diagnostic finding.

type SearchResult

type SearchResult struct {
	Filename string
	Path     string
	Meta     domain.DocMeta
	Title    string // first # heading from body, fallback: slug from filename
}

SearchResult represents a single search hit from SearchDocs.

func FindDocByCommit

func FindDocByCommit(dir string, commitHash string) (*SearchResult, error)

FindDocByCommit searches for a document whose front matter commit field matches commitHash (strict equality — caller resolves short hashes before calling). Returns nil if no match is found.

func SearchDocs

func SearchDocs(dir string, keyword string, filter domain.DocFilter) ([]SearchResult, error)

SearchDocs searches documents matching keyword (case-insensitive) in filename, tags, and body, then applies type and date filters from DocFilter. Returns results sorted by date descending.

type WriteResult

type WriteResult struct {
	Filename string // e.g. "decision-auth-strategy-2026-03-07.md"
	Path     string // e.g. "/path/to/.lore/docs/decision-auth-strategy-2026-03-07.md"
	IndexErr error  // non-nil if index regeneration failed (non-fatal)
}

WriteResult contains the outcome of a WriteDoc operation.

func WriteDoc

func WriteDoc(dir string, meta domain.DocMeta, subject string, body string) (WriteResult, error)

WriteDoc creates a document in the given directory via AtomicWrite. subject is used for the filename slug (e.g., "add JWT middleware" → "add-jwt-middleware"). After writing, it regenerates the README index. Index errors are surfaced in WriteResult.IndexErr but do not cause WriteDoc itself to fail.

Jump to

Keyboard shortcuts

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