From 5288918b6b95fee56e00d320b1da508ee189a539 Mon Sep 17 00:00:00 2001 From: Amolith Date: Wed, 25 Mar 2026 20:49:58 -0600 Subject: [PATCH] Add snapshot JSON parsing and formatting --- internal/restic/snapshots.go | 63 ++++++++ internal/restic/snapshots_test.go | 260 ++++++++++++++++++++++++++++++ 2 files changed, 323 insertions(+) create mode 100644 internal/restic/snapshots.go create mode 100644 internal/restic/snapshots_test.go diff --git a/internal/restic/snapshots.go b/internal/restic/snapshots.go new file mode 100644 index 0000000000000000000000000000000000000000..926846623eb43301bcf3d9003542b98091fbc2d6 --- /dev/null +++ b/internal/restic/snapshots.go @@ -0,0 +1,63 @@ +package restic + +import ( + "encoding/json" + "fmt" + "strings" + "time" +) + +// Snapshot represents a single restic snapshot as returned by +// `restic snapshots --json`. +type Snapshot struct { + ID string `json:"id"` + ShortID string `json:"short_id"` + Time time.Time `json:"time"` + Hostname string `json:"hostname"` + Paths []string `json:"paths"` + Tags []string `json:"tags"` +} + +// ParseSnapshots decodes the JSON output of `restic snapshots --json` +// into a slice of Snapshot values. +func ParseSnapshots(data []byte) ([]Snapshot, error) { + var snapshots []Snapshot + if err := json.Unmarshal(data, &snapshots); err != nil { + return nil, fmt.Errorf("parsing snapshot JSON: %w", err) + } + return snapshots, nil +} + +// FormatSnapshotLine returns a single-line human-readable summary of a +// snapshot, suitable for use as a menu option label. The format is: +// +//