From 8f26fe271c6640b55439fd453fda7e5a088ee6da Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 9 Jun 2018 23:31:31 +0200 Subject: [PATCH] ls: Use walker for ls --- cmd/restic/cmd_ls.go | 36 +++++++++++++----------------------- internal/walker/walker.go | 4 ++++ 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index ca1f7ba62..f7996b438 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -2,13 +2,12 @@ package main import ( "context" - "path" "github.com/spf13/cobra" "github.com/restic/restic/internal/errors" - "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/walker" ) var cmdLs = &cobra.Command{ @@ -46,26 +45,6 @@ func init() { flags.StringArrayVar(&lsOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot ID is given") } -func printTree(ctx context.Context, repo *repository.Repository, id *restic.ID, prefix string) error { - tree, err := repo.LoadTree(ctx, *id) - if err != nil { - return err - } - - for _, entry := range tree.Nodes { - entryPath := path.Join(prefix, entry.Name) - Printf("%s\n", formatNode(entryPath, entry, lsOptions.ListLong)) - - if entry.Type == "dir" && entry.Subtree != nil { - if err = printTree(ctx, repo, entry.Subtree, entryPath); err != nil { - return err - } - } - } - - return nil -} - func runLs(opts LsOptions, gopts GlobalOptions, args []string) error { if len(args) == 0 && opts.Host == "" && len(opts.Tags) == 0 && len(opts.Paths) == 0 { return errors.Fatal("Invalid arguments, either give one or more snapshot IDs or set filters.") @@ -85,7 +64,18 @@ func runLs(opts LsOptions, gopts GlobalOptions, args []string) error { for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) { Verbosef("snapshot %s of %v at %s):\n", sn.ID().Str(), sn.Paths, sn.Time) - if err = printTree(gopts.ctx, repo, sn.Tree, ""); err != nil { + err := walker.Walk(ctx, repo, *sn.Tree, nil, func(nodepath string, node *restic.Node, err error) (bool, error) { + if err != nil { + return false, err + } + + if node == nil { + return false, nil + } + Printf("%s\n", formatNode(nodepath, node, lsOptions.ListLong)) + return false, nil + }) + if err != nil { return err } } diff --git a/internal/walker/walker.go b/internal/walker/walker.go index df2573c57..b679cbce0 100644 --- a/internal/walker/walker.go +++ b/internal/walker/walker.go @@ -49,6 +49,10 @@ func Walk(ctx context.Context, repo TreeLoader, root restic.ID, ignoreTrees rest return err } + if ignoreTrees == nil { + ignoreTrees = restic.NewIDSet() + } + _, err = walk(ctx, repo, "/", tree, ignoreTrees, walkFn) return err }