Skip to content
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

chore: clean metadata handler for attach command #1584

Merged
merged 9 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type Renderer interface {

// AttachHandler handles metadata output for attach events.
type AttachHandler interface {
OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor)
Renderer

OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor)
}

// DiscoverHandler handles metadata output for discover events.
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type AttachHandler struct {
template string
out io.Writer
target *option.Target
path string
root ocispec.Descriptor
}

Expand All @@ -43,11 +43,11 @@ func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler {

// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, _ ocispec.Descriptor) {
ah.target = target
ah.path = target.Path
ah.root = root
}

// Render formats the metadata of attach command.
func (ah *AttachHandler) Render() error {
return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.target.Path), ah.template)
return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.path), ah.template)
}
25 changes: 12 additions & 13 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import (

// AttachHandler handles text metadata output for attach events.
type AttachHandler struct {
printer *output.Printer
target *option.Target
root ocispec.Descriptor
subject ocispec.Descriptor
printer *output.Printer
subjectRefByDigest string
root ocispec.Descriptor
}

// NewAttachHandler returns a new handler for attach events.
Expand All @@ -42,21 +41,21 @@ func NewAttachHandler(printer *output.Printer) metadata.AttachHandler {

// OnAttached implements AttachHandler.
func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor) {
ah.target = target
ah.subjectRefByDigest = target.AnnotatedReference()
if !strings.HasSuffix(target.RawReference, subject.Digest.String()) {
// use subject digest instead of tag
newTarget := *target
newTarget.RawReference = fmt.Sprintf("%s@%s", target.Path, subject.Digest)
ah.subjectRefByDigest = newTarget.AnnotatedReference()
}
qweeah marked this conversation as resolved.
Show resolved Hide resolved
qweeah marked this conversation as resolved.
Show resolved Hide resolved
ah.root = root
ah.subject = subject
}

// Render is called when the attach command is complete.
func (ah *AttachHandler) Render() error {
digest := ah.subject.Digest.String()
if !strings.HasSuffix(ah.target.RawReference, digest) {
ah.target.RawReference = fmt.Sprintf("%s@%s", ah.target.Path, ah.subject.Digest)
}
err := ah.printer.Println("Attached to", ah.target.AnnotatedReference())
err := ah.printer.Println("Attached to", ah.subjectRefByDigest)
if err != nil {
return err
}
err = ah.printer.Println("Digest:", ah.root.Digest)
return err
return ah.printer.Println("Digest:", ah.root.Digest)
}
2 changes: 0 additions & 2 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error {
if err != nil {
return err
}

metadataHandler.OnAttached(&opts.Target, root, subject)

err = metadataHandler.Render()
if err != nil {
return err
Expand Down
Loading