feat(backup): add ListTargetsWithBackups for TUI discovery

Scans backup directory and returns per-target summaries (count,
oldest, latest) sorted by target name. Uses timestamp parsing
instead of modtime for accuracy.
This commit is contained in:
Willie
2026-03-02 02:23:50 +08:00
parent b2af4f4929
commit 2bcd2bf4de
2 changed files with 191 additions and 0 deletions
+80
View File
@@ -120,6 +120,86 @@ type BackupInfo struct {
Date time.Time
}
// TargetBackupSummary holds aggregated backup info for a single target.
type TargetBackupSummary struct {
TargetName string
BackupCount int
Latest time.Time
Oldest time.Time
}
// ListTargetsWithBackups scans the backup directory and returns per-target
// summaries (count, oldest, latest) sorted by target name.
// Returns nil, nil for a non-existent directory.
func ListTargetsWithBackups(backupDir string) ([]TargetBackupSummary, error) {
entries, err := os.ReadDir(backupDir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
// Aggregate per target: count and time range.
type accumulator struct {
count int
latest time.Time
oldest time.Time
}
targets := make(map[string]*accumulator)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
ts, parseErr := time.ParseInLocation("2006-01-02_15-04-05", entry.Name(), time.Local)
if parseErr != nil {
continue // skip directories that don't match the timestamp format
}
targetEntries, readErr := os.ReadDir(filepath.Join(backupDir, entry.Name()))
if readErr != nil {
continue
}
for _, te := range targetEntries {
if !te.IsDir() {
continue
}
name := te.Name()
acc, ok := targets[name]
if !ok {
acc = &accumulator{oldest: ts, latest: ts}
targets[name] = acc
}
acc.count++
if ts.Before(acc.oldest) {
acc.oldest = ts
}
if ts.After(acc.latest) {
acc.latest = ts
}
}
}
summaries := make([]TargetBackupSummary, 0, len(targets))
for name, acc := range targets {
summaries = append(summaries, TargetBackupSummary{
TargetName: name,
BackupCount: acc.count,
Latest: acc.latest,
Oldest: acc.oldest,
})
}
sort.Slice(summaries, func(i, j int) bool {
return summaries[i].TargetName < summaries[j].TargetName
})
return summaries, nil
}
// copyDir copies a directory recursively, skipping symlinks and junctions.
// Uses os.ReadDir + os.Lstat instead of filepath.Walk to avoid failures
// when os.Lstat on Windows junctions returns nil info with an error.
+111
View File
@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
)
func TestCopyDir_RegularFiles(t *testing.T) {
@@ -158,6 +159,116 @@ func TestValidateRestore_SymlinkTarget_IsAllowed(t *testing.T) {
}
}
func TestListTargetsWithBackups_Empty(t *testing.T) {
dir := t.TempDir()
summaries, err := ListTargetsWithBackups(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(summaries) != 0 {
t.Errorf("expected 0 summaries, got %d", len(summaries))
}
}
func TestListTargetsWithBackups_NonExistentDir(t *testing.T) {
summaries, err := ListTargetsWithBackups("/nonexistent/path/backups")
if err != nil {
t.Fatalf("unexpected error for non-existent dir: %v", err)
}
if summaries != nil {
t.Errorf("expected nil, got %v", summaries)
}
}
func TestListTargetsWithBackups_MultiBacks(t *testing.T) {
dir := t.TempDir()
// Create 3 timestamp directories with various targets
// Timestamp format matches backup.Create: 2006-01-02_15-04-05
timestamps := []string{
"2025-01-10_08-00-00",
"2025-02-15_12-30-00",
"2025-03-20_18-45-00",
}
// ts0: claude, cursor
os.MkdirAll(filepath.Join(dir, timestamps[0], "claude"), 0755)
os.MkdirAll(filepath.Join(dir, timestamps[0], "cursor"), 0755)
// ts1: claude, windsurf
os.MkdirAll(filepath.Join(dir, timestamps[1], "claude"), 0755)
os.MkdirAll(filepath.Join(dir, timestamps[1], "windsurf"), 0755)
// ts2: claude
os.MkdirAll(filepath.Join(dir, timestamps[2], "claude"), 0755)
summaries, err := ListTargetsWithBackups(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(summaries) != 3 {
t.Fatalf("expected 3 targets, got %d", len(summaries))
}
// Should be sorted by target name: claude, cursor, windsurf
if summaries[0].TargetName != "claude" {
t.Errorf("summaries[0].TargetName = %q, want %q", summaries[0].TargetName, "claude")
}
if summaries[1].TargetName != "cursor" {
t.Errorf("summaries[1].TargetName = %q, want %q", summaries[1].TargetName, "cursor")
}
if summaries[2].TargetName != "windsurf" {
t.Errorf("summaries[2].TargetName = %q, want %q", summaries[2].TargetName, "windsurf")
}
// claude: 3 backups, oldest=ts0, latest=ts2
if summaries[0].BackupCount != 3 {
t.Errorf("claude BackupCount = %d, want 3", summaries[0].BackupCount)
}
wantOldest := time.Date(2025, 1, 10, 8, 0, 0, 0, time.Local)
wantLatest := time.Date(2025, 3, 20, 18, 45, 0, 0, time.Local)
if !summaries[0].Oldest.Equal(wantOldest) {
t.Errorf("claude Oldest = %v, want %v", summaries[0].Oldest, wantOldest)
}
if !summaries[0].Latest.Equal(wantLatest) {
t.Errorf("claude Latest = %v, want %v", summaries[0].Latest, wantLatest)
}
// cursor: 1 backup
if summaries[1].BackupCount != 1 {
t.Errorf("cursor BackupCount = %d, want 1", summaries[1].BackupCount)
}
// windsurf: 1 backup
if summaries[2].BackupCount != 1 {
t.Errorf("windsurf BackupCount = %d, want 1", summaries[2].BackupCount)
}
}
func TestListTargetsWithBackups_SkipsFiles(t *testing.T) {
dir := t.TempDir()
// Create a timestamp dir with a target
os.MkdirAll(filepath.Join(dir, "2025-01-10_08-00-00", "claude"), 0755)
// Create a plain file at timestamp level (should be skipped)
os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("ignore"), 0644)
// Create a plain file inside timestamp dir (should be skipped as target)
os.WriteFile(filepath.Join(dir, "2025-01-10_08-00-00", "readme.txt"), []byte("ignore"), 0644)
summaries, err := ListTargetsWithBackups(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(summaries) != 1 {
t.Fatalf("expected 1 target, got %d", len(summaries))
}
if summaries[0].TargetName != "claude" {
t.Errorf("TargetName = %q, want %q", summaries[0].TargetName, "claude")
}
}
// --- helpers ---
func writeTestFile(t *testing.T, path, content string) {