restore: add deletions to progress output

This commit is contained in:
Michael Eischer 2024-06-29 21:29:42 +02:00
parent f4b15fdd96
commit aa8e18cf32
8 changed files with 24 additions and 3 deletions

View file

@ -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 |
+----------------------+-----------------------------------------------------------+

View file

@ -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

View file

@ -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")
}

View file

@ -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)

View file

@ -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()
}

View file

@ -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)
}

View file

@ -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)))
}

View file

@ -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)