fs

package
v0.9.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// TempFilePrefix is the prefix used for temporary atomic write files.
	TempFilePrefix = "loam-tmp-"
)

Variables

This section is empty.

Functions

func IsGitInstalled

func IsGitInstalled() bool

IsGitInstalled checks if git is available in the system path.

func ParseDocument added in v0.8.4

func ParseDocument(r io.Reader, ext, metadataKey string) (*core.Document, error)

ParseDocument parses raw content into a Core Document based on extension. Exposed for use by CLI "raw mode".

func SerializeDocument added in v0.8.4

func SerializeDocument(doc core.Document, ext, metadataKey string) ([]byte, error)

SerializeDocument converts a Document to bytes based on extension. Exposed for reuse if needed.

Types

type Config

type Config struct {
	Path        string
	AutoInit    bool
	Gitless     bool
	MustExist   bool
	Logger      *slog.Logger
	SystemDir   string            // e.g. ".loam"
	IDMap       map[string]string // Map filename -> ID column name (e.g. "users.csv": "email"). User must ensure uniqueness of values in this column.
	MetadataKey string            // If set, metadata will be nested under this key in JSON/YAML (e.g. "meta" or "frontmatter"). Contents will be in "content" (unless empty).
}

Config holds the configuration for the filesystem repository.

type Repository

type Repository struct {
	Path string
	// contains filtered or unexported fields
}

Repository implements core.Repository using the filesystem and Git.

func NewRepository

func NewRepository(config Config) *Repository

NewRepository creates a new filesystem-backed repository.

func (*Repository) Begin

func (r *Repository) Begin(ctx context.Context) (core.Transaction, error)

Begin starts a new transaction.

func (*Repository) Delete

func (r *Repository) Delete(ctx context.Context, id string) error

Delete removes a document.

func (*Repository) Get

func (r *Repository) Get(ctx context.Context, id string) (core.Document, error)

Get retrieves a document from the filesystem.

Workflow:

  1. Try to open the file directly (handling extension logic).
  2. If file not found, check if it's a sub-document inside a Collection (e.g. row in CSV).
  3. Parse content based on file extension.

func (*Repository) Initialize

func (r *Repository) Initialize(ctx context.Context) error

Initialize performs the necessary setup for the repository (mkdir, git init).

func (*Repository) List

func (r *Repository) List(ctx context.Context) ([]core.Document, error)

List scans the directory for all documents. It uses Reconcile to ensure the cache is up-to-date and then returns the cached state.

func (*Repository) Reconcile added in v0.9.0

func (r *Repository) Reconcile(ctx context.Context) ([]core.Event, error)

Reconcile implements core.Reconcilable. It detects changes made while the service was offline by comparing the current state with the persistent cache/index.

func (*Repository) Save

func (r *Repository) Save(ctx context.Context, doc core.Document) error

Save persists a document to the filesystem and commits it to Git. If the document belongs to a collection (e.g. CSV), it updates the specific row.

Workflow:

  1. Validate ID and determine extension strategy.
  2. Check if it's a "Collection Item" (e.g. inside a CSV) -> special handling.
  3. Create parent directories.
  4. Serialize content (Markdown/JSON/YAML) and write atomically to disk.
  5. (If Git enabled) 'git add' and 'git commit' with context metadata.

func (*Repository) Sync

func (r *Repository) Sync(ctx context.Context) error

Sync synchronizes the repository with its remote.

func (*Repository) Watch added in v0.9.0

func (r *Repository) Watch(ctx context.Context, pattern string) (<-chan core.Event, error)

Watch implements core.Watchable.

Caveats:

  1. Recursive Monitoring on Linux (inotify): New directories created AFTER the watch starts might NOT be monitored automatically depending on the fsnotify implementation and OS limits. Loam currently does not implement dynamic hierarchical watching for inotify.
  2. OS Limits: Large repositories may hit file descriptor limits (inotify limits).
  3. Debouncing: Events are debounced by 50ms. Rapid atomic writes (Create+Modify) are merged.

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction implements core.Transaction for the filesystem.

func NewTransaction

func NewTransaction(repo *Repository) *Transaction

NewTransaction creates a new transaction.

func (*Transaction) Commit

func (t *Transaction) Commit(ctx context.Context, changeReason string) error

Commit applies all staged changes.

func (*Transaction) Delete

func (t *Transaction) Delete(ctx context.Context, id string) error

Delete stages a document for deletion.

func (*Transaction) Get

func (t *Transaction) Get(ctx context.Context, id string) (core.Document, error)

Get retrieves a document, favoring staged changes.

func (*Transaction) Rollback

func (t *Transaction) Rollback(ctx context.Context) error

Rollback discards all staged changes.

func (*Transaction) Save

func (t *Transaction) Save(ctx context.Context, doc core.Document) error

Save stages a document for saving.

Jump to

Keyboard shortcuts

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