backup: embed progress.Printer in backup specific printer

This commit is contained in:
Michael Eischer
2025-09-14 19:47:54 +02:00
parent e753941ad3
commit 1a76f988ea
5 changed files with 31 additions and 32 deletions

View File

@@ -478,7 +478,12 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
var vsscfg fs.VSSConfig var vsscfg fs.VSSConfig
var err error 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 runtime.GOOS == "windows" {
if vsscfg, err = fs.ParseVSSConfig(gopts.extended); err != nil { if vsscfg, err = fs.ParseVSSConfig(gopts.extended); err != nil {
return err return err
@@ -490,7 +495,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
return err return err
} }
targets, err := collectTargets(opts, args, msg.E, term.InputRaw()) targets, err := collectTargets(opts, args, printer.E, term.InputRaw())
if err != nil { if err != nil {
return err return err
} }
@@ -505,27 +510,21 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
} }
if gopts.verbosity >= 2 && !gopts.JSON { 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 { if err != nil {
return err return err
} }
defer unlock() defer unlock()
var progressPrinter backup.ProgressPrinter progressReporter := backup.NewProgress(printer,
if gopts.JSON {
progressPrinter = backup.NewJSONProgress(term, gopts.verbosity)
} else {
progressPrinter = backup.NewTextProgress(term, gopts.verbosity)
}
progressReporter := backup.NewProgress(progressPrinter,
ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus())) ui.CalculateProgressInterval(!gopts.Quiet, gopts.JSON, term.CanUpdateStatus()))
defer progressReporter.Done() defer progressReporter.Done()
// rejectByNameFuncs collect functions that can reject items from the backup based on path only // 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 { if err != nil {
return err return err
} }
@@ -539,18 +538,18 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
if !gopts.JSON { if !gopts.JSON {
if parentSnapshot != nil { if parentSnapshot != nil {
progressPrinter.P("using parent snapshot %v\n", parentSnapshot.ID().Str()) printer.P("using parent snapshot %v\n", parentSnapshot.ID().Str())
} else { } 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 { 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) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err
@@ -568,7 +567,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
messageHandler := func(msg string, args ...interface{}) { messageHandler := func(msg string, args ...interface{}) {
if !gopts.JSON { 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 opts.Stdin || opts.StdinCommand {
if !gopts.JSON { if !gopts.JSON {
progressPrinter.V("read data from stdin") printer.V("read data from stdin")
} }
filename := path.Join("/", opts.StdinFilename) filename := path.Join("/", opts.StdinFilename)
var source io.ReadCloser = term.InputRaw() var source io.ReadCloser = term.InputRaw()
if opts.StdinCommand { if opts.StdinCommand {
source, err = fs.NewCommandReader(ctx, args, msg.E) source, err = fs.NewCommandReader(ctx, args, printer.E)
if err != nil { if err != nil {
return err 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 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 { if err != nil {
return err return err
} }
@@ -620,11 +619,11 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
sc := archiver.NewScanner(targetFS) sc := archiver.NewScanner(targetFS)
sc.SelectByName = selectByNameFilter sc.SelectByName = selectByNameFilter
sc.Select = selectFilter sc.Select = selectFilter
sc.Error = progressPrinter.ScannerError sc.Error = printer.ScannerError
sc.Result = progressReporter.ReportTotal sc.Result = progressReporter.ReportTotal
if !gopts.JSON { 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) }) 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 { 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) _, id, summary, err := arch.Snapshot(ctx, targets, snapshotOpts)

View File

@@ -7,11 +7,12 @@ import (
"github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/progress"
) )
// JSONProgress reports progress for the `backup` command in JSON. // JSONProgress reports progress for the `backup` command in JSON.
type JSONProgress struct { type JSONProgress struct {
*ui.Message progress.Printer
term ui.Terminal term ui.Terminal
v uint v uint
@@ -23,7 +24,7 @@ var _ ProgressPrinter = &JSONProgress{}
// NewJSONProgress returns a new backup progress reporter. // NewJSONProgress returns a new backup progress reporter.
func NewJSONProgress(term ui.Terminal, verbosity uint) *JSONProgress { func NewJSONProgress(term ui.Terminal, verbosity uint) *JSONProgress {
return &JSONProgress{ return &JSONProgress{
Message: ui.NewMessage(term, verbosity), Printer: ui.NewProgressPrinter(true, verbosity, term),
term: term, term: term,
v: verbosity, v: verbosity,
} }

View File

@@ -20,8 +20,7 @@ type ProgressPrinter interface {
Finish(snapshotID restic.ID, summary *archiver.Summary, dryRun bool) Finish(snapshotID restic.ID, summary *archiver.Summary, dryRun bool)
Reset() Reset()
P(msg string, args ...interface{}) progress.Printer
V(msg string, args ...interface{})
} }
type Counter struct { type Counter struct {

View File

@@ -7,10 +7,12 @@ import (
"github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/progress"
) )
type mockPrinter struct { type mockPrinter struct {
sync.Mutex sync.Mutex
progress.NoopPrinter
dirUnchanged, fileNew bool dirUnchanged, fileNew bool
id restic.ID id restic.ID
} }
@@ -42,9 +44,6 @@ func (p *mockPrinter) Finish(id restic.ID, _ *archiver.Summary, _ bool) {
func (p *mockPrinter) Reset() {} func (p *mockPrinter) Reset() {}
func (p *mockPrinter) P(_ string, _ ...interface{}) {}
func (p *mockPrinter) V(_ string, _ ...interface{}) {}
func TestProgress(t *testing.T) { func TestProgress(t *testing.T) {
t.Parallel() t.Parallel()

View File

@@ -8,11 +8,12 @@ import (
"github.com/restic/restic/internal/archiver" "github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/progress"
) )
// TextProgress reports progress for the `backup` command. // TextProgress reports progress for the `backup` command.
type TextProgress struct { type TextProgress struct {
*ui.Message progress.Printer
term ui.Terminal term ui.Terminal
verbosity uint verbosity uint
@@ -24,7 +25,7 @@ var _ ProgressPrinter = &TextProgress{}
// NewTextProgress returns a new backup progress reporter. // NewTextProgress returns a new backup progress reporter.
func NewTextProgress(term ui.Terminal, verbosity uint) *TextProgress { func NewTextProgress(term ui.Terminal, verbosity uint) *TextProgress {
return &TextProgress{ return &TextProgress{
Message: ui.NewMessage(term, verbosity), Printer: ui.NewProgressPrinter(false, verbosity, term),
term: term, term: term,
verbosity: verbosity, verbosity: verbosity,
} }