From f05cc6697dfcc9e3ed697292fe9d97ab85c5bf36 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Tue, 10 Dec 2024 15:54:38 +0800 Subject: [PATCH] resolved comments Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/handler.go | 8 ++++---- cmd/oras/internal/display/handler_test.go | 2 +- cmd/oras/internal/display/metadata/interface.go | 4 ++-- cmd/oras/internal/display/metadata/json/attach.go | 12 ++++++------ .../internal/display/metadata/template/attach.go | 10 +++++----- cmd/oras/internal/display/metadata/text/attach.go | 10 +++++----- cmd/oras/root/attach.go | 6 +++--- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/cmd/oras/internal/display/handler.go b/cmd/oras/internal/display/handler.go index 17af6104f..ce6243d9d 100644 --- a/cmd/oras/internal/display/handler.go +++ b/cmd/oras/internal/display/handler.go @@ -62,7 +62,7 @@ func NewPushHandler(printer *output.Printer, format option.Format, tty *os.File, } // NewAttachHandler returns status and metadata handlers for attach command. -func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.File, fetcher fetcher.Fetcher, target *option.Target) (status.AttachHandler, metadata.AttachHandler, error) { +func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.File, fetcher fetcher.Fetcher) (status.AttachHandler, metadata.AttachHandler, error) { var statusHandler status.AttachHandler if tty != nil { statusHandler = status.NewTTYAttachHandler(tty, fetcher) @@ -75,11 +75,11 @@ func NewAttachHandler(printer *output.Printer, format option.Format, tty *os.Fil var metadataHandler metadata.AttachHandler switch format.Type { case option.FormatTypeText.Name: - metadataHandler = text.NewAttachHandler(printer, target) + metadataHandler = text.NewAttachHandler(printer) case option.FormatTypeJSON.Name: - metadataHandler = json.NewAttachHandler(printer, target) + metadataHandler = json.NewAttachHandler(printer) case option.FormatTypeGoTemplate.Name: - metadataHandler = template.NewAttachHandler(printer, format.Template, target) + metadataHandler = template.NewAttachHandler(printer, format.Template) default: return nil, nil, errors.UnsupportedFormatTypeError(format.Type) } diff --git a/cmd/oras/internal/display/handler_test.go b/cmd/oras/internal/display/handler_test.go index a2ba05d6d..c6f5b933b 100644 --- a/cmd/oras/internal/display/handler_test.go +++ b/cmd/oras/internal/display/handler_test.go @@ -40,7 +40,7 @@ func TestNewPushHandler(t *testing.T) { func TestNewAttachHandler(t *testing.T) { mockFetcher := testutils.NewMockFetcher() printer := output.NewPrinter(os.Stdout, os.Stderr) - _, _, err := NewAttachHandler(printer, option.Format{Type: option.FormatTypeText.Name}, os.Stdout, mockFetcher.Fetcher, &option.Target{}) + _, _, err := NewAttachHandler(printer, option.Format{Type: option.FormatTypeText.Name}, os.Stdout, mockFetcher.Fetcher) if err != nil { t.Errorf("NewAttachHandler() error = %v, want nil", err) } diff --git a/cmd/oras/internal/display/metadata/interface.go b/cmd/oras/internal/display/metadata/interface.go index e1ebd2bdd..8099bd872 100644 --- a/cmd/oras/internal/display/metadata/interface.go +++ b/cmd/oras/internal/display/metadata/interface.go @@ -30,12 +30,12 @@ type PushHandler interface { // Renderer renders metadata information when an operation is complete. type Renderer interface { - OnCompleted() error + Render() error } // AttachHandler handles metadata output for attach events. type AttachHandler interface { - OnAttached(root ocispec.Descriptor, subject ocispec.Descriptor) + OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor) Renderer } diff --git a/cmd/oras/internal/display/metadata/json/attach.go b/cmd/oras/internal/display/metadata/json/attach.go index e45932c44..91cda78b2 100644 --- a/cmd/oras/internal/display/metadata/json/attach.go +++ b/cmd/oras/internal/display/metadata/json/attach.go @@ -33,19 +33,19 @@ type AttachHandler struct { } // NewAttachHandler creates a new handler for attach events. -func NewAttachHandler(out io.Writer, target *option.Target) metadata.AttachHandler { +func NewAttachHandler(out io.Writer) metadata.AttachHandler { return &AttachHandler{ - out: out, - target: target, + out: out, } } // OnAttached implements AttachHandler. -func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, _ ocispec.Descriptor) { +func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, _ ocispec.Descriptor) { + ah.target = target ah.root = root } -// OnCompleted is called when the attach command is completed. -func (ah *AttachHandler) OnCompleted() error { +// Render is called when the attach command is completed. +func (ah *AttachHandler) Render() error { return output.PrintPrettyJSON(ah.out, model.NewAttach(ah.root, ah.target.Path)) } diff --git a/cmd/oras/internal/display/metadata/template/attach.go b/cmd/oras/internal/display/metadata/template/attach.go index c7383d3de..d792daede 100644 --- a/cmd/oras/internal/display/metadata/template/attach.go +++ b/cmd/oras/internal/display/metadata/template/attach.go @@ -34,20 +34,20 @@ type AttachHandler struct { } // NewAttachHandler returns a new handler for attach metadata events. -func NewAttachHandler(out io.Writer, template string, target *option.Target) metadata.AttachHandler { +func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler { return &AttachHandler{ out: out, template: template, - target: target, } } // OnAttached implements AttachHandler. -func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, _ ocispec.Descriptor) { +func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, _ ocispec.Descriptor) { + ah.target = target ah.root = root } -// OnCompleted formats the metadata of attach command. -func (ah *AttachHandler) OnCompleted() error { +// Render formats the metadata of attach command. +func (ah *AttachHandler) Render() error { return output.ParseAndWrite(ah.out, model.NewAttach(ah.root, ah.target.Path), ah.template) } diff --git a/cmd/oras/internal/display/metadata/text/attach.go b/cmd/oras/internal/display/metadata/text/attach.go index 87c86fe00..dfc4cfc0f 100644 --- a/cmd/oras/internal/display/metadata/text/attach.go +++ b/cmd/oras/internal/display/metadata/text/attach.go @@ -34,21 +34,21 @@ type AttachHandler struct { } // NewAttachHandler returns a new handler for attach events. -func NewAttachHandler(printer *output.Printer, target *option.Target) metadata.AttachHandler { +func NewAttachHandler(printer *output.Printer) metadata.AttachHandler { return &AttachHandler{ printer: printer, - target: target, } } // OnAttached implements AttachHandler. -func (ah *AttachHandler) OnAttached(root ocispec.Descriptor, subject ocispec.Descriptor) { +func (ah *AttachHandler) OnAttached(target *option.Target, root ocispec.Descriptor, subject ocispec.Descriptor) { + ah.target = target ah.root = root ah.subject = subject } -// OnCompleted is called when the attach command is complete. -func (ah *AttachHandler) OnCompleted() error { +// Render is called when the attach command is complete. +func (ah *AttachHandler) Render() error { digest := ah.subject.Digest.String() if !strings.HasSuffix(ah.target.RawReference, digest) { ah.target.RawReference = fmt.Sprintf("%s@%s", ah.target.Path, ah.subject.Digest) diff --git a/cmd/oras/root/attach.go b/cmd/oras/root/attach.go index 7639e3ddb..0085deb3c 100644 --- a/cmd/oras/root/attach.go +++ b/cmd/oras/root/attach.go @@ -158,7 +158,7 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error { if err != nil { return fmt.Errorf("failed to resolve %s: %w", opts.Reference, err) } - statusHandler, metadataHandler, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store, &opts.Target) + statusHandler, metadataHandler, err := display.NewAttachHandler(opts.Printer, opts.Format, opts.TTY, store) if err != nil { return err } @@ -211,9 +211,9 @@ func runAttach(cmd *cobra.Command, opts *attachOptions) error { return err } - metadataHandler.OnAttached(root, subject) + metadataHandler.OnAttached(&opts.Target, root, subject) - err = metadataHandler.OnCompleted() + err = metadataHandler.Render() if err != nil { return err }