From ae978d60ccd8935e8aafb96310ba610ea19b03d4 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 29 Jun 2024 21:24:45 +0200 Subject: [PATCH] restore: pass action enum to restore progress --- internal/restorer/filerestorer.go | 8 ++++++-- internal/restorer/restorer.go | 12 ++++++++---- internal/ui/restore/progress.go | 8 +------- internal/ui/restore/progress_test.go | 26 +++++++++++++------------- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/internal/restorer/filerestorer.go b/internal/restorer/filerestorer.go index 4f9ed930e..fd5b3c5db 100644 --- a/internal/restorer/filerestorer.go +++ b/internal/restorer/filerestorer.go @@ -215,7 +215,7 @@ func (r *fileRestorer) restoreEmptyFileAt(location string) error { return err } - r.progress.AddProgress(location, false, true, 0, 0) + r.progress.AddProgress(location, restore.ActionFileRestored, 0, 0) return nil } @@ -337,7 +337,11 @@ func (r *fileRestorer) downloadBlobs(ctx context.Context, packID restic.ID, createSize = file.size } writeErr := r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize, file.sparse) - r.progress.AddProgress(file.location, false, file.state == nil, uint64(len(blobData)), uint64(file.size)) + action := restore.ActionFileUpdated + if file.state == nil { + action = restore.ActionFileRestored + } + r.progress.AddProgress(file.location, action, uint64(len(blobData)), uint64(file.size)) return writeErr } err := r.sanitizeError(file, writeToFile()) diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index 4ecd762b4..650ad0731 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -234,7 +234,7 @@ func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, targe } } - res.opts.Progress.AddProgress(location, false, true, 0, 0) + res.opts.Progress.AddProgress(location, restoreui.ActionFileRestored, 0, 0) return res.restoreNodeMetadataTo(node, target, location) } @@ -261,7 +261,7 @@ func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location } } - res.opts.Progress.AddProgress(location, false, true, 0, 0) + res.opts.Progress.AddProgress(location, restoreui.ActionFileRestored, 0, 0) // TODO investigate if hardlinks have separate metadata on any supported system return res.restoreNodeMetadataTo(node, path, location) } @@ -343,8 +343,12 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { if !res.opts.DryRun { filerestorer.addFile(location, node.Content, int64(node.Size), matches) } else { + action := restoreui.ActionFileUpdated + if matches == nil { + action = restoreui.ActionFileRestored + } // immediately mark as completed - res.opts.Progress.AddProgress(location, false, matches == nil, node.Size, node.Size) + res.opts.Progress.AddProgress(location, action, node.Size, node.Size) } } res.trackFile(location, updateMetadataOnly) @@ -393,7 +397,7 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { leaveDir: func(node *restic.Node, target, location string) error { err := res.restoreNodeMetadataTo(node, target, location) if err == nil { - res.opts.Progress.AddProgress(location, false, true, 0, 0) + res.opts.Progress.AddProgress(location, restoreui.ActionDirRestored, 0, 0) } return err }, diff --git a/internal/ui/restore/progress.go b/internal/ui/restore/progress.go index 7b12deb8d..04274b7ea 100644 --- a/internal/ui/restore/progress.go +++ b/internal/ui/restore/progress.go @@ -88,7 +88,7 @@ func (p *Progress) AddFile(size uint64) { } // AddProgress accumulates the number of bytes written for a file -func (p *Progress) AddProgress(name string, isDir bool, isNew bool, bytesWrittenPortion uint64, bytesTotal uint64) { +func (p *Progress) AddProgress(name string, action ItemAction, bytesWrittenPortion uint64, bytesTotal uint64) { if p == nil { return } @@ -108,12 +108,6 @@ func (p *Progress) AddProgress(name string, isDir bool, isNew bool, bytesWritten delete(p.progressInfoMap, name) p.s.FilesFinished++ - action := ActionFileUpdated - if isDir { - action = ActionDirRestored - } else if isNew { - action = ActionFileRestored - } p.printer.CompleteItem(action, name, bytesTotal) } } diff --git a/internal/ui/restore/progress_test.go b/internal/ui/restore/progress_test.go index 9d795a313..eda1b05c0 100644 --- a/internal/ui/restore/progress_test.go +++ b/internal/ui/restore/progress_test.go @@ -81,7 +81,7 @@ func TestFirstProgressOnAFile(t *testing.T) { result, items := testProgress(func(progress *Progress) bool { progress.AddFile(expectedBytesTotal) - progress.AddProgress("test", false, false, expectedBytesWritten, expectedBytesTotal) + progress.AddProgress("test", ActionFileUpdated, expectedBytesWritten, expectedBytesTotal) return false }) test.Equals(t, printerTrace{ @@ -95,9 +95,9 @@ func TestLastProgressOnAFile(t *testing.T) { result, items := testProgress(func(progress *Progress) bool { progress.AddFile(fileSize) - progress.AddProgress("test", false, false, 30, fileSize) - progress.AddProgress("test", false, false, 35, fileSize) - progress.AddProgress("test", false, false, 35, fileSize) + progress.AddProgress("test", ActionFileUpdated, 30, fileSize) + progress.AddProgress("test", ActionFileUpdated, 35, fileSize) + progress.AddProgress("test", ActionFileUpdated, 35, fileSize) return false }) test.Equals(t, printerTrace{ @@ -114,9 +114,9 @@ func TestLastProgressOnLastFile(t *testing.T) { result, items := testProgress(func(progress *Progress) bool { progress.AddFile(fileSize) progress.AddFile(50) - progress.AddProgress("test1", false, false, 50, 50) - progress.AddProgress("test2", false, false, 50, fileSize) - progress.AddProgress("test2", false, false, 50, fileSize) + progress.AddProgress("test1", ActionFileUpdated, 50, 50) + progress.AddProgress("test2", ActionFileUpdated, 50, fileSize) + progress.AddProgress("test2", ActionFileUpdated, 50, fileSize) return false }) test.Equals(t, printerTrace{ @@ -134,8 +134,8 @@ func TestSummaryOnSuccess(t *testing.T) { result, _ := testProgress(func(progress *Progress) bool { progress.AddFile(fileSize) progress.AddFile(50) - progress.AddProgress("test1", false, false, 50, 50) - progress.AddProgress("test2", false, false, fileSize, fileSize) + progress.AddProgress("test1", ActionFileUpdated, 50, 50) + progress.AddProgress("test2", ActionFileUpdated, fileSize, fileSize) return true }) test.Equals(t, printerTrace{ @@ -149,8 +149,8 @@ func TestSummaryOnErrors(t *testing.T) { result, _ := testProgress(func(progress *Progress) bool { progress.AddFile(fileSize) progress.AddFile(50) - progress.AddProgress("test1", false, false, 50, 50) - progress.AddProgress("test2", false, false, fileSize/2, fileSize) + progress.AddProgress("test1", ActionFileUpdated, 50, 50) + progress.AddProgress("test2", ActionFileUpdated, fileSize/2, fileSize) return true }) test.Equals(t, printerTrace{ @@ -179,8 +179,8 @@ func TestProgressTypes(t *testing.T) { _, items := testProgress(func(progress *Progress) bool { progress.AddFile(fileSize) progress.AddFile(0) - progress.AddProgress("dir", true, false, fileSize, fileSize) - progress.AddProgress("new", false, true, 0, 0) + progress.AddProgress("dir", ActionDirRestored, fileSize, fileSize) + progress.AddProgress("new", ActionFileRestored, 0, 0) return true }) test.Equals(t, itemTrace{