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) (status.ManifestPushHandler, metadata.ManifestPushHandler) {
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
if outputDescriptor {
return status.NewDiscardHandler(), metadata.NewDiscardHandler()
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}
return status.NewTextManifestPushHandler(printer), text.NewManifestPushHandler(printer)
}

// NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command.
Expand Down
1 change: 1 addition & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type TagHandler interface {
// ManifestPushHandler handles metadata output for manifest push events.
type ManifestPushHandler interface {
TaggedHandler
OnCompleted(desc ocispec.Descriptor) error
}

// ManifestIndexCreateHandler handles metadata output for index create events.
Expand Down
5 changes: 5 additions & 0 deletions cmd/oras/internal/display/metadata/text/manifest_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandle
func (h *ManifestPushHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}

// OnCompleted implements metadata.ManifestPushHandler.
func (h *ManifestPushHandler) OnCompleted(desc ocispec.Descriptor) error {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
return h.printer.Println("Digest:", desc.Digest)
}
20 changes: 20 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,26 @@ func (DiscardHandler) OnFetched(string, ocispec.Descriptor) error {
return nil
}

// OnManifestExists implements ManifestPushHandler.
func (DiscardHandler) OnManifestExists(desc ocispec.Descriptor) error {
return nil
}

// OnManifestUploading implements ManifestPushHandler.
func (DiscardHandler) OnManifestUploading(desc ocispec.Descriptor) error {
return nil
}

// OnManifestUploaded implements ManifestPushHandler.
func (DiscardHandler) OnManifestUploaded(desc ocispec.Descriptor) error {
return nil
}

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

// OnManifestRemoved implements ManifestIndexUpdateHandler.
func (DiscardHandler) OnManifestRemoved(digest.Digest) error {
return nil
Expand Down
14 changes: 14 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,20 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestDiscardHandler_OnManifestPushed(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestPushed("test"); err != nil {
t.Errorf("DiscardHandler.OnIndexMerged() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnManifestExists(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestExists(v1.Descriptor{}); err != nil {
t.Errorf("DiscardHandler.OnIndexExists() error = %v, wantErr nil", err)
}
}

func TestDiscardHandler_OnManifestRemoved(t *testing.T) {
testDiscard := NewDiscardHandler()
if err := testDiscard.OnManifestRemoved("sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ type CopyHandler interface {
OnMounted(ctx context.Context, desc ocispec.Descriptor) error
}

// ManifestPushHandler handles status output for manifest push command.
type ManifestPushHandler interface {
OnManifestExists(desc ocispec.Descriptor) error
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
OnManifestUploading(desc ocispec.Descriptor) error
OnManifestUploaded(desc ocispec.Descriptor) error
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
OnManifestPushed(ref string) error
qweeah marked this conversation as resolved.
Show resolved Hide resolved
}

// ManifestIndexCreateHandler handles status output for manifest index create command.
type ManifestIndexCreateHandler interface {
OnFetching(manifestRef string) error
Expand Down
29 changes: 29 additions & 0 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ 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 {
printer *output.Printer
}

// NewTextManifestPushHandler returns a new handler for manifest push command.
func NewTextManifestPushHandler(printer *output.Printer) ManifestPushHandler {
tmich := TextManifestPushHandler{
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
printer: printer,
}
return &tmich
}

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

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

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

func (mph *TextManifestPushHandler) OnManifestPushed(ref string) error {
return mph.printer.Println(PushPromptPushed, ref)
}

// 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_OnManifestExists(t *testing.T) {
mph := NewTextManifestPushHandler(printer)
if mph.OnManifestExists(ocispec.Descriptor{}) != nil {
t.Error("OnManifestExists() should not return an error")
}
}

func TestTextManifestIndexUpdateHandler_OnManifestAdded(t *testing.T) {
tests := []struct {
name string
Expand Down
1 change: 1 addition & 0 deletions cmd/oras/internal/display/status/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
PushPromptUploading = "Uploading"
PushPromptSkipped = "Skipped "
PushPromptExists = "Exists "
PushPromptPushed = "Pushed "
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
)

// Prompts for cp events.
Expand Down
19 changes: 10 additions & 9 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
}
}

displayStatus, displayMetadata := display.NewManifestPushHandler(opts.Printer, opts.OutputDescriptor, opts.Pretty.Pretty)
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved

// prepare manifest descriptor
desc := content.NewDescriptorFromBytes(mediaType, contentBytes)

wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -159,17 +161,17 @@
return err
}
if match {
if err := opts.Printer.PrintStatus(desc, "Exists"); err != nil {
if err := displayStatus.OnManifestExists(desc); err != nil {
return err
}
} else {
if err = opts.Printer.PrintStatus(desc, "Uploading"); err != nil {
if err = displayStatus.OnManifestUploading(desc); 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 = displayStatus.OnManifestUploaded(desc); err != nil {
return err
}
}
Expand All @@ -190,18 +192,17 @@
}
return opts.Output(os.Stdout, descJSON)
}
_ = opts.Printer.Println("Pushed", opts.AnnotatedReference())
if err := displayStatus.OnManifestPushed(opts.AnnotatedReference()); err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L196 - L197 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, displayMetadata.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 displayMetadata.OnCompleted(desc)
}

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