From a81f0432e9e5188b88d45a298dd802200fe0058d Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 3 Oct 2022 14:16:33 +0200 Subject: [PATCH] restic: Add unified method to resolve a single snapshot --- cmd/restic/cmd_dump.go | 15 +++------------ cmd/restic/cmd_restore.go | 15 +++------------ internal/restic/snapshot_find.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index fdb6b0b91..4f2827b02 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -139,18 +139,9 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args [] } } - var id restic.ID - - if snapshotIDString == "latest" { - id, err = restic.FindLatestSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil) - if err != nil { - Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts) - } - } else { - id, err = restic.FindSnapshot(ctx, repo.Backend(), snapshotIDString) - if err != nil { - Exitf(1, "invalid id %q: %v", snapshotIDString, err) - } + id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, snapshotIDString) + if err != nil { + Exitf(1, "failed to find snapshot: %v", err) } sn, err := restic.LoadSnapshot(ctx, repo, id) diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 1254174d7..33a12b441 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -131,18 +131,9 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, a } } - var id restic.ID - - if snapshotIDString == "latest" { - id, err = restic.FindLatestSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil) - if err != nil { - Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts) - } - } else { - id, err = restic.FindSnapshot(ctx, repo.Backend(), snapshotIDString) - if err != nil { - Exitf(1, "invalid id %q: %v", snapshotIDString, err) - } + id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Hosts, opts.Tags, opts.Paths, snapshotIDString) + if err != nil { + Exitf(1, "failed to find snapshot %q: %v", snapshotIDString, err) } err = repo.LoadIndex(ctx) diff --git a/internal/restic/snapshot_find.go b/internal/restic/snapshot_find.go index f472ccfaa..088b920ec 100644 --- a/internal/restic/snapshot_find.go +++ b/internal/restic/snapshot_find.go @@ -88,6 +88,22 @@ func FindSnapshot(ctx context.Context, be Lister, s string) (ID, error) { return ParseID(name) } +func FindFilteredSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, snapshotID string) (ID, error) { + if snapshotID == "latest" { + id, err := FindLatestSnapshot(ctx, be, loader, paths, tags, hosts, nil) + if err == ErrNoSnapshotFound { + err = errors.Errorf("no snapshot matched given filter (Paths:%v Tags:%v Hosts:%v)", paths, tags, hosts) + } + return id, err + } else { + id, err := FindSnapshot(ctx, be, snapshotID) + if err != nil { + return ID{}, err + } + return id, err + } +} + type SnapshotFindCb func(string, *Snapshot, error) error // FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.