From 4becd164842a564da7c64971bac3834dd7bca494 Mon Sep 17 00:00:00 2001 From: "Kyle M. Tarplee" Date: Fri, 29 Dec 2023 06:28:53 -0500 Subject: [PATCH] fix: added a custom error type for getContent's use Signed-off-by: Kyle M. Tarplee --- copy.go | 2 +- copy_test.go | 5 +++-- registry/remote/repository.go | 2 +- registry/remote/repository_test.go | 4 ++-- registry/repository.go | 5 +++++ 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/copy.go b/copy.go index 7d80e55e..a224e2d0 100644 --- a/copy.go +++ b/copy.go @@ -144,7 +144,7 @@ func (opts *CopyGraphOptions) WithMount(sourceRepository string, mounter registr // To avoid needing a content.Fetcher as an input argument we simply fall back to the default behavior // as if getContent was nil - return nil, errdef.ErrUnsupported + return nil, registry.UseSourceRepository } // Mount or copy diff --git a/copy_test.go b/copy_test.go index 4efffccc..4c54bbcd 100644 --- a/copy_test.go +++ b/copy_test.go @@ -37,6 +37,7 @@ import ( "oras.land/oras-go/v2/internal/cas" "oras.land/oras-go/v2/internal/docker" "oras.land/oras-go/v2/internal/spec" + "oras.land/oras-go/v2/registry" ) // storageTracker tracks storage API counts. @@ -1549,8 +1550,8 @@ func TestCopyGraph_WithOptions(t *testing.T) { } _, err := getContent() - if !errors.Is(err, errdef.ErrUnsupported) { - t.Fatalf("Expected error %v", errdef.ErrUnsupported) + if !errors.Is(err, registry.UseSourceRepository) { + t.Fatalf("Expected error %v", registry.UseSourceRepository) } rc, err := src.Fetch(ctx, desc) if err != nil { diff --git a/registry/remote/repository.go b/registry/remote/repository.go index 80b57eb1..4fd4cbae 100644 --- a/registry/remote/repository.go +++ b/registry/remote/repository.go @@ -806,7 +806,7 @@ func (s *blobStore) Mount(ctx context.Context, desc ocispec.Descriptor, fromRepo var r io.ReadCloser if getContent != nil { r, err = getContent() - if errors.Is(err, errdef.ErrUnsupported) { + if errors.Is(err, registry.UseSourceRepository) { // getContent can return a ErrUnsupported to fallback to the default copy operation r, err = s.sibling(fromRepo).Fetch(ctx, desc) } diff --git a/registry/remote/repository_test.go b/registry/remote/repository_test.go index 4578a840..f4d3ff5e 100644 --- a/registry/remote/repository_test.go +++ b/registry/remote/repository_test.go @@ -436,11 +436,11 @@ func TestRepository_Mount_Fallback(t *testing.T) { } }) - t.Run("getContent is ErrUnsupported", func(t *testing.T) { + t.Run("getContent is UseSourceRepository", func(t *testing.T) { sequence = "" err = repo.Mount(ctx, blobDesc, "test", func() (io.ReadCloser, error) { - return nil, errdef.ErrUnsupported + return nil, registry.UseSourceRepository }) if err != nil { t.Fatalf("Repository.Push() error = %v", err) diff --git a/registry/repository.go b/registry/repository.go index 394a9aa8..29b22fc2 100644 --- a/registry/repository.go +++ b/registry/repository.go @@ -18,6 +18,7 @@ package registry import ( "context" "encoding/json" + "errors" "fmt" "io" @@ -128,6 +129,10 @@ type Mounter interface { ) error } +// UseSourceRepository signals to Mount implementations that the content from the source repository should be used. This may be returned from the getContent function of the Mounter.Mount method. +// This is useful to avoid having to know that source repository in the definition of getContent. +var UseSourceRepository = errors.New("use source repository") + // Tags lists the tags available in the repository. func Tags(ctx context.Context, repo TagLister) ([]string, error) { var res []string