restorer: use options struct

This commit is contained in:
Michael Eischer 2024-05-31 11:42:25 +02:00
parent 0fcd89f892
commit 2b50c2606c
5 changed files with 24 additions and 18 deletions

View file

@ -162,7 +162,10 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
} }
progress := restoreui.NewProgress(printer, calculateProgressInterval(!gopts.Quiet, gopts.JSON)) progress := restoreui.NewProgress(printer, calculateProgressInterval(!gopts.Quiet, gopts.JSON))
res := restorer.NewRestorer(repo, sn, opts.Sparse, progress) res := restorer.NewRestorer(repo, sn, restorer.Options{
Sparse: opts.Sparse,
Progress: progress,
})
totalErrors := 0 totalErrors := 0
res.Error = func(location string, err error) error { res.Error = func(location string, err error) error {

View file

@ -20,7 +20,6 @@ type Restorer struct {
repo restic.Repository repo restic.Repository
sn *restic.Snapshot sn *restic.Snapshot
sparse bool sparse bool
progress *restoreui.Progress progress *restoreui.Progress
Error func(location string, err error) error Error func(location string, err error) error
@ -30,15 +29,19 @@ type Restorer struct {
var restorerAbortOnAllErrors = func(_ string, err error) error { return err } var restorerAbortOnAllErrors = func(_ string, err error) error { return err }
type Options struct {
Sparse bool
Progress *restoreui.Progress
}
// NewRestorer creates a restorer preloaded with the content from the snapshot id. // NewRestorer creates a restorer preloaded with the content from the snapshot id.
func NewRestorer(repo restic.Repository, sn *restic.Snapshot, sparse bool, func NewRestorer(repo restic.Repository, sn *restic.Snapshot, opts Options) *Restorer {
progress *restoreui.Progress) *Restorer {
r := &Restorer{ r := &Restorer{
repo: repo, repo: repo,
sparse: sparse, sparse: opts.Sparse,
progress: opts.Progress,
Error: restorerAbortOnAllErrors, Error: restorerAbortOnAllErrors,
SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true }, SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true },
progress: progress,
sn: sn, sn: sn,
} }

View file

@ -343,7 +343,7 @@ func TestRestorer(t *testing.T) {
sn, id := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes) sn, id := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
// make sure we're creating a new subdir of the tempdir // make sure we're creating a new subdir of the tempdir
@ -460,7 +460,7 @@ func TestRestorerRelative(t *testing.T) {
sn, id := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes) sn, id := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
cleanup := rtest.Chdir(t, tempdir) cleanup := rtest.Chdir(t, tempdir)
@ -689,7 +689,7 @@ func TestRestorerTraverseTree(t *testing.T) {
repo := repository.TestRepository(t) repo := repository.TestRepository(t)
sn, _ := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes) sn, _ := saveSnapshot(t, repo, test.Snapshot, noopGetGenericAttributes)
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = test.Select res.SelectFilter = test.Select
@ -765,7 +765,7 @@ func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
}, },
}, noopGetGenericAttributes) }, noopGetGenericAttributes)
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
switch filepath.ToSlash(item) { switch filepath.ToSlash(item) {
@ -820,7 +820,7 @@ func TestVerifyCancel(t *testing.T) {
repo := repository.TestRepository(t) repo := repository.TestRepository(t)
sn, _ := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes) sn, _ := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -862,7 +862,7 @@ func TestRestorerSparseFiles(t *testing.T) {
archiver.SnapshotOptions{}) archiver.SnapshotOptions{})
rtest.OK(t, err) rtest.OK(t, err)
res := NewRestorer(repo, sn, true, nil) res := NewRestorer(repo, sn, Options{Sparse: true})
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())

View file

@ -31,7 +31,7 @@ func TestRestorerRestoreEmptyHardlinkedFileds(t *testing.T) {
}, },
}, noopGetGenericAttributes) }, noopGetGenericAttributes)
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
return true, true return true, true
@ -99,7 +99,7 @@ func TestRestorerProgressBar(t *testing.T) {
mock := &printerMock{} mock := &printerMock{}
progress := restoreui.NewProgress(mock, 0) progress := restoreui.NewProgress(mock, 0)
res := NewRestorer(repo, sn, false, progress) res := NewRestorer(repo, sn, Options{Progress: progress})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
return true, true return true, true
} }

View file

@ -269,7 +269,7 @@ func setup(t *testing.T, nodesMap map[string]Node) *Restorer {
sn, _ := saveSnapshot(t, repo, Snapshot{ sn, _ := saveSnapshot(t, repo, Snapshot{
Nodes: nodesMap, Nodes: nodesMap,
}, getFileAttributes) }, getFileAttributes)
res := NewRestorer(repo, sn, false, nil) res := NewRestorer(repo, sn, Options{})
return res return res
} }