Skip to content

Commit

Permalink
resolve some comments
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
Xiaoxuan Wang committed Dec 4, 2024
1 parent 1e4e94c commit 2f10233
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 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,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,
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/internal/display/status/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/status/discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/display/status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/display/status/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {

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

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/push.go#L164

Added line #L164 was not covered by tests
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
}
}
Expand Down

0 comments on commit 2f10233

Please sign in to comment.