Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/memory/file"

Package file is a memory.Store that keeps notes as markdown files under a directory, one file per note, which a person can read and edit with any editor. An edit by hand is a new revision, so a session holding the old one gets a conflict instead of overwriting it.

Writes take an exclusive file lock per scope, so Stores in several processes over one directory serialize, and replace a note’s file by renaming a synced temporary file over it, so a crash leaves the old note or the new one.

See docs/design/durability.md for the Windows NTFS process-crash contract.

Example

dir, err := os.MkdirTemp("", "memory")
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = os.RemoveAll(dir) }()
s, err := file.New(dir)
if err != nil {
fmt.Println(err)
return
}
ctx := context.Background()
scope := memory.Project("/work/app")
n, err := s.Put(ctx, memory.Note{Scope: scope, Name: "table-tests", Description: "Write Go tests table-driven",
Type: memory.TypeFeedback, Author: memory.AuthorUser, Body: "Use tables of named cases."}, "")
if err != nil {
fmt.Println(err)
return
}
// Correcting needs the revision read; a stale one is a conflict.
_, err = s.Put(ctx, memory.Note{Scope: scope, Name: "table-tests", Description: "Write Go tests table-driven",
Type: memory.TypeFeedback, Author: memory.AuthorUser, Body: "Use tables of named cases run with t.Run."}, n.Revision)
fmt.Println(err)
if err != nil {
return
}
_, err = s.Put(ctx, n, n.Revision)
fmt.Println(err != nil)
notes, err := s.List(ctx, scope)
if err != nil {
fmt.Println(err)
return
}
for _, note := range notes {
fmt.Println(note.Body)
}
// Output:
// <nil>
// true
// Use tables of named cases run with t.Run.

Output:

<nil>
true
Use tables of named cases run with t.Run.
type Option func(*Store)

Option configures a Store.

func WithClock(now func() time.Time) Option

WithClock sets the clock that stamps notes; the default is time.Now.

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

Store keeps notes as markdown files under a directory: the user scope in user/, each project in projects/<digest of its ID>/, one <name>.md per note. A Store is safe for concurrent use, and Stores in several processes over one directory serialize their writes through a lock file per scope.

func New(dir string, opts ...Option) (*Store, error)

New returns a Store over dir, creating it if it doesn’t exist.

func (s *Store) Delete(ctx context.Context, scope memory.Scope, name string, expect memory.Revision) error

Delete removes scope’s note name, as memory.Store.Delete says.

func (s *Store) Dir(scope memory.Scope) string

Dir returns the directory scope’s notes are kept in, which may not exist yet. Hosts show it, so the user knows where their notes live.

func (s *Store) List(ctx context.Context, scope memory.Scope) ([]memory.Note, error)

List returns scope’s notes, ordered by name. Files whose names can’t name a note are not notes and are left out; a note’s file that is a link, or doesn’t parse, is listed with its Problem.

func (s *Store) Put(ctx context.Context, n memory.Note, expect memory.Revision) (memory.Note, error)

Put saves n, as memory.Store.Put says. It returns once the note’s file and its directory entry are on disk.