Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Jul 4, 2023
1 parent 0ac73b9 commit cd68f66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
12 changes: 6 additions & 6 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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(&copyOptions.CopyGraphOptions, union, opts.Verbose)
copy := func(root ocispec.Descriptor) error {
if tag := opts.Reference; tag == "" {
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
36 changes: 11 additions & 25 deletions internal/contentutil/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,34 @@ 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.
func (m *multiReadOnlyTarget) Resolve(ctx context.Context, ref string) (ocispec.Descriptor, error) {
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
}

0 comments on commit cd68f66

Please sign in to comment.