editor

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package editor provides safe, auditable file edits for user content.

The package offers two independent facilities:

  • SafeWrite performs an atomic write (temp-file + rename) and, if a file already exists at the target, creates a sibling ".bak" copy so an immediate-prior version is always recoverable even without revision history enabled.

  • CreateRevision / ListRevisions / RestoreRevision / PruneRevisions keep timestamped snapshots under a ".revisions/" directory alongside the edited file.

Both facilities are path-agnostic. Callers are expected to validate that paths sit inside the project content root before invoking these functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateRevision

func CreateRevision(filePath string) error

CreateRevision copies the current contents of filePath into a sibling ".revisions/" directory under a timestamped name. It is a no-op if the source file cannot be read.

func PruneRevisions

func PruneRevisions(filePath string, maxCount int) error

PruneRevisions deletes all but the newest maxCount revisions.

func RestoreRevision

func RestoreRevision(filePath, revisionPath string) error

RestoreRevision overwrites filePath with the contents of revisionPath. The current contents of filePath are snapshotted first so a restore is itself reversible.

func SafeWrite

func SafeWrite(path string, data []byte, opts SafeWriteOptions) error

SafeWrite writes data to path atomically. When opts.Backup is set and a file already exists at path, the prior contents are copied to "<path>.bak" before the new data is written. Parent directories are created as needed.

func ValidateWithinRoot

func ValidateWithinRoot(root, relPath string) error

ValidateWithinRoot reports an error if relPath would resolve outside root after cleaning and symlink-independent joining. It is intentionally lexical (no Stat) so callers can validate paths that do not yet exist.

Types

type RevisionInfo

type RevisionInfo struct {
	Path      string    // absolute path to the snapshot file
	Timestamp time.Time // moment the snapshot was taken
	Size      int64     // bytes
}

RevisionInfo describes one saved snapshot of a file.

func ListRevisions

func ListRevisions(filePath string) []RevisionInfo

ListRevisions returns all revisions of filePath ordered newest first. Returns an empty slice when no revisions exist.

type SafeWriteOptions

type SafeWriteOptions struct {
	// Backup, when true, copies the existing file to "<path>.bak" before
	// overwriting. Ignored when the target does not yet exist.
	Backup bool
	// Revision, when true, creates a timestamped snapshot in the sibling
	// ".revisions/" directory before overwriting. Ignored when the target
	// does not yet exist.
	Revision bool
	// Perm is the mode bits applied when creating a new file.
	Perm os.FileMode
}

SafeWriteOptions controls the behavior of SafeWrite.

Jump to

Keyboard shortcuts

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