Skip to content

refactor: add resolve metadata handler #1631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ func NewBlobPushHandler(printer *output.Printer, outputDescriptor bool, pretty b
}
return status.NewTextBlobPushHandler(printer, desc), text.NewBlobPushHandler(printer, desc)
}

// NewResolveHandler returns a resolve metadata handler.
func NewResolveHandler(printer *output.Printer, fullRef bool, path string) metadata.ResolveHandler {
return text.NewResolveHandler(printer, fullRef, path)
}
5 changes: 5 additions & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ type BlobPushHandler interface {

OnBlobPushed(target *option.Target) error
}

// ResolveHandler handles metadata output for resolve events.
type ResolveHandler interface {
OnResolved(desc ocispec.Descriptor) error
}
46 changes: 46 additions & 0 deletions cmd/oras/internal/display/metadata/text/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package text

import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/output"
)

// ResolveHandler handles text metadata output for resolve events.
type ResolveHandler struct {
printer *output.Printer
fullRef bool
path string
}

// NewResolveHandler returns a new handler for resolve events.
func NewResolveHandler(printer *output.Printer, fullRef bool, path string) metadata.ResolveHandler {
return &ResolveHandler{
printer: printer,
fullRef: fullRef,
path: path,
}
}

// OnResolved implements metadata.ResolveHandler.
func (h *ResolveHandler) OnResolved(desc ocispec.Descriptor) error {
if h.fullRef {
return h.printer.Printf("%s@%s\n", h.path, desc.Digest)
}
return h.printer.Println(desc.Digest.String())
}
12 changes: 3 additions & 9 deletions cmd/oras/root/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
)
Expand Down Expand Up @@ -70,19 +71,12 @@ func runResolve(cmd *cobra.Command, opts *resolveOptions) error {
if err := opts.EnsureReferenceNotEmpty(cmd, true); err != nil {
return err
}
metadataHandler := display.NewResolveHandler(opts.Printer, opts.fullRef, opts.Path)
resolveOpts := oras.DefaultResolveOptions
resolveOpts.TargetPlatform = opts.Platform.Platform
desc, err := oras.Resolve(ctx, repo, opts.Reference, resolveOpts)

if err != nil {
return fmt.Errorf("failed to resolve digest: %w", err)
}

if opts.fullRef {
_ = opts.Printer.Printf("%s@%s\n", opts.Path, desc.Digest)
} else {
_ = opts.Printer.Println(desc.Digest.String())
}

return nil
return metadataHandler.OnResolved(desc)
}
Loading