From 2f1023387580f03eebca86c2679a4286ec2ae8d1 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Wed, 4 Dec 2024 10:56:11 +0800 Subject: [PATCH] resolve some comments Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/handler.go | 4 ++-- .../display/metadata/text/manifest_push.go | 5 ++++- cmd/oras/internal/display/status/discard.go | 8 ++++---- .../internal/display/status/discard_test.go | 4 ++-- cmd/oras/internal/display/status/interface.go | 6 +++--- cmd/oras/internal/display/status/text.go | 19 ++++++++++--------- cmd/oras/internal/display/status/text_test.go | 6 +++--- cmd/oras/root/manifest/push.go | 10 +++++----- 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/cmd/oras/internal/display/handler.go b/cmd/oras/internal/display/handler.go index 731a0f600..4d80c9867 100644 --- a/cmd/oras/internal/display/handler.go +++ b/cmd/oras/internal/display/handler.go @@ -170,11 +170,11 @@ func NewTagHandler(printer *output.Printer, target option.Target) metadata.TagHa } // NewManifestPushHandler returns a manifest push handler. -func NewManifestPushHandler(printer *output.Printer, outputDescriptor bool, pretty bool) (status.ManifestPushHandler, metadata.ManifestPushHandler) { +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() } - return status.NewTextManifestPushHandler(printer), text.NewManifestPushHandler(printer) + return status.NewTextManifestPushHandler(printer, desc), text.NewManifestPushHandler(printer, target) } // NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command. diff --git a/cmd/oras/internal/display/metadata/text/manifest_push.go b/cmd/oras/internal/display/metadata/text/manifest_push.go index f6246a118..b8686da3d 100644 --- a/cmd/oras/internal/display/metadata/text/manifest_push.go +++ b/cmd/oras/internal/display/metadata/text/manifest_push.go @@ -18,18 +18,21 @@ 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 } // 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, } } diff --git a/cmd/oras/internal/display/status/discard.go b/cmd/oras/internal/display/status/discard.go index b86b119c8..ab9bc1610 100644 --- a/cmd/oras/internal/display/status/discard.go +++ b/cmd/oras/internal/display/status/discard.go @@ -100,18 +100,18 @@ func (DiscardHandler) OnFetched(string, ocispec.Descriptor) error { return nil } -// OnManifestExists implements ManifestPushHandler. -func (DiscardHandler) OnManifestExists(desc ocispec.Descriptor) error { +// OnPushSkipped implements ManifestPushHandler. +func (DiscardHandler) OnPushSkipped() error { return nil } // OnManifestUploading implements ManifestPushHandler. -func (DiscardHandler) OnManifestUploading(desc ocispec.Descriptor) error { +func (DiscardHandler) OnManifestUploading() error { return nil } // OnManifestUploaded implements ManifestPushHandler. -func (DiscardHandler) OnManifestUploaded(desc ocispec.Descriptor) error { +func (DiscardHandler) OnManifestUploaded() error { return nil } diff --git a/cmd/oras/internal/display/status/discard_test.go b/cmd/oras/internal/display/status/discard_test.go index 11b6582f9..5df1a1c62 100644 --- a/cmd/oras/internal/display/status/discard_test.go +++ b/cmd/oras/internal/display/status/discard_test.go @@ -28,9 +28,9 @@ func TestDiscardHandler_OnManifestPushed(t *testing.T) { } } -func TestDiscardHandler_OnManifestExists(t *testing.T) { +func TestDiscardHandler_OnPushSkipped(t *testing.T) { testDiscard := NewDiscardHandler() - if err := testDiscard.OnManifestExists(v1.Descriptor{}); err != nil { + if err := testDiscard.OnPushSkipped(); err != nil { t.Errorf("DiscardHandler.OnIndexExists() error = %v, wantErr nil", err) } } diff --git a/cmd/oras/internal/display/status/interface.go b/cmd/oras/internal/display/status/interface.go index 45251d264..e5151a634 100644 --- a/cmd/oras/internal/display/status/interface.go +++ b/cmd/oras/internal/display/status/interface.go @@ -66,9 +66,9 @@ type CopyHandler interface { // ManifestPushHandler handles status output for manifest push command. type ManifestPushHandler interface { - OnManifestExists(desc ocispec.Descriptor) error - OnManifestUploading(desc ocispec.Descriptor) error - OnManifestUploaded(desc ocispec.Descriptor) error + OnPushSkipped() error + OnManifestUploading() error + OnManifestUploaded() error OnManifestPushed(ref string) error } diff --git a/cmd/oras/internal/display/status/text.go b/cmd/oras/internal/display/status/text.go index cf17281b4..3a2e60a99 100644 --- a/cmd/oras/internal/display/status/text.go +++ b/cmd/oras/internal/display/status/text.go @@ -185,27 +185,28 @@ func (ch *TextCopyHandler) OnMounted(_ context.Context, desc ocispec.Descriptor) // 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) ManifestPushHandler { - tmich := TextManifestPushHandler{ +func NewTextManifestPushHandler(printer *output.Printer, desc ocispec.Descriptor) ManifestPushHandler { + return &TextManifestPushHandler{ + desc: desc, printer: printer, } - return &tmich } -func (mph *TextManifestPushHandler) OnManifestExists(desc ocispec.Descriptor) error { - return mph.printer.PrintStatus(desc, PushPromptExists) +func (mph *TextManifestPushHandler) OnPushSkipped() error { + return mph.printer.PrintStatus(mph.desc, PushPromptExists) } -func (mph *TextManifestPushHandler) OnManifestUploading(desc ocispec.Descriptor) error { - return mph.printer.PrintStatus(desc, PushPromptUploading) +func (mph *TextManifestPushHandler) OnManifestUploading() error { + return mph.printer.PrintStatus(mph.desc, PushPromptUploading) } -func (mph *TextManifestPushHandler) OnManifestUploaded(desc ocispec.Descriptor) error { - return mph.printer.PrintStatus(desc, PushPromptUploaded) +func (mph *TextManifestPushHandler) OnManifestUploaded() error { + return mph.printer.PrintStatus(mph.desc, PushPromptUploaded) } func (mph *TextManifestPushHandler) OnManifestPushed(ref string) error { diff --git a/cmd/oras/internal/display/status/text_test.go b/cmd/oras/internal/display/status/text_test.go index cb6a245de..407db3f63 100644 --- a/cmd/oras/internal/display/status/text_test.go +++ b/cmd/oras/internal/display/status/text_test.go @@ -194,9 +194,9 @@ 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 { +func TestTextManifestPushHandler_OnPushSkipped(t *testing.T) { + mph := NewTextManifestPushHandler(printer, ocispec.Descriptor{}) + if mph.OnPushSkipped() != nil { t.Error("OnManifestExists() should not return an error") } } diff --git a/cmd/oras/root/manifest/push.go b/cmd/oras/root/manifest/push.go index b9885677c..f6b63890c 100644 --- a/cmd/oras/root/manifest/push.go +++ b/cmd/oras/root/manifest/push.go @@ -147,11 +147,11 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error { } } - displayStatus, displayMetadata := display.NewManifestPushHandler(opts.Printer, opts.OutputDescriptor, opts.Pretty.Pretty) - // prepare manifest descriptor desc := content.NewDescriptorFromBytes(mediaType, contentBytes) + displayStatus, displayMetadata := display.NewManifestPushHandler(opts.Printer, opts.OutputDescriptor, opts.Pretty.Pretty, desc, opts.Target) + ref := opts.Reference if ref == "" { ref = desc.Digest.String() @@ -161,17 +161,17 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error { return err } if match { - if err := displayStatus.OnManifestExists(desc); err != nil { + if err := displayStatus.OnPushSkipped(); err != nil { return err } } else { - if err = displayStatus.OnManifestUploading(desc); err != nil { + if err = displayStatus.OnManifestUploading(); err != nil { return err } if _, err := oras.TagBytes(ctx, target, mediaType, contentBytes, ref); err != nil { return err } - if err = displayStatus.OnManifestUploaded(desc); err != nil { + if err = displayStatus.OnManifestUploaded(); err != nil { return err } }