From 1a76f988ea0bcad89f4fd86a2ab13cacd17079ee Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 14 Sep 2025 19:47:54 +0200 Subject: [PATCH] backup: embed progress.Printer in backup specific printer --- cmd/restic/cmd_backup.go | 45 ++++++++++++++--------------- internal/ui/backup/json.go | 5 ++-- internal/ui/backup/progress.go | 3 +- internal/ui/backup/progress_test.go | 5 ++-- internal/ui/backup/text.go | 5 ++-- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index a69a4b46a..c137eafe1 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -478,7 +478,12 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter var vsscfg fs.VSSConfig var err error - msg := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, term) + var printer backup.ProgressPrinter + if gopts.JSON { + printer = backup.NewJSONProgress(term, gopts.verbosity) + } else { + printer = backup.NewTextProgress(term, gopts.verbosity) + } if runtime.GOOS == "windows" { if vsscfg, err = fs.ParseVSSConfig(gopts.extended); err != nil { return err @@ -490,7 +495,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter return err } - targets, err := collectTargets(opts, args, msg.E, term.InputRaw()) + targets, err := collectTargets(opts, args, printer.E, term.InputRaw()) if err != nil { return err } @@ -505,27 +510,21 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter } if gopts.verbosity >= 2 && !gopts.JSON { - msg.P("open repository") + printer.P("open repository") } - ctx, repo, unlock, err := openWithAppendLock(ctx, gopts, opts.DryRun, msg) + ctx, repo, unlock, err := openWithAppendLock(ctx, gopts, opts.DryRun, printer) if err != nil { return err } defer unlock() - var progressPrinter backup.ProgressPrinter - if gopts.JSON { - progressPrinter = backup.NewJSONProgress(term, gopts.verbosity) - } else { - progressPrinter = backup.NewTextProgress(term, gopts.verbosity) - } - progressReporter := backup.NewProgress(progressPrinter, + progressReporter := backup.NewProgress(printer, ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus())) defer progressReporter.Done() // rejectByNameFuncs collect functions that can reject items from the backup based on path only - rejectByNameFuncs, err := collectRejectByNameFuncs(opts, repo, msg.E) + rejectByNameFuncs, err := collectRejectByNameFuncs(opts, repo, printer.E) if err != nil { return err } @@ -539,18 +538,18 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter if !gopts.JSON { if parentSnapshot != nil { - progressPrinter.P("using parent snapshot %v\n", parentSnapshot.ID().Str()) + printer.P("using parent snapshot %v\n", parentSnapshot.ID().Str()) } else { - progressPrinter.P("no parent snapshot found, will read all files\n") + printer.P("no parent snapshot found, will read all files\n") } } } if !gopts.JSON { - progressPrinter.V("load index files") + printer.V("load index files") } - bar := ui.NewIndexCounter(msg) + bar := ui.NewIndexCounter(printer) err = repo.LoadIndex(ctx, bar) if err != nil { return err @@ -568,7 +567,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter messageHandler := func(msg string, args ...interface{}) { if !gopts.JSON { - progressPrinter.P(msg, args...) + printer.P(msg, args...) } } @@ -579,12 +578,12 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter if opts.Stdin || opts.StdinCommand { if !gopts.JSON { - progressPrinter.V("read data from stdin") + printer.V("read data from stdin") } filename := path.Join("/", opts.StdinFilename) var source io.ReadCloser = term.InputRaw() if opts.StdinCommand { - source, err = fs.NewCommandReader(ctx, args, msg.E) + source, err = fs.NewCommandReader(ctx, args, printer.E) if err != nil { return err } @@ -604,7 +603,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter } // rejectFuncs collect functions that can reject items from the backup based on path and file info - rejectFuncs, err := collectRejectFuncs(opts, targets, targetFS, msg.E) + rejectFuncs, err := collectRejectFuncs(opts, targets, targetFS, printer.E) if err != nil { return err } @@ -620,11 +619,11 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter sc := archiver.NewScanner(targetFS) sc.SelectByName = selectByNameFilter sc.Select = selectFilter - sc.Error = progressPrinter.ScannerError + sc.Error = printer.ScannerError sc.Result = progressReporter.ReportTotal if !gopts.JSON { - progressPrinter.V("start scan on %v", targets) + printer.V("start scan on %v", targets) } wg.Go(func() error { return sc.Scan(cancelCtx, targets) }) } @@ -669,7 +668,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter } if !gopts.JSON { - progressPrinter.V("start backup on %v", targets) + printer.V("start backup on %v", targets) } _, id, summary, err := arch.Snapshot(ctx, targets, snapshotOpts) diff --git a/internal/ui/backup/json.go b/internal/ui/backup/json.go index 79da353eb..b46bbdc5f 100644 --- a/internal/ui/backup/json.go +++ b/internal/ui/backup/json.go @@ -7,11 +7,12 @@ import ( "github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/ui" + "github.com/restic/restic/internal/ui/progress" ) // JSONProgress reports progress for the `backup` command in JSON. type JSONProgress struct { - *ui.Message + progress.Printer term ui.Terminal v uint @@ -23,7 +24,7 @@ var _ ProgressPrinter = &JSONProgress{} // NewJSONProgress returns a new backup progress reporter. func NewJSONProgress(term ui.Terminal, verbosity uint) *JSONProgress { return &JSONProgress{ - Message: ui.NewMessage(term, verbosity), + Printer: ui.NewProgressPrinter(true, verbosity, term), term: term, v: verbosity, } diff --git a/internal/ui/backup/progress.go b/internal/ui/backup/progress.go index 318d30435..0feaf64eb 100644 --- a/internal/ui/backup/progress.go +++ b/internal/ui/backup/progress.go @@ -20,8 +20,7 @@ type ProgressPrinter interface { Finish(snapshotID restic.ID, summary *archiver.Summary, dryRun bool) Reset() - P(msg string, args ...interface{}) - V(msg string, args ...interface{}) + progress.Printer } type Counter struct { diff --git a/internal/ui/backup/progress_test.go b/internal/ui/backup/progress_test.go index 60e754b4a..5c088336d 100644 --- a/internal/ui/backup/progress_test.go +++ b/internal/ui/backup/progress_test.go @@ -7,10 +7,12 @@ import ( "github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui/progress" ) type mockPrinter struct { sync.Mutex + progress.NoopPrinter dirUnchanged, fileNew bool id restic.ID } @@ -42,9 +44,6 @@ func (p *mockPrinter) Finish(id restic.ID, _ *archiver.Summary, _ bool) { func (p *mockPrinter) Reset() {} -func (p *mockPrinter) P(_ string, _ ...interface{}) {} -func (p *mockPrinter) V(_ string, _ ...interface{}) {} - func TestProgress(t *testing.T) { t.Parallel() diff --git a/internal/ui/backup/text.go b/internal/ui/backup/text.go index 359331b27..e4981fdec 100644 --- a/internal/ui/backup/text.go +++ b/internal/ui/backup/text.go @@ -8,11 +8,12 @@ import ( "github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/ui" + "github.com/restic/restic/internal/ui/progress" ) // TextProgress reports progress for the `backup` command. type TextProgress struct { - *ui.Message + progress.Printer term ui.Terminal verbosity uint @@ -24,7 +25,7 @@ var _ ProgressPrinter = &TextProgress{} // NewTextProgress returns a new backup progress reporter. func NewTextProgress(term ui.Terminal, verbosity uint) *TextProgress { return &TextProgress{ - Message: ui.NewMessage(term, verbosity), + Printer: ui.NewProgressPrinter(false, verbosity, term), term: term, verbosity: verbosity, }