From 52eb66929f447f4e05a84fe132f7770d804327d3 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 21 Sep 2025 17:43:44 +0200 Subject: [PATCH] repository: deduplicate index progress bar initializaton --- cmd/restic/cmd_backup.go | 3 +-- cmd/restic/cmd_cat.go | 6 ++---- cmd/restic/cmd_check.go | 3 +-- cmd/restic/cmd_copy.go | 6 ++---- cmd/restic/cmd_debug.go | 3 +-- cmd/restic/cmd_diff.go | 3 +-- cmd/restic/cmd_dump.go | 3 +-- cmd/restic/cmd_find.go | 3 +-- cmd/restic/cmd_ls.go | 3 +-- cmd/restic/cmd_mount.go | 3 +-- cmd/restic/cmd_prune.go | 3 +-- cmd/restic/cmd_recover.go | 5 ++--- cmd/restic/cmd_repair_packs.go | 3 +-- cmd/restic/cmd_repair_snapshots.go | 3 +-- cmd/restic/cmd_restore.go | 3 +-- cmd/restic/cmd_rewrite.go | 3 +-- cmd/restic/cmd_stats.go | 3 +-- internal/checker/checker.go | 9 +++++++-- internal/repository/repository.go | 9 +++++++-- internal/restic/repository.go | 8 +++++++- internal/ui/progress.go | 4 ---- 21 files changed, 41 insertions(+), 48 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index c137eafe1..9cd22ae01 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -549,8 +549,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter printer.V("load index files") } - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index 3c3573d66..967687171 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -168,8 +168,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter return err case "blob": - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } @@ -196,8 +195,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter return errors.Fatalf("could not find snapshot: %v", err) } - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 9897924e4..afc7835f0 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -249,8 +249,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args } printer.P("load indexes\n") - bar := ui.NewIndexCounter(printer) - hints, errs := chkr.LoadIndex(ctx, bar) + hints, errs := chkr.LoadIndex(ctx, printer) if ctx.Err() != nil { return summary, ctx.Err() } diff --git a/cmd/restic/cmd_copy.go b/cmd/restic/cmd_copy.go index cd627af3a..19bfe8679 100644 --- a/cmd/restic/cmd_copy.go +++ b/cmd/restic/cmd_copy.go @@ -101,13 +101,11 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] } debug.Log("Loading source index") - bar := ui.NewIndexCounter(printer) - if err := srcRepo.LoadIndex(ctx, bar); err != nil { + if err := srcRepo.LoadIndex(ctx, printer); err != nil { return err } - bar = ui.NewIndexCounter(printer) debug.Log("Loading destination index") - if err := dstRepo.LoadIndex(ctx, bar); err != nil { + if err := dstRepo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index 4709fffec..00e312b13 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -484,8 +484,7 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamine return errors.Fatal("no pack files to examine") } - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_diff.go b/cmd/restic/cmd_diff.go index f1e832cb8..a25c79fd9 100644 --- a/cmd/restic/cmd_diff.go +++ b/cmd/restic/cmd_diff.go @@ -391,8 +391,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args [] if !gopts.JSON { printer.P("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str()) } - bar := ui.NewIndexCounter(printer) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index fcbd9cfd4..b36fb2236 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -160,8 +160,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args [] return errors.Fatalf("failed to find snapshot: %v", err) } - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index dc2564a57..bb3c0b55a 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -623,8 +623,7 @@ func runFind(ctx context.Context, opts FindOptions, gopts GlobalOptions, args [] if err != nil { return err } - bar := ui.NewIndexCounter(printer) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index 2c973586c..de015f180 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -373,8 +373,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri return err } - bar := ui.NewIndexCounter(termPrinter) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, termPrinter); err != nil { return err } diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index 2b12b9b98..84e28f86f 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -146,8 +146,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args } defer unlock() - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_prune.go b/cmd/restic/cmd_prune.go index 6cefc2d81..dabcc8293 100644 --- a/cmd/restic/cmd_prune.go +++ b/cmd/restic/cmd_prune.go @@ -190,8 +190,7 @@ func runPruneWithRepo(ctx context.Context, opts PruneOptions, repo *repository.R } // loading the index before the snapshots is ok, as we use an exclusive lock here - bar := ui.NewIndexCounter(printer) - err := repo.LoadIndex(ctx, bar) + err := repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_recover.go b/cmd/restic/cmd_recover.go index 94570ae31..3d1b7aec0 100644 --- a/cmd/restic/cmd_recover.go +++ b/cmd/restic/cmd_recover.go @@ -66,8 +66,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro } printer.P("load index files\n") - bar := ui.NewIndexCounter(printer) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, printer); err != nil { return err } @@ -85,7 +84,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro } printer.P("load %d trees\n", len(trees)) - bar = printer.NewCounter("trees loaded") + bar := printer.NewCounter("trees loaded") bar.SetMax(uint64(len(trees))) for id := range trees { tree, err := restic.LoadTree(ctx, repo, id) diff --git a/cmd/restic/cmd_repair_packs.go b/cmd/restic/cmd_repair_packs.go index 237eec913..b8e68a65e 100644 --- a/cmd/restic/cmd_repair_packs.go +++ b/cmd/restic/cmd_repair_packs.go @@ -59,8 +59,7 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term ui.Terminal, } defer unlock() - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return errors.Fatalf("%s", err) } diff --git a/cmd/restic/cmd_repair_snapshots.go b/cmd/restic/cmd_repair_snapshots.go index eacf7f2e5..0c7608e7f 100644 --- a/cmd/restic/cmd_repair_snapshots.go +++ b/cmd/restic/cmd_repair_snapshots.go @@ -87,8 +87,7 @@ func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOpt return err } - bar := ui.NewIndexCounter(printer) - if err := repo.LoadIndex(ctx, bar); err != nil { + if err := repo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 692c5e259..fab22327e 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -151,8 +151,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, return errors.Fatalf("failed to find snapshot: %v", err) } - bar := ui.NewIndexCounter(printer) - err = repo.LoadIndex(ctx, bar) + err = repo.LoadIndex(ctx, printer) if err != nil { return err } diff --git a/cmd/restic/cmd_rewrite.go b/cmd/restic/cmd_rewrite.go index 45a0d6a01..fb6f66b32 100644 --- a/cmd/restic/cmd_rewrite.go +++ b/cmd/restic/cmd_rewrite.go @@ -318,8 +318,7 @@ func runRewrite(ctx context.Context, opts RewriteOptions, gopts GlobalOptions, a return err } - bar := ui.NewIndexCounter(printer) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index 651e7ad2f..604805e67 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -111,8 +111,7 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args if err != nil { return err } - bar := ui.NewIndexCounter(printer) - if err = repo.LoadIndex(ctx, bar); err != nil { + if err = repo.LoadIndex(ctx, printer); err != nil { return err } diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 12020891a..1f5d29a67 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -93,11 +93,16 @@ func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.I } // LoadIndex loads all index files. -func (c *Checker) LoadIndex(ctx context.Context, p *progress.Counter) (hints []error, errs []error) { +func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) (hints []error, errs []error) { debug.Log("Start") + var bar *progress.Counter + if p != nil { + bar = p.NewCounterTerminalOnly("index files loaded") + } + packToIndex := make(map[restic.ID]restic.IDSet) - err := c.masterIndex.Load(ctx, c.repo, p, func(id restic.ID, idx *index.Index, err error) error { + err := c.masterIndex.Load(ctx, c.repo, bar, func(id restic.ID, idx *index.Index, err error) error { debug.Log("process index %v, err %v", id, err) err = errors.Wrapf(err, "error loading index %v", id) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index d5da0ca5f..8d1b35d91 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -639,13 +639,18 @@ func (r *Repository) clearIndex() { } // LoadIndex loads all index files from the backend in parallel and stores them -func (r *Repository) LoadIndex(ctx context.Context, p *progress.Counter) error { +func (r *Repository) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) error { debug.Log("Loading index") // reset in-memory index before loading it from the repository r.clearIndex() - err := r.idx.Load(ctx, r, p, nil) + var bar *progress.Counter + if p != nil { + bar = p.NewCounterTerminalOnly("index files loaded") + } + + err := r.idx.Load(ctx, r, bar, nil) if err != nil { return err } diff --git a/internal/restic/repository.go b/internal/restic/repository.go index 8462e1b9e..5631202a9 100644 --- a/internal/restic/repository.go +++ b/internal/restic/repository.go @@ -21,7 +21,7 @@ type Repository interface { Config() Config Key() *crypto.Key - LoadIndex(ctx context.Context, p *progress.Counter) error + LoadIndex(ctx context.Context, p TerminalCounterFactory) error SetIndex(mi MasterIndex) error LookupBlob(t BlobType, id ID) []PackedBlob @@ -131,6 +131,12 @@ type PackBlobs struct { Blobs []Blob } +type TerminalCounterFactory interface { + // NewCounterTerminalOnly returns a new progress counter that is only shown if stdout points to a + // terminal. It is not shown if --quiet or --json is specified. + NewCounterTerminalOnly(description string) *progress.Counter +} + // MasterIndex keeps track of the blobs are stored within files. type MasterIndex interface { Has(bh BlobHandle) bool diff --git a/internal/ui/progress.go b/internal/ui/progress.go index 9e18da5ec..b3a3226e9 100644 --- a/internal/ui/progress.go +++ b/internal/ui/progress.go @@ -76,7 +76,3 @@ func NewProgressPrinter(json bool, verbosity uint, term Terminal) progress.Print show: verbosity > 0, } } - -func NewIndexCounter(printer progress.Printer) *progress.Counter { - return printer.NewCounterTerminalOnly("index files loaded") -}