From aa8e18cf32528435350d3a8e99b7d7f0cb49edba Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 29 Jun 2024 21:29:42 +0200 Subject: [PATCH] restore: add deletions to progress output --- doc/075_scripting.rst | 2 +- internal/restorer/restorer.go | 1 + internal/ui/restore/json.go | 2 ++ internal/ui/restore/json_test.go | 1 + internal/ui/restore/progress.go | 12 ++++++++++++ internal/ui/restore/progress_test.go | 2 ++ internal/ui/restore/text.go | 6 ++++-- internal/ui/restore/text_test.go | 1 + 8 files changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/075_scripting.rst b/doc/075_scripting.rst index b83fe5eb5..e11f280db 100644 --- a/doc/075_scripting.rst +++ b/doc/075_scripting.rst @@ -520,7 +520,7 @@ Only printed if `--verbose=2` is specified. +----------------------+-----------------------------------------------------------+ | ``message_type`` | Always "verbose_status" | +----------------------+-----------------------------------------------------------+ -| ``action`` | Either "restored", "updated" or "unchanged" | +| ``action`` | Either "restored", "updated", "unchanged" or "deleted" | +----------------------+-----------------------------------------------------------+ | ``item`` | The item in question | +----------------------+-----------------------------------------------------------+ diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index 9efaa64df..37072d9a9 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -499,6 +499,7 @@ func (res *Restorer) removeUnexpectedFiles(target, location string, expectedFile selectedForRestore, _ := res.SelectFilter(nodeLocation, false) // only delete files that were selected for restore if selectedForRestore { + res.opts.Progress.ReportDeletedFile(nodeLocation) if !res.opts.DryRun { if err := fs.RemoveAll(nodeTarget); err != nil { return err diff --git a/internal/ui/restore/json.go b/internal/ui/restore/json.go index ebc217176..7db2e21a3 100644 --- a/internal/ui/restore/json.go +++ b/internal/ui/restore/json.go @@ -56,6 +56,8 @@ func (t *jsonPrinter) CompleteItem(messageType ItemAction, item string, size uin action = "updated" case ActionFileUnchanged: action = "unchanged" + case ActionDeleted: + action = "deleted" default: panic("unknown message type") } diff --git a/internal/ui/restore/json_test.go b/internal/ui/restore/json_test.go index 1a749b933..06a70d5dc 100644 --- a/internal/ui/restore/json_test.go +++ b/internal/ui/restore/json_test.go @@ -53,6 +53,7 @@ func TestJSONPrintCompleteItem(t *testing.T) { {ActionFileRestored, 123, "{\"message_type\":\"verbose_status\",\"action\":\"restored\",\"item\":\"test\",\"size\":123}\n"}, {ActionFileUpdated, 123, "{\"message_type\":\"verbose_status\",\"action\":\"updated\",\"item\":\"test\",\"size\":123}\n"}, {ActionFileUnchanged, 123, "{\"message_type\":\"verbose_status\",\"action\":\"unchanged\",\"item\":\"test\",\"size\":123}\n"}, + {ActionDeleted, 0, "{\"message_type\":\"verbose_status\",\"action\":\"deleted\",\"item\":\"test\",\"size\":0}\n"}, } { term, printer := createJSONProgress() printer.CompleteItem(data.action, "test", data.size) diff --git a/internal/ui/restore/progress.go b/internal/ui/restore/progress.go index 04274b7ea..71a46e9dd 100644 --- a/internal/ui/restore/progress.go +++ b/internal/ui/restore/progress.go @@ -51,6 +51,7 @@ const ( ActionFileRestored ItemAction = "file restored" ActionFileUpdated ItemAction = "file updated" ActionFileUnchanged ItemAction = "file unchanged" + ActionDeleted ItemAction = "deleted" ) func NewProgress(printer ProgressPrinter, interval time.Duration) *Progress { @@ -126,6 +127,17 @@ func (p *Progress) AddSkippedFile(name string, size uint64) { p.printer.CompleteItem(ActionFileUnchanged, name, size) } +func (p *Progress) ReportDeletedFile(name string) { + if p == nil { + return + } + + p.m.Lock() + defer p.m.Unlock() + + p.printer.CompleteItem(ActionDeleted, name, 0) +} + func (p *Progress) Finish() { p.updater.Done() } diff --git a/internal/ui/restore/progress_test.go b/internal/ui/restore/progress_test.go index eda1b05c0..4a6304741 100644 --- a/internal/ui/restore/progress_test.go +++ b/internal/ui/restore/progress_test.go @@ -181,10 +181,12 @@ func TestProgressTypes(t *testing.T) { progress.AddFile(0) progress.AddProgress("dir", ActionDirRestored, fileSize, fileSize) progress.AddProgress("new", ActionFileRestored, 0, 0) + progress.ReportDeletedFile("del") return true }) test.Equals(t, itemTrace{ itemTraceEntry{ActionDirRestored, "dir", fileSize}, itemTraceEntry{ActionFileRestored, "new", 0}, + itemTraceEntry{ActionDeleted, "del", 0}, }, items) } diff --git a/internal/ui/restore/text.go b/internal/ui/restore/text.go index 77c2f2d15..2f0b3c01f 100644 --- a/internal/ui/restore/text.go +++ b/internal/ui/restore/text.go @@ -48,12 +48,14 @@ func (t *textPrinter) CompleteItem(messageType ItemAction, item string, size uin action = "updated" case ActionFileUnchanged: action = "unchanged" + case ActionDeleted: + action = "deleted" default: panic("unknown message type") } - if messageType == ActionDirRestored { - t.terminal.Print(fmt.Sprintf("restored %v", item)) + if messageType == ActionDirRestored || messageType == ActionDeleted { + t.terminal.Print(fmt.Sprintf("%-9v %v", action, item)) } else { t.terminal.Print(fmt.Sprintf("%-9v %v with size %v", action, item, ui.FormatBytes(size))) } diff --git a/internal/ui/restore/text_test.go b/internal/ui/restore/text_test.go index c7d173422..eddc0d1ca 100644 --- a/internal/ui/restore/text_test.go +++ b/internal/ui/restore/text_test.go @@ -65,6 +65,7 @@ func TestPrintCompleteItem(t *testing.T) { {ActionFileRestored, 123, "restored test with size 123 B"}, {ActionFileUpdated, 123, "updated test with size 123 B"}, {ActionFileUnchanged, 123, "unchanged test with size 123 B"}, + {ActionDeleted, 0, "deleted test"}, } { term, printer := createTextProgress() printer.CompleteItem(data.action, "test", data.size)