Skip to content

Commit

Permalink
chore: clean metadata handler for attach command (#1584)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Dec 13, 2024
1 parent 53247dd commit 6fe0395
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
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
10 changes: 5 additions & 5 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (

// AttachHandler handles json metadata output for attach events.
type AttachHandler struct {
out io.Writer
target *option.Target
root ocispec.Descriptor
out io.Writer
path string
root ocispec.Descriptor
}

// NewAttachHandler creates a new handler for attach events.
Expand All @@ -41,11 +41,11 @@ func NewAttachHandler(out io.Writer) 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 is called when the attach command is completed.
func (ah *AttachHandler) Render() error {
return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.target.Path))
return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.path))
}
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)
}
26 changes: 13 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,22 @@ 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.root = root
ah.subject = subject
if strings.HasSuffix(target.RawReference, subject.Digest.String()) {
ah.subjectRefByDigest = target.AnnotatedReference()
} else {
// use subject digest instead of tag
newTarget := *target
newTarget.RawReference = fmt.Sprintf("%s@%s", target.Path, subject.Digest)
ah.subjectRefByDigest = newTarget.AnnotatedReference()
}
}

// 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
21 changes: 15 additions & 6 deletions test/e2e/suite/command/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,32 @@ var _ = Describe("ORAS beginners:", func() {
var _ = Describe("1.1 registry users:", func() {
When("running attach command", func() {
It("should attach a file to a subject and output status", func() {
testRepo := attachTestRepo("simple")
testRepo := attachTestRepo("attach-tag")
CopyZOTRepo(ImageRepo, testRepo)
subjectRef := RegistryRef(ZOTHost, testRepo, foobar.Tag)
ORAS("attach", "--artifact-type", "test/attach", subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia)).
WithWorkDir(PrepareTempFiles()).
MatchKeyWords(fmt.Sprintf("Attached to [registry] %s", RegistryRef(ZOTHost, testRepo, foobar.Digest))).
MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec()
})

It("should attach a file to a subject and output status", func() {
testRepo := attachTestRepo("attach-digest")
CopyZOTRepo(ImageRepo, testRepo)
subjectRef := RegistryRef(ZOTHost, testRepo, foobar.Digest)
ORAS("attach", "--artifact-type", "test/attach", subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia)).
WithWorkDir(PrepareTempFiles()).
MatchKeyWords(fmt.Sprintf("Attached to [registry] %s", subjectRef)).
MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec()
})

It("should attach a file to an arch-specific subject", func() {
// prepare
testRepo := attachTestRepo("arch-specific")
// Below line will cause unexpected 500
// pending for https://github.com/project-zot/zot/pull/2351 to be released
// CopyZOTRepo(ImageRepo, testRepo)
CopyZOTRepo(ImageRepo, testRepo)
// test
subjectRef := RegistryRef(ZOTHost, testRepo, multi_arch.Tag)
ORAS("cp", RegistryRef(ZOTHost, ImageRepo, multi_arch.Tag), subjectRef).Exec()
artifactType := "test/attach"
// test
out := ORAS("attach", "--artifact-type", artifactType, subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia), "--format", "go-template={{.digest}}", "--platform", "linux/amd64").
WithWorkDir(PrepareTempFiles()).Exec().Out.Contents()
// validate
Expand Down

0 comments on commit 6fe0395

Please sign in to comment.