copy: convert to use termstatus

This commit is contained in:
Michael Eischer
2025-09-13 22:50:07 +02:00
parent 94b19d64be
commit e63aee2ec6
2 changed files with 24 additions and 22 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "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" "golang.org/x/sync/errgroup"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -46,7 +48,9 @@ Exit status is 12 if the password is incorrect.
GroupID: cmdGroupDefault, GroupID: cmdGroupDefault,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error { 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) 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") secondaryGopts, isFromRepo, err := fillSecondaryGlobalOpts(ctx, opts.secondaryRepoOptions, gopts, "destination")
if err != nil { if err != nil {
return err return err
@@ -75,6 +79,8 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
gopts, secondaryGopts = secondaryGopts, gopts gopts, secondaryGopts = secondaryGopts, gopts
} }
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term)
ctx, srcRepo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock) ctx, srcRepo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
if err != nil { if err != nil {
return err return err
@@ -98,11 +104,11 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
} }
debug.Log("Loading source index") debug.Log("Loading source index")
bar := newIndexProgress(gopts.Quiet, gopts.JSON) bar := newIndexTerminalProgress(printer)
if err := srcRepo.LoadIndex(ctx, bar); err != nil { if err := srcRepo.LoadIndex(ctx, bar); err != nil {
return err return err
} }
bar = newIndexProgress(gopts.Quiet, gopts.JSON) bar = newIndexTerminalProgress(printer)
debug.Log("Loading destination index") debug.Log("Loading destination index")
if err := dstRepo.LoadIndex(ctx, bar); err != nil { if err := dstRepo.LoadIndex(ctx, bar); err != nil {
return err return err
@@ -134,8 +140,8 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
isCopy := false isCopy := false
for _, originalSn := range originalSns { for _, originalSn := range originalSns {
if similarSnapshots(originalSn, sn) { if similarSnapshots(originalSn, sn) {
Verboseff("\n%v\n", sn) printer.V("\n%v", sn)
Verboseff("skipping source snapshot %s, was already copied to snapshot %s\n", sn.ID().Str(), originalSn.ID().Str()) printer.V("skipping source snapshot %s, was already copied to snapshot %s", sn.ID().Str(), originalSn.ID().Str())
isCopy = true isCopy = true
break break
} }
@@ -144,9 +150,9 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
continue continue
} }
} }
Verbosef("\n%v\n", sn) printer.P("\n%v", sn)
Verbosef(" copy started, this may take a while...\n") printer.P(" copy started, this may take a while...")
if err := copyTree(ctx, srcRepo, dstRepo, visitedTrees, *sn.Tree, gopts.Quiet); err != nil { if err := copyTree(ctx, srcRepo, dstRepo, visitedTrees, *sn.Tree, printer); err != nil {
return err return err
} }
debug.Log("tree copied") debug.Log("tree copied")
@@ -161,7 +167,7 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
if err != nil { if err != nil {
return err return err
} }
Verbosef("snapshot %s saved\n", newID.Str()) printer.P("snapshot %s saved", newID.Str())
} }
return ctx.Err() 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, 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) wg, wgCtx := errgroup.WithContext(ctx)
@@ -238,16 +244,9 @@ func copyTree(ctx context.Context, srcRepo restic.Repository, dstRepo restic.Rep
return err return err
} }
bar := newProgressMax(!quiet, uint64(len(packList)), "packs copied") bar := printer.NewCounter("packs copied")
_, err = repository.Repack( bar.SetMax(uint64(len(packList)))
ctx, _, err = repository.Repack(ctx, srcRepo, dstRepo, packList, copyBlobs, bar, printer.P)
srcRepo,
dstRepo,
packList,
copyBlobs,
bar,
func(msg string, args ...interface{}) { fmt.Printf(msg+"\n", args...) },
)
bar.Done() bar.Done()
if err != nil { if err != nil {
return errors.Fatal(err.Error()) return errors.Fatal(err.Error())

View File

@@ -7,6 +7,7 @@ import (
"testing" "testing"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/termstatus"
) )
func testRunCopy(t testing.TB, srcGopts GlobalOptions, dstGopts GlobalOptions) { 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) { func TestCopy(t *testing.T) {