backup

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package backup exports the user's whole setup to one file and imports it back: their settings, their repositories, the prompt library, the launch defaults, and their own scripts, each a category the user ticks on export and again on import (see File for the format). It owns no table: each category is an owner's, which core adapts to a Part.

An import shows first what it would do (Preview), under either mode: Merge, which adds and changes but deletes nothing, and Replace, which makes each category exactly the file's. The import itself (Import) reads the file again and refuses one that changed since the preview, saves the setup as it stands into the app's own backups folder, and then applies every category in one transaction, so it is all or nothing.

A backup never holds the GitHub sign-in, which the app never stores, nor anything the app reads again or keeps on its own: caches, mirrors, history, per-item triage, the agent profiles found on disk, script trust (a file from elsewhere could pre-approve a repository's command), the window's memory, and the tools' detection.

Index

Constants

View Source
const (
	// Format names the backup file format, so a backup can be told from any other JSON
	// file.
	Format = brand.Slug + ".backup"

	// Version is the version of the format this build writes, and the newest it reads. A
	// change an older build would misread raises it; adding a section or a field does not.
	Version = 1

	// MaxBytes caps the size of a backup the app reads.
	MaxBytes = 16 << 20
)
View Source
const (
	// KeepBefore is how many of the copies an import saves first the app keeps, and how
	// many of those a reset saves first.
	KeepBefore = 5
)

Variables

View Source
var (
	// ErrNoPath is returned when the file to export to or import from is not an absolute
	// path.
	ErrNoPath = errors.New("choose a file")

	// ErrNoSections is returned for an export or an import that names no category.
	ErrNoSections = errors.New("choose what to include")

	// ErrUnknownSection is returned for a category this build doesn't know.
	ErrUnknownSection = errors.New("unknown category")

	// ErrNotInFile is returned for an import of a category the file doesn't hold.
	ErrNotInFile = errors.New("the backup doesn't hold that category")

	// ErrUnknownMode is returned for an import mode this build doesn't know.
	ErrUnknownMode = errors.New("unknown import mode")

	// ErrChanged is returned when the file changed between the preview and the import.
	ErrChanged = errors.New("the file changed since you opened it; open it again to see what it holds now")
)
View Source
var (
	// ErrNotBackup is returned for a file that is not a backup.
	ErrNotBackup = errors.New("the file isn't a " + brand.Name + " backup")

	// ErrNewerBackup is returned for a backup a newer build wrote in a format this build
	// cannot read.
	ErrNewerBackup = errors.New("the backup was made by a newer version of " + brand.Name +
		"; update " + brand.Name + " to import it")

	// ErrTooLarge is returned for a backup larger than MaxBytes.
	ErrTooLarge = fmt.Errorf("the file is too large: a backup is at most %d MB", MaxBytes>>20)
)

Functions

func Encode

func Encode(f File) ([]byte, error)

Encode writes f as indented JSON ending in a newline. Characters HTML treats specially stay as they are, so a prompt's text reads in the file as it was written.

func EncodeSection

func EncodeSection(v any) (json.RawMessage, error)

EncodeSection returns v as a section of a backup: JSON in which the characters HTML treats specially stay as they are.

func Hash

func Hash(data []byte) string

Hash returns the SHA-256 of data, in hex, which an import checks the file against.

Types

type Change

type Change string

Change says what importing one item does.

const (
	// ChangeAdd adds an item this Mac doesn't have.
	ChangeAdd Change = "add"

	// ChangeUpdate changes an item this Mac has.
	ChangeUpdate Change = "update"

	// ChangeSame leaves an item this Mac has as it is: the file's is the same.
	ChangeSame Change = "same"

	// ChangeRemove deletes an item this Mac has that the file doesn't, under ModeReplace.
	ChangeRemove Change = "remove"
)

The changes.

type Clearer

type Clearer interface {
	Clear(ctx context.Context, tx *sql.Tx) error
}

Clearer is a part whose rows Replace deletes before any part is applied, so a category applied before it is free of them: the launch defaults, which name prompts the prompts part may delete.

type Config

type Config struct {
	// Pool is the database every part's Apply runs in, in one transaction.
	Pool *sql.DB

	// Parts are the categories' owners. A category without one is never exported or
	// imported.
	Parts map[Section]Part

	// Dir is where an import saves the setup as it stands before it changes anything.
	Dir string

	// App is the build's version, which every backup records.
	App string

	// Home shortens the paths the window is shown with ~.
	Home string

	// Clock stamps the backups. Nil is the system clock.
	Clock clock.Clock

	// Logger hears what an import could not tidy. Nil discards.
	Logger *slog.Logger
}

Config holds a Service's dependencies. Pool and Parts are required.

type Count

type Count struct {
	Add    int `json:"add"`
	Update int `json:"update"`
	Same   int `json:"same"`
	Remove int `json:"remove"`
}

Count counts an import's items by what it does to them.

func (Count) Changes

func (c Count) Changes() bool

Changes reports whether the count changes anything.

func (Count) With

func (c Count) With(change Change) Count

With returns the count with one more item whose change is change.

type Exported

type Exported struct {
	// Path is the file.
	Path string `json:"path"`

	// Sections are the categories written, in order, with how many items each holds.
	Sections []SectionCount `json:"sections"`
}

Exported says what Export wrote.

type File

type File struct {
	// Format is Format.
	Format string `json:"format"`

	// Version is the format's version, Version when this build wrote it.
	Version int `json:"version"`

	// ExportedAt is when the file was written, as RFC 3339.
	ExportedAt string `json:"exportedAt"`

	// App is the version of the app that wrote the file.
	App string `json:"app"`

	Settings       json.RawMessage `json:"settings,omitempty"`
	Repositories   json.RawMessage `json:"repositories,omitempty"`
	Prompts        json.RawMessage `json:"prompts,omitempty"`
	LaunchDefaults json.RawMessage `json:"launchDefaults,omitempty"`
	Scripts        json.RawMessage `json:"scripts,omitempty"`
}

File is a backup: the user's setup in portable form, one section per category the user ticked, each absent when it wasn't. The prompts and the launch defaults sections are the app's own portable formats, each with its own format and version, exactly as the copies it keeps beside its database (prompts.json and launch-defaults.json); both name prompts by nickname. docs/backup-files.md specifies the format:

{
  "format": "<slug>.backup",
  "version": 1,
  "exportedAt": "2026-11-14T17:00:00Z",
  "app": "v0.4.0",
  "settings": {...},
  "repositories": {...},
  "prompts": {"format": "<slug>.prompts", ...},
  "launchDefaults": {"format": "<slug>.launch-defaults", ...},
  "scripts": {"scripts": [...]}
}

func Decode

func Decode(data []byte) (File, error)

Decode reads a backup. It refuses a file that is not one, one larger than MaxBytes, and one in a newer format than this build reads. Fields it does not know are ignored, so a file a newer build wrote in the same format still reads.

func (File) Section

func (f File) Section(s Section) json.RawMessage

Section returns the section s of the file, or nil when the file doesn't hold it.

type How

type How struct {
	// Mode is how the import treats what is here.
	Mode Mode

	// Sections are the categories the import takes.
	Sections []Section

	// Prompts names, by their nicknames in the file, the prompts the library will hold
	// for the file once its prompts section is applied, with what each is; nil when the
	// import doesn't take the prompts.
	Prompts map[string]PromptKind
}

How is what a part's Prepare needs to know of the import beside its own section.

func (How) Takes

func (h How) Takes(s Section) bool

Takes reports whether the import takes the category s.

type Imported

type Imported struct {
	// SavedTo is the copy of the setup as it stood before the import, with ~ for home.
	SavedTo string `json:"savedTo"`

	// Sections are the categories imported, in order.
	Sections []SectionResult `json:"sections"`
}

Imported says what Import did.

type Item

type Item struct {
	// Label names the item, such as a setting, a prompt's nickname, or a repository.
	Label string `json:"label"`

	// Change is what importing does to it.
	Change Change `json:"change"`

	// Detail says more, such as the value it has now; "" for nothing more.
	Detail string `json:"detail"`
}

Item is one thing an import adds, changes, keeps, or deletes, as the dialog lists it.

type Links map[string]int64

Links maps each prompt's nickname in the file to its ID here, once the prompts section is applied: the prompt added, saved over, or already holding it. It is empty when the import doesn't take the prompts.

type Mode

type Mode string

Mode is how an import treats what is here already.

const (
	// ModeMerge adds what the file holds and changes what it names, deleting nothing.
	ModeMerge Mode = "combine"

	// ModeReplace makes each category exactly the file's, deleting what the file doesn't
	// hold.
	ModeReplace Mode = "replace"
)

The modes.

func (Mode) Valid

func (m Mode) Valid() bool

Valid reports whether m is one of the modes.

type Part

type Part interface {
	// Export returns the category in portable form, and how many items it holds.
	Export(ctx context.Context) (json.RawMessage, int, error)

	// Prepare reads the category's section and works out what importing it does,
	// changing nothing: what it checks against the disk, the profiles, or the library is
	// checked here.
	Prepare(ctx context.Context, raw json.RawMessage, how How) (Prepared, error)

	// Apply makes the changes Prepare worked out inside tx, the import's transaction, and
	// returns what it did. The prompts part fills in links; the launch defaults read them.
	Apply(ctx context.Context, tx *sql.Tx, prepared Prepared, links Links) (Count, error)

	// Done runs once the transaction committed: the part's events, and the rewrite of the
	// copies the app keeps of it.
	Done(ctx context.Context, prepared Prepared)
}

Part is one category's owner, as backup reaches it. Every method but Apply runs outside the import's transaction, and Apply only inside it.

type Plan

type Plan struct {
	Count Count    `json:"count"`
	Items []Item   `json:"items"`
	Notes []string `json:"notes"`
}

Plan is what importing one category would do under one mode: its items, counted, and notes on what is left out or kept, and why.

func (*Plan) Add

func (p *Plan) Add(item Item)

Add records item, counting it.

func (*Plan) AddNote

func (p *Plan) AddNote(note string)

AddNote records a note on what is left out or kept, and why.

type Prepared

type Prepared struct {
	Plan

	// Prompts are, for the prompts part alone, the prompts the library will hold for the
	// file, as How.Prompts names them.
	Prompts map[string]PromptKind

	// State is the part's own, which its Apply reads back.
	State any
}

Prepared is what a part's Prepare worked out: its Plan, and what Apply needs.

type Preview

type Preview struct {
	// Path is the file, and Hash the SHA-256 of its bytes, which Import checks again.
	Path string `json:"path"`
	Hash string `json:"hash"`

	// ExportedAt is when the file was written, as RFC 3339, and App the version that wrote
	// it, each as the file says.
	ExportedAt string `json:"exportedAt"`
	App        string `json:"app"`

	// Sections are every category, in order, each present or not.
	Sections []SectionPreview `json:"sections"`
}

Preview is a backup as the import dialog shows it, before anything is changed.

type PromptKind

type PromptKind struct {
	Reply     bool
	AppliesTo string
}

PromptKind is what a prompt the import will bring is: an agent prompt or a review reply, and what kinds of item it applies to, as "pr", "issue", or "any".

type Request

type Request struct {
	// Path is the backup, and Hash the hash its preview gave.
	Path string `json:"path"`
	Hash string `json:"hash"`

	// Sections are the categories to import, each one the file holds.
	Sections []Section `json:"sections"`

	// Mode is how to treat what is here.
	Mode Mode `json:"mode"`
}

Request is an import as the window asks for it.

type Saver added in v0.3.0

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

Saver saves the user's whole setup before the app does something that loses it: a reset the user asked for. It is a type of its own, apart from the Service, so the window, which binds the Service, never reaches it.

func NewSaver added in v0.3.0

func NewSaver(s *Service) *Saver

NewSaver returns the Saver over s, which saves every category s has a part for.

func (*Saver) SaveBeforeReset added in v0.3.0

func (sv *Saver) SaveBeforeReset(ctx context.Context) (string, error)

SaveBeforeReset saves every category into the backups folder, as "<slug> before reset <stamp>.json", the same file a backup the user exports holds, and keeps the newest KeepBefore such copies. It returns the copy's path with ~ for the home directory, or "" when there is no folder to save it in.

type Section

type Section string

Section is one category of the user's setup a backup holds.

const (
	// SectionSettings is the user's settings, but for the repositories and the window's
	// memory.
	SectionSettings Section = "settings"

	// SectionRepositories is the clone folder, the other repository locations, and the
	// pinned and muted repositories and orgs.
	SectionRepositories Section = "repositories"

	// SectionPrompts is the prompt library: the prompts, the review replies, and their
	// defaults.
	SectionPrompts Section = "prompts"

	// SectionLaunchDefaults is each org's and repository's launch defaults.
	SectionLaunchDefaults Section = "launchDefaults"

	// SectionScripts is the user's own scripts, in their order.
	SectionScripts Section = "scripts"
)

The categories, in the order a backup lists and applies them.

func Sections

func Sections() []Section

Sections returns every category, in the order a backup lists and applies them.

func (Section) Valid

func (s Section) Valid() bool

Valid reports whether s is one of Sections.

type SectionCount

type SectionCount struct {
	Section Section `json:"section"`
	Items   int     `json:"items"`
}

SectionCount is how many items one category of a backup holds.

type SectionPreview

type SectionPreview struct {
	// Section is the category.
	Section Section `json:"section"`

	// Present reports whether the file holds it.
	Present bool `json:"present"`

	// Merge and Replace are what importing it would do under each mode. Remove counts
	// only under Replace.
	Merge   Plan `json:"merge"`
	Replace Plan `json:"replace"`

	// Error says why the section can't be imported, or is "" when it can.
	Error string `json:"error"`
}

SectionPreview is one category of a backup as the import dialog shows it.

type SectionResult

type SectionResult struct {
	Section Section  `json:"section"`
	Count   Count    `json:"count"`
	Notes   []string `json:"notes"`
}

SectionResult is what an import did to one category.

type Service

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

Service exports and imports the user's setup.

func NewService

func NewService(cfg Config) *Service

NewService returns a Service over cfg.

func (*Service) Export

func (s *Service) Export(ctx context.Context, path string, sections []Section) (Exported, error)

Export writes the categories in sections to a backup at path, readable only by the user, replacing any file there, and says how many items each holds.

func (*Service) Import

func (s *Service) Import(ctx context.Context, req Request) (Imported, error)

Import applies the categories req names from the backup at its path, all of them or, when anything fails, none. It refuses a file whose bytes changed since its preview (its hash differs), saves the whole setup as it stands into the backups folder first (keeping the newest KeepBefore such copies), works each category out again, then applies every one in one transaction, in Sections' order, the rows Replace deletes from a Clearer first. Once it commits, each part's Done tells the app and rewrites the copies it keeps.

func (*Service) Preview

func (s *Service) Preview(ctx context.Context, path string) (Preview, error)

Preview reads the backup at path and says, for each category it holds, what importing it would do under each mode, changing nothing. A section that can't be read is shown with why, and the others still are.

Jump to

Keyboard shortcuts

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