From e34a5a11fae4ceaae01d274d53a2b72fb5e3bc7a Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Tue, 14 Jan 2025 10:11:40 +0800 Subject: [PATCH] apply metadata render interface to oras discover command Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/metadata/interface.go | 4 ++-- cmd/oras/internal/display/metadata/json/discover.go | 4 ++-- cmd/oras/internal/display/metadata/table/discover.go | 4 ++-- .../internal/display/metadata/template/discover.go | 4 ++-- cmd/oras/internal/display/metadata/tree/discover.go | 4 ++-- cmd/oras/root/discover.go | 10 +++++----- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/oras/internal/display/metadata/interface.go b/cmd/oras/internal/display/metadata/interface.go index cc8d504c0..8815a0500 100644 --- a/cmd/oras/internal/display/metadata/interface.go +++ b/cmd/oras/internal/display/metadata/interface.go @@ -42,13 +42,13 @@ type AttachHandler interface { // DiscoverHandler handles metadata output for discover events. type DiscoverHandler interface { + Renderer + // MultiLevelSupported returns true if the handler supports multi-level // discovery. MultiLevelSupported() bool // OnDiscovered is called after a referrer is discovered. OnDiscovered(referrer, subject ocispec.Descriptor) error - // OnCompleted is called when referrer discovery is completed. - OnCompleted() error } // ManifestFetchHandler handles metadata output for manifest fetch events. diff --git a/cmd/oras/internal/display/metadata/json/discover.go b/cmd/oras/internal/display/metadata/json/discover.go index 8d9eea230..fa1c24e0e 100644 --- a/cmd/oras/internal/display/metadata/json/discover.go +++ b/cmd/oras/internal/display/metadata/json/discover.go @@ -57,7 +57,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err return nil } -// OnCompleted implements metadata.DiscoverHandler. -func (h *discoverHandler) OnCompleted() error { +// Render implements metadata.DiscoverHandler. +func (h *discoverHandler) Render() error { return output.PrintPrettyJSON(h.out, model.NewDiscover(h.path, h.referrers)) } diff --git a/cmd/oras/internal/display/metadata/table/discover.go b/cmd/oras/internal/display/metadata/table/discover.go index 6acd0e6e8..d76967fd0 100644 --- a/cmd/oras/internal/display/metadata/table/discover.go +++ b/cmd/oras/internal/display/metadata/table/discover.go @@ -59,8 +59,8 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err return nil } -// OnCompleted implements metadata.DiscoverHandler. -func (h *discoverHandler) OnCompleted() error { +// Render implements metadata.DiscoverHandler. +func (h *discoverHandler) Render() error { if n := len(h.referrers); n > 1 { fmt.Fprintln(h.out, "Discovered", n, "artifacts referencing", h.rawReference) } else { diff --git a/cmd/oras/internal/display/metadata/template/discover.go b/cmd/oras/internal/display/metadata/template/discover.go index 1495a4b03..8ce7581ae 100644 --- a/cmd/oras/internal/display/metadata/template/discover.go +++ b/cmd/oras/internal/display/metadata/template/discover.go @@ -59,7 +59,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err return nil } -// OnCompleted implements metadata.DiscoverHandler. -func (h *discoverHandler) OnCompleted() error { +// Render implements metadata.DiscoverHandler. +func (h *discoverHandler) Render() error { return output.ParseAndWrite(h.out, model.NewDiscover(h.path, h.referrers), h.template) } diff --git a/cmd/oras/internal/display/metadata/tree/discover.go b/cmd/oras/internal/display/metadata/tree/discover.go index 496de1279..0738a6597 100644 --- a/cmd/oras/internal/display/metadata/tree/discover.go +++ b/cmd/oras/internal/display/metadata/tree/discover.go @@ -78,7 +78,7 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err return nil } -// OnCompleted implements metadata.DiscoverHandler. -func (h *discoverHandler) OnCompleted() error { +// Render implements metadata.DiscoverHandler. +func (h *discoverHandler) Render() error { return tree.NewPrinter(h.out).Print(h.root) } diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index e65817b57..cd2c36b1d 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -132,12 +132,12 @@ func runDiscover(cmd *cobra.Command, opts *discoverOptions) error { return err } - handler, err := display.NewDiscoverHandler(opts.Printer, opts.Format, opts.Path, opts.RawReference, desc, opts.verbose) + metadataHandler, err := display.NewDiscoverHandler(opts.Printer, opts.Format, opts.Path, opts.RawReference, desc, opts.verbose) if err != nil { return err } - if handler.MultiLevelSupported() { - if err := fetchAllReferrers(ctx, repo, desc, opts.artifactType, handler); err != nil { + if metadataHandler.MultiLevelSupported() { + if err := fetchAllReferrers(ctx, repo, desc, opts.artifactType, metadataHandler); err != nil { return err } } else { @@ -146,12 +146,12 @@ func runDiscover(cmd *cobra.Command, opts *discoverOptions) error { return err } for _, ref := range refs { - if err := handler.OnDiscovered(ref, desc); err != nil { + if err := metadataHandler.OnDiscovered(ref, desc); err != nil { return err } } } - return handler.OnCompleted() + return metadataHandler.Render() } func fetchAllReferrers(ctx context.Context, repo oras.ReadOnlyGraphTarget, desc ocispec.Descriptor, artifactType string, handler metadata.DiscoverHandler) error {