From d62bfed65d4911e56d95d7ff8d3e836653046b2f Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Wed, 18 Aug 2021 13:03:08 +0200 Subject: [PATCH] ui: move SetDryRun to ProgressReporter --- cmd/restic/cmd_backup.go | 2 +- internal/ui/backup.go | 9 ++------- internal/ui/json/backup.go | 10 ++-------- internal/ui/progress.go | 11 ++++++++--- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index c67393b01..d4ec3c288 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -537,7 +537,7 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina if opts.DryRun { repo.SetDryRun() - progressPrinter.SetDryRun() + progressReporter.SetDryRun() } // use the terminal for stdout/stderr diff --git a/internal/ui/backup.go b/internal/ui/backup.go index 9449f0193..17181dc70 100644 --- a/internal/ui/backup.go +++ b/internal/ui/backup.go @@ -17,7 +17,6 @@ type Backup struct { *StdioWrapper term *termstatus.Terminal - dry bool // true if writes are faked } // NewBackup returns a new backup progress reporter. @@ -165,14 +164,14 @@ func (b *Backup) Reset() { } // Finish prints the finishing messages. -func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *Summary) { +func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool) { b.P("\n") b.P("Files: %5d new, %5d changed, %5d unmodified\n", summary.Files.New, summary.Files.Changed, summary.Files.Unchanged) b.P("Dirs: %5d new, %5d changed, %5d unmodified\n", summary.Dirs.New, summary.Dirs.Changed, summary.Dirs.Unchanged) b.V("Data Blobs: %5d new\n", summary.ItemStats.DataBlobs) b.V("Tree Blobs: %5d new\n", summary.ItemStats.TreeBlobs) verb := "Added" - if b.dry { + if dryRun { verb = "Would add" } b.P("%s to the repo: %-5s\n", verb, formatBytes(summary.ItemStats.DataSize+summary.ItemStats.TreeSize)) @@ -183,7 +182,3 @@ func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *Summary) formatDuration(time.Since(start)), ) } - -func (b *Backup) SetDryRun() { - b.dry = true -} diff --git a/internal/ui/json/backup.go b/internal/ui/json/backup.go index 274f2b043..acaa394bc 100644 --- a/internal/ui/json/backup.go +++ b/internal/ui/json/backup.go @@ -20,7 +20,6 @@ type Backup struct { term *termstatus.Terminal v uint - dry bool } // NewBackup returns a new backup progress reporter. @@ -169,7 +168,7 @@ func (b *Backup) ReportTotal(item string, start time.Time, s archiver.ScanStats) } // Finish prints the finishing messages. -func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *ui.Summary) { +func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *ui.Summary, dryRun bool) { b.print(summaryOutput{ MessageType: "summary", FilesNew: summary.Files.New, @@ -185,7 +184,7 @@ func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *ui.Summa TotalBytesProcessed: summary.ProcessedBytes, TotalDuration: time.Since(start).Seconds(), SnapshotID: snapshotID.Str(), - DryRun: b.dry, + DryRun: dryRun, }) } @@ -193,11 +192,6 @@ func (b *Backup) Finish(snapshotID restic.ID, start time.Time, summary *ui.Summa func (b *Backup) Reset() { } -// SetDryRun marks the backup as a "dry run". -func (b *Backup) SetDryRun() { - b.dry = true -} - type statusUpdate struct { MessageType string `json:"message_type"` // "status" SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"` diff --git a/internal/ui/progress.go b/internal/ui/progress.go index 66859ba18..b0d2e8ed3 100644 --- a/internal/ui/progress.go +++ b/internal/ui/progress.go @@ -18,9 +18,8 @@ type ProgressPrinter interface { ScannerError(item string, fi os.FileInfo, err error) error CompleteItem(messageType string, item string, previous, current *restic.Node, s archiver.ItemStats, d time.Duration) ReportTotal(item string, start time.Time, s archiver.ScanStats) - Finish(snapshotID restic.ID, start time.Time, summary *Summary) + Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool) Reset() - SetDryRun() // ui.StdioWrapper Stdout() io.WriteCloser @@ -69,6 +68,7 @@ type Progress struct { MinUpdatePause time.Duration start time.Time + dry bool totalBytes uint64 @@ -310,7 +310,7 @@ func (p *Progress) ReportTotal(item string, s archiver.ScanStats) { func (p *Progress) Finish(snapshotID restic.ID) { // wait for the status update goroutine to shut down <-p.closed - p.printer.Finish(snapshotID, p.start, p.summary) + p.printer.Finish(snapshotID, p.start, p.summary, p.dry) } // SetMinUpdatePause sets b.MinUpdatePause. It satisfies the @@ -318,3 +318,8 @@ func (p *Progress) Finish(snapshotID restic.ID) { func (p *Progress) SetMinUpdatePause(d time.Duration) { p.MinUpdatePause = d } + +// SetDryRun marks the backup as a "dry run". +func (p *Progress) SetDryRun() { + p.dry = true +}