From e63aee2ec6b4ec7b21712495f1022fd79a5ef242 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 13 Sep 2025 22:50:07 +0200 Subject: [PATCH] copy: convert to use termstatus --- cmd/restic/cmd_copy.go | 41 ++++++++++++------------- cmd/restic/cmd_copy_integration_test.go | 5 ++- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/cmd/restic/cmd_copy.go b/cmd/restic/cmd_copy.go index 2ad5a464c..1467a9982 100644 --- a/cmd/restic/cmd_copy.go +++ b/cmd/restic/cmd_copy.go @@ -8,6 +8,8 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui/progress" + "github.com/restic/restic/internal/ui/termstatus" "golang.org/x/sync/errgroup" "github.com/spf13/cobra" @@ -46,7 +48,9 @@ Exit status is 12 if the password is incorrect. GroupID: cmdGroupDefault, DisableAutoGenTag: true, RunE: func(cmd *cobra.Command, args []string) error { - return runCopy(cmd.Context(), opts, globalOptions, args) + term, cancel := setupTermstatus() + defer cancel() + return runCopy(cmd.Context(), opts, globalOptions, args, term) }, } @@ -65,7 +69,7 @@ func (opts *CopyOptions) AddFlags(f *pflag.FlagSet) { initMultiSnapshotFilter(f, &opts.SnapshotFilter, true) } -func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []string) error { +func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error { secondaryGopts, isFromRepo, err := fillSecondaryGlobalOpts(ctx, opts.secondaryRepoOptions, gopts, "destination") if err != nil { return err @@ -75,6 +79,8 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] gopts, secondaryGopts = secondaryGopts, gopts } + printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term) + ctx, srcRepo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock) if err != nil { return err @@ -98,11 +104,11 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] } debug.Log("Loading source index") - bar := newIndexProgress(gopts.Quiet, gopts.JSON) + bar := newIndexTerminalProgress(printer) if err := srcRepo.LoadIndex(ctx, bar); err != nil { return err } - bar = newIndexProgress(gopts.Quiet, gopts.JSON) + bar = newIndexTerminalProgress(printer) debug.Log("Loading destination index") if err := dstRepo.LoadIndex(ctx, bar); err != nil { return err @@ -134,8 +140,8 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] isCopy := false for _, originalSn := range originalSns { if similarSnapshots(originalSn, sn) { - Verboseff("\n%v\n", sn) - Verboseff("skipping source snapshot %s, was already copied to snapshot %s\n", sn.ID().Str(), originalSn.ID().Str()) + printer.V("\n%v", sn) + printer.V("skipping source snapshot %s, was already copied to snapshot %s", sn.ID().Str(), originalSn.ID().Str()) isCopy = true break } @@ -144,9 +150,9 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] continue } } - Verbosef("\n%v\n", sn) - Verbosef(" copy started, this may take a while...\n") - if err := copyTree(ctx, srcRepo, dstRepo, visitedTrees, *sn.Tree, gopts.Quiet); err != nil { + printer.P("\n%v", sn) + printer.P(" copy started, this may take a while...") + if err := copyTree(ctx, srcRepo, dstRepo, visitedTrees, *sn.Tree, printer); err != nil { return err } debug.Log("tree copied") @@ -161,7 +167,7 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args [] if err != nil { return err } - Verbosef("snapshot %s saved\n", newID.Str()) + printer.P("snapshot %s saved", newID.Str()) } return ctx.Err() } @@ -186,7 +192,7 @@ func similarSnapshots(sna *restic.Snapshot, snb *restic.Snapshot) bool { } func copyTree(ctx context.Context, srcRepo restic.Repository, dstRepo restic.Repository, - visitedTrees restic.IDSet, rootTreeID restic.ID, quiet bool) error { + visitedTrees restic.IDSet, rootTreeID restic.ID, printer progress.Printer) error { wg, wgCtx := errgroup.WithContext(ctx) @@ -238,16 +244,9 @@ func copyTree(ctx context.Context, srcRepo restic.Repository, dstRepo restic.Rep return err } - bar := newProgressMax(!quiet, uint64(len(packList)), "packs copied") - _, err = repository.Repack( - ctx, - srcRepo, - dstRepo, - packList, - copyBlobs, - bar, - func(msg string, args ...interface{}) { fmt.Printf(msg+"\n", args...) }, - ) + bar := printer.NewCounter("packs copied") + bar.SetMax(uint64(len(packList))) + _, err = repository.Repack(ctx, srcRepo, dstRepo, packList, copyBlobs, bar, printer.P) bar.Done() if err != nil { return errors.Fatal(err.Error()) diff --git a/cmd/restic/cmd_copy_integration_test.go b/cmd/restic/cmd_copy_integration_test.go index 9ae78ba50..83cfb8425 100644 --- a/cmd/restic/cmd_copy_integration_test.go +++ b/cmd/restic/cmd_copy_integration_test.go @@ -7,6 +7,7 @@ import ( "testing" rtest "github.com/restic/restic/internal/test" + "github.com/restic/restic/internal/ui/termstatus" ) func testRunCopy(t testing.TB, srcGopts GlobalOptions, dstGopts GlobalOptions) { @@ -22,7 +23,9 @@ func testRunCopy(t testing.TB, srcGopts GlobalOptions, dstGopts GlobalOptions) { }, } - rtest.OK(t, runCopy(context.TODO(), copyOpts, gopts, nil)) + rtest.OK(t, withTermStatus(gopts, func(ctx context.Context, term *termstatus.Terminal) error { + return runCopy(context.TODO(), copyOpts, gopts, nil, term) + })) } func TestCopy(t *testing.T) {