diff --git a/cmd/oras/root/push.go b/cmd/oras/root/push.go index 1fd234de5..0423df717 100644 --- a/cmd/oras/root/push.go +++ b/cmd/oras/root/push.go @@ -141,8 +141,6 @@ func runPush(ctx context.Context, opts pushOptions) error { return err } defer store.Close() - memStore := memory.New() - union := contentutil.MultiReadOnlyTarget(memStore, store) if opts.manifestConfigRef != "" { path, cfgMediaType, err := fileref.Parse(opts.manifestConfigRef, oras.MediaTypeUnknownConfig) if err != nil { @@ -163,12 +161,13 @@ func runPush(ctx context.Context, opts pushOptions) error { if err != nil { return err } + memoryStore := memory.New() pack := func() (ocispec.Descriptor, error) { - root, err := oras.Pack(ctx, memStore, opts.artifactType, descs, packOpts) + root, err := oras.Pack(ctx, memoryStore, opts.artifactType, descs, packOpts) if err != nil { return ocispec.Descriptor{}, err } - if err = memStore.Tag(ctx, root, root.Digest.String()); err != nil { + if err = memoryStore.Tag(ctx, root, root.Digest.String()); err != nil { return ocispec.Descriptor{}, err } return root, nil @@ -181,6 +180,7 @@ func runPush(ctx context.Context, opts pushOptions) error { } copyOptions := oras.DefaultCopyOptions copyOptions.Concurrency = opts.concurrency + union := contentutil.MultiReadOnlyTarget(memoryStore, store) updateDisplayOption(©Options.CopyGraphOptions, union, opts.Verbose) copy := func(root ocispec.Descriptor) error { if tag := opts.Reference; tag == "" { @@ -199,7 +199,7 @@ func runPush(ctx context.Context, opts pushOptions) error { fmt.Println("Pushed", opts.AnnotatedReference()) if len(opts.extraRefs) != 0 { - contentBytes, err := content.FetchAll(ctx, memStore, root) + contentBytes, err := content.FetchAll(ctx, memoryStore, root) if err != nil { return err } @@ -213,7 +213,7 @@ func runPush(ctx context.Context, opts pushOptions) error { fmt.Println("Digest:", root.Digest) // Export manifest - return opts.ExportManifest(ctx, memStore, root) + return opts.ExportManifest(ctx, memoryStore, root) } func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, verbose bool) { diff --git a/internal/contentutil/target.go b/internal/contentutil/target.go index ae7659181..cb6cc3d81 100644 --- a/internal/contentutil/target.go +++ b/internal/contentutil/target.go @@ -41,33 +41,20 @@ func (m *multiReadOnlyTarget) Fetch(ctx context.Context, target ocispec.Descript lastErr := errdef.ErrNotFound for _, c := range m.targets { rc, err := c.Fetch(ctx, target) - if err != nil { - if errors.Is(err, errdef.ErrNotFound) { - lastErr = err - continue - } + if err == nil { + return rc, nil + } + if !errors.Is(err, errdef.ErrNotFound) { return nil, err } - return rc, nil + lastErr = err } return nil, lastErr } // Exists returns true if the described content exists. func (m *multiReadOnlyTarget) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) { - lastErr := errdef.ErrNotFound - for _, c := range m.targets { - exists, err := c.Exists(ctx, target) - if err != nil { - if errors.Is(err, errdef.ErrNotFound) { - lastErr = err - continue - } - return false, err - } - return exists, nil - } - return false, lastErr + return false, errors.New("MultiReadOnlyTarget.Exists() not implemented") } // Resolve resolves the content from cache first, then from the provider. @@ -75,14 +62,13 @@ func (m *multiReadOnlyTarget) Resolve(ctx context.Context, ref string) (ocispec. lastErr := errdef.ErrNotFound for _, c := range m.targets { desc, err := c.Resolve(ctx, ref) - if err != nil { - if errors.Is(err, errdef.ErrNotFound) { - lastErr = err - continue - } + if err == nil { + return desc, nil + } + if !errors.Is(err, errdef.ErrNotFound) { return ocispec.Descriptor{}, err } - return desc, nil + lastErr = err } return ocispec.Descriptor{}, lastErr }