Ensure consistent naming for <snapshot>:<subfolder> syntax

This commit is contained in:
Michael Eischer 2023-07-16 16:50:54 +02:00
parent 184b7616ba
commit 8154f6a77a
9 changed files with 43 additions and 43 deletions

View file

@ -13,7 +13,7 @@ import (
)
var cmdCat = &cobra.Command{
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:path]",
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:subfolder]",
Short: "Print internal objects to stdout",
Long: `
The "cat" command is used to print internal objects to stdout.
@ -166,7 +166,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string) error {
return errors.Fatal("blob not found")
case "tree":
sn, subpath, err := restic.FindSnapshot(ctx, repo.Backend(), repo, args[1])
sn, subfolder, err := restic.FindSnapshot(ctx, repo.Backend(), repo, args[1])
if err != nil {
return errors.Fatalf("could not find snapshot: %v\n", err)
}
@ -176,7 +176,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string) error {
return err
}
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subpath)
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
if err != nil {
return err
}

View file

@ -55,11 +55,11 @@ func init() {
}
func loadSnapshot(ctx context.Context, be restic.Lister, repo restic.Repository, desc string) (*restic.Snapshot, string, error) {
sn, subpath, err := restic.FindSnapshot(ctx, be, repo, desc)
sn, subfolder, err := restic.FindSnapshot(ctx, be, repo, desc)
if err != nil {
return nil, "", errors.Fatal(err.Error())
}
return sn, subpath, err
return sn, subfolder, err
}
// Comparer collects all things needed to compare two snapshots.
@ -346,12 +346,12 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
if err != nil {
return err
}
sn1, subpath1, err := loadSnapshot(ctx, be, repo, args[0])
sn1, subfolder1, err := loadSnapshot(ctx, be, repo, args[0])
if err != nil {
return err
}
sn2, subpath2, err := loadSnapshot(ctx, be, repo, args[1])
sn2, subfolder2, err := loadSnapshot(ctx, be, repo, args[1])
if err != nil {
return err
}
@ -372,12 +372,12 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
return errors.Errorf("snapshot %v has nil tree", sn2.ID().Str())
}
sn1.Tree, err = restic.FindTreeDirectory(ctx, repo, sn1.Tree, subpath1)
sn1.Tree, err = restic.FindTreeDirectory(ctx, repo, sn1.Tree, subfolder1)
if err != nil {
return err
}
sn2.Tree, err = restic.FindTreeDirectory(ctx, repo, sn2.Tree, subpath2)
sn2.Tree, err = restic.FindTreeDirectory(ctx, repo, sn2.Tree, subfolder2)
if err != nil {
return err
}

View file

@ -139,7 +139,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
}
}
sn, subpath, err := (&restic.SnapshotFilter{
sn, subfolder, err := (&restic.SnapshotFilter{
Hosts: opts.Hosts,
Paths: opts.Paths,
Tags: opts.Tags,
@ -153,7 +153,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
return err
}
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subpath)
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
if err != nil {
return err
}

View file

@ -212,7 +212,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
}
}
sn, subpath, err := (&restic.SnapshotFilter{
sn, subfolder, err := (&restic.SnapshotFilter{
Hosts: opts.Hosts,
Paths: opts.Paths,
Tags: opts.Tags,
@ -221,7 +221,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
return err
}
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subpath)
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
if err != nil {
return err
}

View file

@ -161,7 +161,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
}
}
sn, subpath, err := (&restic.SnapshotFilter{
sn, subfolder, err := (&restic.SnapshotFilter{
Hosts: opts.Hosts,
Paths: opts.Paths,
Tags: opts.Tags,
@ -175,7 +175,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
return err
}
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subpath)
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
if err != nil {
return err
}

View file

@ -83,15 +83,15 @@ func (f *SnapshotFilter) findLatest(ctx context.Context, be Lister, loader Loade
return latest, nil
}
func splitSnapshotID(s string) (id, subpath string) {
id, subpath, _ = strings.Cut(s, ":")
func splitSnapshotID(s string) (id, subfolder string) {
id, subfolder, _ = strings.Cut(s, ":")
return
}
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
// the string as closely as possible.
func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s string) (*Snapshot, string, error) {
s, subpath := splitSnapshotID(s)
s, subfolder := splitSnapshotID(s)
// no need to list snapshots if `s` is already a full id
id, err := ParseID(s)
@ -103,27 +103,27 @@ func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s strin
}
}
sn, err := LoadSnapshot(ctx, loader, id)
return sn, subpath, err
return sn, subfolder, err
}
// FindLatest returns either the latest of a filtered list of all snapshots
// or a snapshot specified by `snapshotID`.
func (f *SnapshotFilter) FindLatest(ctx context.Context, be Lister, loader LoaderUnpacked, snapshotID string) (*Snapshot, string, error) {
id, subpath := splitSnapshotID(snapshotID)
id, subfolder := splitSnapshotID(snapshotID)
if id == "latest" {
sn, err := f.findLatest(ctx, be, loader)
if err == ErrNoSnapshotFound {
err = fmt.Errorf("snapshot filter (Paths:%v Tags:%v Hosts:%v): %w",
f.Paths, f.Tags, f.Hosts, err)
}
return sn, subpath, err
return sn, subfolder, err
}
return FindSnapshot(ctx, be, loader, snapshotID)
}
type SnapshotFindCb func(string, *Snapshot, error) error
var ErrInvalidSnapshotSyntax = errors.New("snapshot:path syntax not allowed")
var ErrInvalidSnapshotSyntax = errors.New("<snapshot>:<subfolder> syntax not allowed")
// FindAll yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.
func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUnpacked, snapshotIDs []string, fn SnapshotFindCb) error {
@ -153,9 +153,9 @@ func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUn
} else if strings.HasPrefix(s, "latest:") {
err = ErrInvalidSnapshotSyntax
} else {
var subpath string
sn, subpath, err = FindSnapshot(ctx, be, loader, s)
if err == nil && subpath != "" {
var subfolder string
sn, subfolder, err = FindSnapshot(ctx, be, loader, s)
if err == nil && subfolder != "" {
err = ErrInvalidSnapshotSyntax
} else if err == nil {
if ids.Has(*sn.ID()) {

View file

@ -52,23 +52,23 @@ func TestFindLatestWithSubpath(t *testing.T) {
for _, exp := range []struct {
query string
subpath string
subfolder string
}{
{"latest", ""},
{"latest:subpath", "subpath"},
{"latest:subfolder", "subfolder"},
{desiredSnapshot.ID().Str(), ""},
{desiredSnapshot.ID().Str() + ":subpath", "subpath"},
{desiredSnapshot.ID().Str() + ":subfolder", "subfolder"},
{desiredSnapshot.ID().String(), ""},
{desiredSnapshot.ID().String() + ":subpath", "subpath"},
{desiredSnapshot.ID().String() + ":subfolder", "subfolder"},
} {
t.Run("", func(t *testing.T) {
sn, subpath, err := (&restic.SnapshotFilter{}).FindLatest(context.TODO(), repo.Backend(), repo, exp.query)
sn, subfolder, err := (&restic.SnapshotFilter{}).FindLatest(context.TODO(), repo.Backend(), repo, exp.query)
if err != nil {
t.Fatalf("FindLatest returned error: %v", err)
}
test.Assert(t, *sn.ID() == *desiredSnapshot.ID(), "FindLatest returned wrong snapshot ID: %v", *sn.ID())
test.Assert(t, subpath == exp.subpath, "FindLatest returned wrong path in snapshot: %v", subpath)
test.Assert(t, subfolder == exp.subfolder, "FindLatest returned wrong path in snapshot: %v", subfolder)
})
}
}
@ -79,7 +79,7 @@ func TestFindAllSubpathError(t *testing.T) {
count := 0
test.OK(t, (&restic.SnapshotFilter{}).FindAll(context.TODO(), repo.Backend(), repo,
[]string{"latest:subpath", desiredSnapshot.ID().Str() + ":subpath"},
[]string{"latest:subfolder", desiredSnapshot.ID().Str() + ":subfolder"},
func(id string, sn *restic.Snapshot, err error) error {
if err == restic.ErrInvalidSnapshotSyntax {
count++
@ -87,5 +87,5 @@ func TestFindAllSubpathError(t *testing.T) {
}
return err
}))
test.Assert(t, count == 2, "unexpected number of subpath errors: %v, wanted %v", count, 2)
test.Assert(t, count == 2, "unexpected number of subfolder errors: %v, wanted %v", count, 2)
}

View file

@ -193,23 +193,23 @@ func FindTreeDirectory(ctx context.Context, repo BlobLoader, id *ID, dir string)
}
dirs := strings.Split(path.Clean(dir), "/")
subpath := ""
subfolder := ""
for _, name := range dirs {
if name == "" || name == "." {
continue
}
subpath = path.Join(subpath, name)
subfolder = path.Join(subfolder, name)
tree, err := LoadTree(ctx, repo, *id)
if err != nil {
return nil, fmt.Errorf("path %s: %w", subpath, err)
return nil, fmt.Errorf("path %s: %w", subfolder, err)
}
node := tree.Find(name)
if node == nil {
return nil, fmt.Errorf("path %s: not found", subpath)
return nil, fmt.Errorf("path %s: not found", subfolder)
}
if node.Type != "dir" || node.Subtree == nil {
return nil, fmt.Errorf("path %s: not a directory", subpath)
return nil, fmt.Errorf("path %s: not a directory", subfolder)
}
id = node.Subtree
}

View file

@ -216,7 +216,7 @@ func TestFindTreeDirectory(t *testing.T) {
sn := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:08"), 3)
for _, exp := range []struct {
subpath string
subfolder string
id restic.ID
err error
}{
@ -231,7 +231,7 @@ func TestFindTreeDirectory(t *testing.T) {
{"dir-21/dir-24", restic.TestParseID("74626b3fb2bd4b3e572b81a4059b3e912bcf2a8f69fecd9c187613b7173f13b1"), nil},
} {
t.Run("", func(t *testing.T) {
id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subpath)
id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subfolder)
if exp.err == nil {
rtest.OK(t, err)
rtest.Assert(t, exp.id == *id, "unexpected id, expected %v, got %v", exp.id, id)