store

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package store handles snippet persistence as a single JSON file.

The on-disk format is documented in docs/format.md. Keep that doc and this package in sync — external compatibility lives there.

Index

Constants

View Source
const FormatVersion = 1

Variables

View Source
var (
	ErrNotFound = errors.New("snippet not found")
	ErrExists   = errors.New("snippet already exists")
	ErrEmptyKey = errors.New("key must not be empty")
)

Common errors.

Functions

func DefaultPath

func DefaultPath() (string, error)

DefaultPath returns the default snippet file location.

PE_DIR overrides everything. Otherwise falls back to ~/.pe/pe.json (per the project spec). XDG paths are intentionally NOT consulted — keeping a single, stable, easy-to-remember location is a feature.

func Save

func Save(path string, f *File) error

Save atomically writes the file (write to .tmp + rename).

Types

type File

type File struct {
	Version  int       `json:"version"`
	Snippets []Snippet `json:"snippets"`
}

File is the root JSON document on disk.

func Load

func Load(path string) (*File, error)

Load reads and parses the snippet file. Returns an empty File if missing.

func (*File) Add

func (f *File) Add(s Snippet) error

Add inserts a new snippet. Returns ErrExists if the key is taken. CreatedAt / UpdatedAt are stamped if zero.

func (*File) Delete

func (f *File) Delete(key string) error

Delete removes a snippet by key.

func (*File) Get

func (f *File) Get(key string) (*Snippet, error)

Get returns the snippet with the given key, or ErrNotFound.

func (*File) Has

func (f *File) Has(key string) bool

Has reports whether a snippet with the given key exists.

func (*File) Search

func (f *File) Search(query string) []Snippet

Search returns all snippets whose key, description, value, or tags contain the query (case-insensitive). Results are sorted by key.

func (*File) SortedKeys

func (f *File) SortedKeys() []string

SortedKeys returns all keys in sorted order.

func (*File) Touch

func (f *File) Touch(key string, at time.Time) error

Touch records that the snippet was used (copied) at the given time. Increments UseCount. No-op (with ErrNotFound) if key is missing.

func (*File) Update

func (f *File) Update(oldKey string, newSnippet Snippet) error

Update replaces the snippet with the given oldKey. If newKey != oldKey, it is checked for collisions. UpdatedAt is bumped to now.

type Snippet

type Snippet struct {
	Key         string     `json:"key"`
	Value       string     `json:"value"`
	Description string     `json:"description,omitempty"`
	Tags        []string   `json:"tags,omitempty"`
	CreatedAt   time.Time  `json:"createdAt"`
	UpdatedAt   time.Time  `json:"updatedAt"`
	LastUsedAt  *time.Time `json:"lastUsedAt,omitempty"`
	UseCount    int        `json:"useCount"`
}

Snippet is a single registered phrase / command / prompt fragment.

Jump to

Keyboard shortcuts

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