Skip to content

Commit

Permalink
apply metadata render interface to oras discover command
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Jan 14, 2025
1 parent 8a31d9b commit e34a5a1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/table/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/template/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/tree/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit e34a5a1

Please sign in to comment.