From 26be094f289e6099beb6beb3f42a4c3ca3ad951b Mon Sep 17 00:00:00 2001 From: Igor Fedorenko Date: Thu, 10 May 2018 22:56:10 -0400 Subject: [PATCH] Refactor: moved restorer to separate package Signed-off-by: Igor Fedorenko --- cmd/restic/cmd_restore.go | 3 ++- internal/{restic => restorer}/restorer.go | 27 ++++++++++--------- .../{restic => restorer}/restorer_test.go | 7 ++--- 3 files changed, 20 insertions(+), 17 deletions(-) rename internal/{restic => restorer}/restorer.go (82%) rename internal/{restic => restorer}/restorer_test.go (98%) diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 2ffb4cda8..846eb74b2 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -5,6 +5,7 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/filter" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/restorer" "github.com/spf13/cobra" ) @@ -104,7 +105,7 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error { } } - res, err := restic.NewRestorer(repo, id) + res, err := restorer.NewRestorer(repo, id) if err != nil { Exitf(2, "creating restorer failed: %v\n", err) } diff --git a/internal/restic/restorer.go b/internal/restorer/restorer.go similarity index 82% rename from internal/restic/restorer.go rename to internal/restorer/restorer.go index 8c27988f2..85e82d307 100644 --- a/internal/restic/restorer.go +++ b/internal/restorer/restorer.go @@ -1,4 +1,4 @@ -package restic +package restorer import ( "context" @@ -9,29 +9,30 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/fs" + "github.com/restic/restic/internal/restic" ) // Restorer is used to restore a snapshot to a directory. type Restorer struct { - repo Repository - sn *Snapshot + repo restic.Repository + sn *restic.Snapshot - Error func(dir string, node *Node, err error) error - SelectFilter func(item string, dstpath string, node *Node) (selectedForRestore bool, childMayBeSelected bool) + Error func(dir string, node *restic.Node, err error) error + SelectFilter func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) } -var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { return err } +var restorerAbortOnAllErrors = func(str string, node *restic.Node, err error) error { return err } // NewRestorer creates a restorer preloaded with the content from the snapshot id. -func NewRestorer(repo Repository, id ID) (*Restorer, error) { +func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) { r := &Restorer{ repo: repo, Error: restorerAbortOnAllErrors, - SelectFilter: func(string, string, *Node) (bool, bool) { return true, true }, + SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true }, } var err error - r.sn, err = LoadSnapshot(context.TODO(), repo, id) + r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id) if err != nil { return nil, err } @@ -41,7 +42,7 @@ func NewRestorer(repo Repository, id ID) (*Restorer, error) { // restoreTo restores a tree from the repo to a destination. target is the path in // the file system, location within the snapshot. -func (res *Restorer) restoreTo(ctx context.Context, target, location string, treeID ID, idx *HardlinkIndex) error { +func (res *Restorer) restoreTo(ctx context.Context, target, location string, treeID restic.ID, idx *restic.HardlinkIndex) error { debug.Log("%v %v %v", target, location, treeID) tree, err := res.repo.LoadTree(ctx, treeID) if err != nil { @@ -122,7 +123,7 @@ func (res *Restorer) restoreTo(ctx context.Context, target, location string, tre return nil } -func (res *Restorer) restoreNodeTo(ctx context.Context, node *Node, target, location string, idx *HardlinkIndex) error { +func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, target, location string, idx *restic.HardlinkIndex) error { debug.Log("%v %v %v", node.Name, target, location) err := node.CreateAt(ctx, target, res.repo, idx) @@ -163,11 +164,11 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { } } - idx := NewHardlinkIndex() + idx := restic.NewHardlinkIndex() return res.restoreTo(ctx, dst, string(filepath.Separator), *res.sn.Tree, idx) } // Snapshot returns the snapshot this restorer is configured to use. -func (res *Restorer) Snapshot() *Snapshot { +func (res *Restorer) Snapshot() *restic.Snapshot { return res.sn } diff --git a/internal/restic/restorer_test.go b/internal/restorer/restorer_test.go similarity index 98% rename from internal/restic/restorer_test.go rename to internal/restorer/restorer_test.go index 9b1758dc9..4fde75edd 100644 --- a/internal/restic/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -1,4 +1,4 @@ -package restic_test +package restorer_test import ( "bytes" @@ -13,6 +13,7 @@ import ( "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/restorer" rtest "github.com/restic/restic/internal/test" ) @@ -264,7 +265,7 @@ func TestRestorer(t *testing.T) { _, id := saveSnapshot(t, repo, test.Snapshot) t.Logf("snapshot saved as %v", id.Str()) - res, err := restic.NewRestorer(repo, id) + res, err := restorer.NewRestorer(repo, id) if err != nil { t.Fatal(err) } @@ -377,7 +378,7 @@ func TestRestorerRelative(t *testing.T) { _, id := saveSnapshot(t, repo, test.Snapshot) t.Logf("snapshot saved as %v", id.Str()) - res, err := restic.NewRestorer(repo, id) + res, err := restorer.NewRestorer(repo, id) if err != nil { t.Fatal(err) }