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

refactor: apply metadata render interface to oras push command #1583

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"oras.land/oras/cmd/oras/internal/option"
)

// Renderer renders metadata information when an operation is complete.
type Renderer interface {
Render() error
}

// PushHandler handles metadata output for push events.
type PushHandler interface {
TaggedHandler

OnCopied(opts *option.Target) error
OnCompleted(root ocispec.Descriptor) error
}

// Renderer renders metadata information when an operation is complete.
type Renderer interface {
Render() error
OnCopied(opts *option.Target, root ocispec.Descriptor) error
Renderer
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}

// AttachHandler handles metadata output for attach events.
Expand Down
10 changes: 6 additions & 4 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type PushHandler struct {
path string
out io.Writer
tagged model.Tagged
root ocispec.Descriptor
}

// NewPushHandler creates a new handler for push events.
Expand All @@ -47,15 +48,16 @@ func (ph *PushHandler) OnTagged(desc ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
func (ph *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
if opts.RawReference != "" && !contentutil.IsDigest(opts.Reference) {
ph.tagged.AddTag(opts.Reference)
}
ph.path = opts.Path
ph.root = root
return nil
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return output.PrintPrettyJSON(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()))
// Render is called after the push is completed.
func (ph *PushHandler) Render() error {
return output.PrintPrettyJSON(ph.out, model.NewPush(ph.root, ph.path, ph.tagged.Tags()))
}
10 changes: 6 additions & 4 deletions cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PushHandler struct {
path string
tagged model.Tagged
out io.Writer
root ocispec.Descriptor
}

// NewPushHandler returns a new handler for push events.
Expand All @@ -49,15 +50,16 @@ func (ph *PushHandler) OnTagged(desc ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
func (ph *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
if opts.RawReference != "" && !contentutil.IsDigest(opts.Reference) {
ph.tagged.AddTag(opts.Reference)
}
ph.path = opts.Path
ph.root = root
return nil
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return output.ParseAndWrite(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()), ph.template)
// Render is called after the push is completed.
qweeah marked this conversation as resolved.
Show resolved Hide resolved
func (ph *PushHandler) Render() error {
return output.ParseAndWrite(ph.out, model.NewPush(ph.root, ph.path, ph.tagged.Tags()), ph.template)
}
12 changes: 7 additions & 5 deletions cmd/oras/internal/display/metadata/text/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type PushHandler struct {
printer *output.Printer
tagLock sync.Mutex
root ocispec.Descriptor
}

// NewPushHandler returns a new handler for push events.
Expand All @@ -45,15 +46,16 @@ func (h *PushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
}

// OnCopied is called after files are copied.
func (h *PushHandler) OnCopied(opts *option.Target) error {
func (h *PushHandler) OnCopied(opts *option.Target, root ocispec.Descriptor) error {
h.root = root
return h.printer.Println("Pushed", opts.AnnotatedReference())
}

// OnCompleted is called after the push is completed.
func (h *PushHandler) OnCompleted(root ocispec.Descriptor) error {
err := h.printer.Println("ArtifactType:", root.ArtifactType)
// Render is called after the push is completed.
func (h *PushHandler) Render() error {
err := h.printer.Println("ArtifactType:", h.root.ArtifactType)
if err != nil {
return err
}
return h.printer.Println("Digest:", root.Digest)
return h.printer.Println("Digest:", h.root.Digest)
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/text/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func TestPushHandler_OnCompleted(t *testing.T) {
printer := output.NewPrinter(tt.out, os.Stderr)
p := &PushHandler{
printer: printer,
root: tt.root,
}
if err := p.OnCompleted(tt.root); (err != nil) != tt.wantErr {
if err := p.Render(); (err != nil) != tt.wantErr {
t.Errorf("PushHandler.OnCompleted() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func runPush(cmd *cobra.Command, opts *pushOptions) error {
if err != nil {
return err
}
err = displayMetadata.OnCopied(&opts.Target)
err = displayMetadata.OnCopied(&opts.Target, root)
if err != nil {
return err
}
Expand All @@ -288,7 +288,7 @@ func runPush(cmd *cobra.Command, opts *pushOptions) error {
}
}

err = displayMetadata.OnCompleted(root)
err = displayMetadata.Render()
if err != nil {
return err
}
Expand Down
Loading