1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-24 16:54:00 +00:00

Refactor: moved restorer to separate package

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
This commit is contained in:
Igor Fedorenko 2018-05-10 22:56:10 -04:00
parent e4c0d77bdd
commit 26be094f28
3 changed files with 20 additions and 17 deletions

View file

@ -5,6 +5,7 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/filter" "github.com/restic/restic/internal/filter"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/restorer"
"github.com/spf13/cobra" "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 { if err != nil {
Exitf(2, "creating restorer failed: %v\n", err) Exitf(2, "creating restorer failed: %v\n", err)
} }

View file

@ -1,4 +1,4 @@
package restic package restorer
import ( import (
"context" "context"
@ -9,29 +9,30 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic"
) )
// Restorer is used to restore a snapshot to a directory. // Restorer is used to restore a snapshot to a directory.
type Restorer struct { type Restorer struct {
repo Repository repo restic.Repository
sn *Snapshot sn *restic.Snapshot
Error func(dir string, node *Node, err error) error Error func(dir string, node *restic.Node, err error) error
SelectFilter func(item string, dstpath string, node *Node) (selectedForRestore bool, childMayBeSelected bool) 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. // 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{ r := &Restorer{
repo: repo, Error: restorerAbortOnAllErrors, 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 var err error
r.sn, err = LoadSnapshot(context.TODO(), repo, id) r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id)
if err != nil { if err != nil {
return nil, err 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 // restoreTo restores a tree from the repo to a destination. target is the path in
// the file system, location within the snapshot. // 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) debug.Log("%v %v %v", target, location, treeID)
tree, err := res.repo.LoadTree(ctx, treeID) tree, err := res.repo.LoadTree(ctx, treeID)
if err != nil { if err != nil {
@ -122,7 +123,7 @@ func (res *Restorer) restoreTo(ctx context.Context, target, location string, tre
return nil 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) debug.Log("%v %v %v", node.Name, target, location)
err := node.CreateAt(ctx, target, res.repo, idx) 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) return res.restoreTo(ctx, dst, string(filepath.Separator), *res.sn.Tree, idx)
} }
// Snapshot returns the snapshot this restorer is configured to use. // Snapshot returns the snapshot this restorer is configured to use.
func (res *Restorer) Snapshot() *Snapshot { func (res *Restorer) Snapshot() *restic.Snapshot {
return res.sn return res.sn
} }

View file

@ -1,4 +1,4 @@
package restic_test package restorer_test
import ( import (
"bytes" "bytes"
@ -13,6 +13,7 @@ import (
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/restorer"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
) )
@ -264,7 +265,7 @@ func TestRestorer(t *testing.T) {
_, id := saveSnapshot(t, repo, test.Snapshot) _, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res, err := restic.NewRestorer(repo, id) res, err := restorer.NewRestorer(repo, id)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -377,7 +378,7 @@ func TestRestorerRelative(t *testing.T) {
_, id := saveSnapshot(t, repo, test.Snapshot) _, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res, err := restic.NewRestorer(repo, id) res, err := restorer.NewRestorer(repo, id)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }