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: add handlers to manifest push commmand #1555

Merged
merged 20 commits into from
Dec 16, 2024
7 changes: 5 additions & 2 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ func NewTagHandler(printer *output.Printer, target option.Target) metadata.TagHa
}

// NewManifestPushHandler returns a manifest push handler.
func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandler {
return text.NewManifestPushHandler(printer)
func NewManifestPushHandler(printer *output.Printer, outputDescriptor bool, pretty bool, desc ocispec.Descriptor, target *option.Target) (status.ManifestPushHandler, metadata.ManifestPushHandler) {
if outputDescriptor {
return status.NewDiscardHandler(), metadata.NewDiscardHandler()
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}
return status.NewTextManifestPushHandler(printer, desc), text.NewManifestPushHandler(printer, target)
}

// NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command.
Expand Down
10 changes: 10 additions & 0 deletions cmd/oras/internal/display/metadata/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
return nil
}

// OnManifestPushed implements ManifestPushHandler.
func (Discard) OnManifestPushed(ocispec.Descriptor) error {
return nil
}

// Render implements ManifestPushHandler.
func (Discard) Render() error {
return nil

Check warning on line 41 in cmd/oras/internal/display/metadata/discard.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/metadata/discard.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

// OnTagged implements ManifestIndexCreateHandler.
func (Discard) OnTagged(ocispec.Descriptor, string) error {
return nil
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/metadata/discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ func TestDiscard_OnTagged(t *testing.T) {
t.Errorf("testDiscard.OnTagged() error = %v, want nil", err)
}
}

func TestDiscardHandler_OnManifestPushed(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestPushed(ocispec.Descriptor{}); err != nil {
t.Errorf("DiscardHandler.OnManifestPushed() error = %v, wantErr nil", err)
}
}
3 changes: 3 additions & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type TagHandler interface {
// ManifestPushHandler handles metadata output for manifest push events.
type ManifestPushHandler interface {
TaggedHandler
Renderer

OnManifestPushed(desc ocispec.Descriptor) error
}

// ManifestIndexCreateHandler handles metadata output for index create events.
Expand Down
17 changes: 16 additions & 1 deletion cmd/oras/internal/display/metadata/text/manifest_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,37 @@ 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/option"
"oras.land/oras/cmd/oras/internal/output"
)

// ManifestPushHandler handles text metadata output for manifest push events.
type ManifestPushHandler struct {
printer *output.Printer
target *option.Target
desc ocispec.Descriptor
}

// NewManifestPushHandler returns a new handler for manifest push events.
func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandler {
func NewManifestPushHandler(printer *output.Printer, target *option.Target) metadata.ManifestPushHandler {
return &ManifestPushHandler{
printer: printer,
target: target,
}
}

// OnTagged implements metadata.TaggedHandler.
func (h *ManifestPushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}

// OnManifestPushed implements metadata.ManifestPushHandler.
func (h *ManifestPushHandler) OnManifestPushed(desc ocispec.Descriptor) error {
h.desc = desc
return h.printer.Println("Pushed:", h.target.AnnotatedReference())
}

// Render implements metadata.ManifestPushHandler.
func (h *ManifestPushHandler) Render() error {
return h.printer.Println("Digest:", h.desc.Digest)
}
15 changes: 15 additions & 0 deletions cmd/oras/internal/display/status/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ func (DiscardHandler) OnFetched(string, ocispec.Descriptor) error {
return nil
}

// OnManifestPushSkipped implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushSkipped() error {
return nil
}

// OnManifestPushing implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushing() error {
return nil
}

// OnManifestPushed implements ManifestPushHandler.
func (DiscardHandler) OnManifestPushed() error {
return nil
}

// OnManifestRemoved implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnManifestRemoved(digest.Digest) error {
return nil
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestDiscardHandler_OnPushSkipped(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestPushSkipped(); err != nil {
t.Errorf("DiscardHandler.OnPushSkipped() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnManifestRemoved(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestRemoved("sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ type CopyHandler interface {
StopTracking() error
}

// ManifestPushHandler handles status output for manifest push command.
type ManifestPushHandler interface {
OnManifestPushSkipped() error
OnManifestPushing() error
OnManifestPushed() error
}

// ManifestIndexCreateHandler handles status output for manifest index create command.
type ManifestIndexCreateHandler interface {
OnFetching(manifestRef string) error
Expand Down
26 changes: 26 additions & 0 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ func (ch *TextCopyHandler) OnMounted(_ context.Context, desc ocispec.Descriptor)
return ch.printer.PrintStatus(desc, copyPromptMounted)
}

// TextManifestPushHandler handles text status output for manifest push events.
type TextManifestPushHandler struct {
desc ocispec.Descriptor
printer *output.Printer
}

// NewTextManifestPushHandler returns a new handler for manifest push command.
func NewTextManifestPushHandler(printer *output.Printer, desc ocispec.Descriptor) ManifestPushHandler {
return &TextManifestPushHandler{
desc: desc,
printer: printer,
}
}

func (mph *TextManifestPushHandler) OnManifestPushSkipped() error {
return mph.printer.PrintStatus(mph.desc, PushPromptExists)
}

func (mph *TextManifestPushHandler) OnManifestPushing() error {
return mph.printer.PrintStatus(mph.desc, PushPromptUploading)
}

func (mph *TextManifestPushHandler) OnManifestPushed() error {
return mph.printer.PrintStatus(mph.desc, PushPromptUploaded)
}

// TextManifestIndexCreateHandler handles text status output for manifest index create events.
type TextManifestIndexCreateHandler struct {
printer *output.Printer
Expand Down
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/status/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ func TestTextPushHandler_PreCopy(t *testing.T) {
validatePrinted(t, "Uploading 0b442c23c1dd oci-image")
}

func TestTextManifestPushHandler_OnPushSkipped(t *testing.T) {
mph := NewTextManifestPushHandler(printer, ocispec.Descriptor{})
if mph.OnManifestPushSkipped() != nil {
t.Error("OnManifestExists() should not return an error")
}
}

func TestTextManifestIndexUpdateHandler_OnManifestAdded(t *testing.T) {
tests := []struct {
name string
Expand Down
20 changes: 10 additions & 10 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
return option.Parse(cmd, &opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Printer.Verbose = opts.verbose && !opts.OutputDescriptor
opts.Printer.Verbose = opts.verbose
return pushManifest(cmd, opts)
},
}
Expand Down Expand Up @@ -151,6 +151,7 @@

// prepare manifest descriptor
desc := content.NewDescriptorFromBytes(mediaType, contentBytes)
statusHandler, metadataHandler := display.NewManifestPushHandler(opts.Printer, opts.OutputDescriptor, opts.Pretty.Pretty, desc, &opts.Target)

ref := opts.Reference
if ref == "" {
Expand All @@ -161,17 +162,17 @@
return err
}
if match {
if err := opts.Printer.PrintStatus(desc, "Exists"); err != nil {
if err := statusHandler.OnManifestPushSkipped(); err != nil {
return err
}
} else {
if err = opts.Printer.PrintStatus(desc, "Uploading"); err != nil {
if err = statusHandler.OnManifestPushing(); err != nil {
return err
}
if _, err := oras.TagBytes(ctx, target, mediaType, contentBytes, ref); err != nil {
return err
}
if err = opts.Printer.PrintStatus(desc, "Uploaded "); err != nil {
if err = statusHandler.OnManifestPushed(); err != nil {
return err
}
}
Expand All @@ -192,18 +193,17 @@
}
return opts.Output(os.Stdout, descJSON)
}
_ = opts.Printer.Println("Pushed", opts.AnnotatedReference())
if err := metadataHandler.OnManifestPushed(desc); err != nil {
return err
}

Check warning on line 198 in cmd/oras/root/manifest/push.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/push.go#L197-L198

Added lines #L197 - L198 were not covered by tests
if len(opts.extraRefs) != 0 {
handler := display.NewManifestPushHandler(opts.Printer)
tagListener := listener.NewTaggedListener(target, handler.OnTagged)
tagListener := listener.NewTaggedListener(target, metadataHandler.OnTagged)
if _, err = oras.TagBytesN(ctx, tagListener, mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
return err
}
}

_ = opts.Printer.Println("Digest:", desc.Digest)

return nil
return metadataHandler.Render()
}

// matchDigest checks whether the manifest's digest matches to it in the remote
Expand Down
Loading