From 02634dce7a532e63a959282d09a521b98607b9ce Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 15 Oct 2022 16:00:05 +0200 Subject: [PATCH] restic: change Find to return ids That way consumers no longer have to manually convert the returned name to an id. --- cmd/restic/cmd_debug.go | 5 +---- cmd/restic/cmd_key.go | 2 +- internal/repository/key.go | 2 +- internal/restic/backend_find.go | 16 ++++++++-------- internal/restic/backend_find_test.go | 8 ++++---- internal/restic/snapshot_find.go | 7 +------ 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index 9fcf1b6c7..4c1ffc383 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -436,10 +436,7 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, args []string) er for _, name := range args { id, err := restic.ParseID(name) if err != nil { - name, err = restic.Find(ctx, repo.Backend(), restic.PackFile, name) - if err == nil { - id, err = restic.ParseID(name) - } + id, err = restic.Find(ctx, repo.Backend(), restic.PackFile, name) if err != nil { Warnf("error: %v\n", err) continue diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index 52143bbc1..cab5a9d4d 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -236,7 +236,7 @@ func runKey(ctx context.Context, gopts GlobalOptions, args []string) error { return err } - return deleteKey(ctx, repo, id) + return deleteKey(ctx, repo, id.String()) case "passwd": lock, ctx, err := lockRepoExclusive(ctx, repo) defer unlockRepo(lock) diff --git a/internal/repository/key.go b/internal/repository/key.go index 4ce59a1f5..f0e091239 100644 --- a/internal/repository/key.go +++ b/internal/repository/key.go @@ -119,7 +119,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int, id, err := restic.Find(ctx, s.Backend(), restic.KeyFile, keyHint) if err == nil { - key, err := OpenKey(ctx, s, id, password) + key, err := OpenKey(ctx, s, id.String(), password) if err == nil { debug.Log("successfully opened hinted key %v", id) diff --git a/internal/restic/backend_find.go b/internal/restic/backend_find.go index 8ce57dd1d..7c78b3355 100644 --- a/internal/restic/backend_find.go +++ b/internal/restic/backend_find.go @@ -26,23 +26,23 @@ func (e *NoIDByPrefixError) Error() string { // Find loads the list of all files of type t and searches for names which // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. // If more than one is found, nil and ErrMultipleIDMatches is returned. -func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, error) { - match := "" +func Find(ctx context.Context, be Lister, t FileType, prefix string) (ID, error) { + match := ID{} ctx, cancel := context.WithCancel(ctx) defer cancel() err := be.List(ctx, t, func(fi FileInfo) error { // ignore filename which are not an id - _, err := ParseID(fi.Name) + id, err := ParseID(fi.Name) if err != nil { debug.Log("unable to parse %v as an ID", fi.Name) return nil } if len(fi.Name) >= len(prefix) && prefix == fi.Name[:len(prefix)] { - if match == "" { - match = fi.Name + if match.IsNull() { + match = id } else { return &MultipleIDMatchesError{prefix} } @@ -52,12 +52,12 @@ func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, er }) if err != nil { - return "", err + return ID{}, err } - if match != "" { + if !match.IsNull() { return match, nil } - return "", &NoIDByPrefixError{prefix} + return ID{}, &NoIDByPrefixError{prefix} } diff --git a/internal/restic/backend_find_test.go b/internal/restic/backend_find_test.go index bd1e6f7e6..cbd5e7f48 100644 --- a/internal/restic/backend_find_test.go +++ b/internal/restic/backend_find_test.go @@ -43,7 +43,7 @@ func TestFind(t *testing.T) { if err != nil { t.Error(err) } - expectedMatch := "20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff" + expectedMatch := TestParseID("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff") if f != expectedMatch { t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f) } @@ -52,7 +52,7 @@ func TestFind(t *testing.T) { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), "NotAPrefix") { t.Error("Expected no snapshots to be found.") } - if f != "" { + if !f.IsNull() { t.Errorf("Find should not return a match on error.") } @@ -62,7 +62,7 @@ func TestFind(t *testing.T) { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), extraLengthID) { t.Errorf("Wrong error %v for no snapshots matched", err) } - if f != "" { + if !f.IsNull() { t.Errorf("Find should not return a match on error.") } @@ -71,7 +71,7 @@ func TestFind(t *testing.T) { if _, ok := err.(*MultipleIDMatchesError); !ok { t.Errorf("Wrong error %v for multiple snapshots", err) } - if f != "" { + if !f.IsNull() { t.Errorf("Find should not return a match on error.") } } diff --git a/internal/restic/snapshot_find.go b/internal/restic/snapshot_find.go index eadb01f4b..4f8231a7f 100644 --- a/internal/restic/snapshot_find.go +++ b/internal/restic/snapshot_find.go @@ -77,12 +77,7 @@ func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s strin id, err := ParseID(s) if err != nil { // find snapshot id with prefix - name, err := Find(ctx, be, SnapshotFile, s) - if err != nil { - return nil, err - } - - id, err = ParseID(name) + id, err = Find(ctx, be, SnapshotFile, s) if err != nil { return nil, err }