snapshot

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package snapshot provides types and interfaces for workspace snapshots.

Index

Constants

View Source
const (
	BackendArchive = "archive"
	BackendAPFS    = "apfs"
)

Backend identifiers, used for EngineOptions.ForceBackend and Backend.Name.

Variables

View Source
var ErrAPFSNotAvailable = errors.New("APFS snapshots are only available on macOS")

ErrAPFSNotAvailable is returned when APFS operations are attempted on non-Darwin platforms.

Functions

func IsAPFS

func IsAPFS(path string) bool

IsAPFS returns false on non-darwin platforms.

func NewID

func NewID() string

NewID generates a new snapshot ID using id.Generate with the "snap" prefix, producing values like "snap_a1b2c3d4e5f6" (12-character hex suffix).

Types

type APFSBackend

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

APFSBackend is a stub for non-darwin platforms. On non-darwin systems, all methods return errors to indicate APFS is unavailable.

func NewAPFSBackend

func NewAPFSBackend(snapshotDir string) *APFSBackend

NewAPFSBackend returns a stub APFSBackend on non-darwin platforms. The snapshotDir parameter is accepted for API compatibility but not used.

func (*APFSBackend) Create

func (b *APFSBackend) Create(workspacePath, id string) (string, error)

Create returns an error on non-darwin platforms.

func (*APFSBackend) Delete

func (b *APFSBackend) Delete(nativeRef string) error

Delete returns an error on non-darwin platforms.

func (*APFSBackend) List

func (b *APFSBackend) List(workspacePath string) ([]string, error)

List returns an error on non-darwin platforms.

func (*APFSBackend) Name

func (b *APFSBackend) Name() string

Name returns the backend identifier.

func (*APFSBackend) Restore

func (b *APFSBackend) Restore(workspacePath, nativeRef string) error

Restore returns an error on non-darwin platforms.

func (*APFSBackend) RestoreTo

func (b *APFSBackend) RestoreTo(nativeRef, destPath string) error

RestoreTo returns an error on non-darwin platforms.

type ArchiveBackend

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

ArchiveBackend implements the Backend interface using tar.gz archives.

func NewArchiveBackend

func NewArchiveBackend(snapshotDir string, opts ArchiveOptions) *ArchiveBackend

NewArchiveBackend creates a new archive-based snapshot backend.

func (*ArchiveBackend) Create

func (b *ArchiveBackend) Create(workspacePath, id string) (string, error)

Create creates a tar.gz archive of the workspace.

func (*ArchiveBackend) Delete

func (b *ArchiveBackend) Delete(nativeRef string) error

Delete removes the archive file.

func (*ArchiveBackend) List

func (b *ArchiveBackend) List(_ string) ([]string, error)

List returns all archive files in the snapshot directory. The workspacePath parameter is unused but required by the Backend interface.

func (*ArchiveBackend) Name

func (b *ArchiveBackend) Name() string

Name returns the backend identifier.

func (*ArchiveBackend) Restore

func (b *ArchiveBackend) Restore(workspacePath, nativeRef string) error

Restore extracts the archive to the workspace, preserving .git directory.

func (*ArchiveBackend) RestoreTo

func (b *ArchiveBackend) RestoreTo(nativeRef, destPath string) error

RestoreTo extracts the archive to an arbitrary destination path.

type ArchiveOptions

type ArchiveOptions struct {
	// UseGitignore enables parsing .gitignore files to exclude matching paths.
	UseGitignore bool
	// Additional specifies extra patterns to exclude (gitignore syntax).
	Additional []string
	// IncludeGit keeps the .git directory in the archive instead of skipping
	// it. Volume mode sets this so agent commits made inside the container
	// survive snapshot extraction. Defaults to false (bind-mode behavior),
	// which always excludes .git.
	IncludeGit bool
}

ArchiveOptions configures the archive backend behavior.

type Backend

type Backend interface {
	// Name returns the backend identifier (e.g., "apfs", "zfs", "archive").
	Name() string

	// Create creates a snapshot of the workspace and returns its native reference.
	Create(workspacePath string, id string) (nativeRef string, err error)

	// Restore restores a snapshot to the workspace (in-place).
	Restore(workspacePath string, nativeRef string) error

	// RestoreTo restores a snapshot to a different directory.
	RestoreTo(nativeRef string, destPath string) error

	// Delete removes a snapshot.
	Delete(nativeRef string) error

	// List returns all snapshots for a workspace.
	List(workspacePath string) ([]string, error)
}

Backend defines the interface for snapshot storage backends.

type Engine

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

Engine manages snapshot operations with automatic backend detection.

func NewEngine

func NewEngine(workspace, snapshotDir string, opts EngineOptions) (*Engine, error)

NewEngine creates a new snapshot engine with automatic backend detection. It loads any existing snapshot metadata from the snapshot directory.

func (*Engine) Backend

func (e *Engine) Backend() Backend

Backend returns the detected backend.

func (*Engine) Create

func (e *Engine) Create(typ Type, label string) (Metadata, error)

Create creates a new snapshot with the given type and label.

func (*Engine) Delete

func (e *Engine) Delete(id string) error

Delete removes a snapshot and its metadata.

func (*Engine) Get

func (e *Engine) Get(id string) (Metadata, bool)

Get returns a snapshot by ID.

func (*Engine) List

func (e *Engine) List() ([]Metadata, error)

List returns all snapshots sorted by creation time (newest first).

func (*Engine) Restore

func (e *Engine) Restore(id string) error

Restore restores a snapshot in-place to the workspace.

func (*Engine) RestoreTo

func (e *Engine) RestoreTo(id, destPath string) error

RestoreTo restores a snapshot to a different directory.

type EngineOptions

type EngineOptions struct {
	// UseGitignore enables parsing .gitignore files to exclude matching paths.
	UseGitignore bool
	// Additional specifies extra patterns to exclude (gitignore syntax).
	Additional []string
	// ForceBackend forces a specific backend: BackendArchive or BackendAPFS.
	ForceBackend string
	// IncludeGit keeps the .git directory in archive snapshots. Volume mode
	// sets this so agent commits survive extraction; default false preserves
	// bind-mode behavior (always exclude .git).
	IncludeGit bool
}

EngineOptions configures the snapshot engine behavior.

type Metadata

type Metadata struct {
	ID        string    `json:"id"`
	Type      Type      `json:"type"`
	Label     string    `json:"label,omitempty"`
	Backend   string    `json:"backend"`
	CreatedAt time.Time `json:"created_at"`
	SizeDelta *int64    `json:"size_delta,omitempty"`
	NativeRef string    `json:"native_ref,omitempty"`
}

Metadata describes a snapshot.

func ListSnapshots added in v0.7.0

func ListSnapshots(snapshotDir string) ([]Metadata, error)

ListSnapshots reads snapshot metadata directly from a snapshot directory, without constructing an Engine. Unlike NewEngine(...).List(), it does NOT require the run's workspace to still exist on disk, so guards that run after the workspace is gone (e.g. the destroy/clean extraction-snapshot check) get a correct answer instead of failing closed. A missing metadata file is not an error — it yields an empty list (no snapshots taken yet).

type Type

type Type string

Type represents the trigger type for a snapshot.

const (
	TypePreRun Type = "pre-run"
	TypeGit    Type = "git"
	TypeBuild  Type = "build"
	TypeIdle   Type = "idle"
	TypeManual Type = "manual"
	TypeSafety Type = "safety"
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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