From 052a6a0accdf92e63ec5a47258c1ba12c946b0d0 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 18 Jun 2017 13:18:12 +0200 Subject: [PATCH] Move snapshot filter function to restic package --- src/cmds/restic/find.go | 10 +--------- src/restic/snapshot_find.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/cmds/restic/find.go b/src/cmds/restic/find.go index 9a0e9bb56..7efdc7d8c 100644 --- a/src/cmds/restic/find.go +++ b/src/cmds/restic/find.go @@ -58,15 +58,7 @@ func FindFilteredSnapshots(ctx context.Context, repo *repository.Repository, hos return } - for id := range repo.List(ctx, restic.SnapshotFile) { - sn, err := restic.LoadSnapshot(ctx, repo, id) - if err != nil { - Warnf("Ignoring %q, could not load snapshot: %v\n", id, err) - continue - } - if (host != "" && host != sn.Hostname) || !sn.HasTags(tags) || !sn.HasPaths(paths) { - continue - } + for _, sn := range restic.FindFilteredSnapshots(ctx, repo, host, tags, paths) { select { case <-ctx.Done(): return diff --git a/src/restic/snapshot_find.go b/src/restic/snapshot_find.go index fafff61cd..406bd2c1b 100644 --- a/src/restic/snapshot_find.go +++ b/src/restic/snapshot_find.go @@ -2,6 +2,8 @@ package restic import ( "context" + "fmt" + "os" "restic/errors" "time" ) @@ -48,3 +50,23 @@ func FindSnapshot(repo Repository, s string) (ID, error) { return ParseID(name) } + +// FindFilteredSnapshots yields Snapshots filtered from the list of all +// snapshots. +func FindFilteredSnapshots(ctx context.Context, repo Repository, host string, tags []string, paths []string) Snapshots { + results := make(Snapshots, 0, 20) + + for id := range repo.List(ctx, SnapshotFile) { + sn, err := LoadSnapshot(ctx, repo, id) + if err != nil { + fmt.Fprintf(os.Stderr, "could not load snapshot %v: %v\n", id.Str(), err) + continue + } + if (host != "" && host != sn.Hostname) || !sn.HasTags(tags) || !sn.HasPaths(paths) { + continue + } + + results = append(results, sn) + } + return results +}