Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: status, metadata and content handlers for manifest index commands #1509

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

wangxiaoxuan273
Copy link
Contributor

What this PR does / why we need it:
Implements output handlers for manifest index commands.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #1503

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

Xiaoxuan Wang added 5 commits September 24, 2024 10:39
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
@wangxiaoxuan273 wangxiaoxuan273 changed the title Output output refactor: status, metadata and content handlers for manifest index commands Sep 24, 2024
Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 73.46939% with 52 lines in your changes missing coverage. Please review.

Project coverage is 85.28%. Comparing base (8dc05a7) to head (4d18845).

Files with missing lines Patch % Lines
cmd/oras/root/manifest/index/update.go 47.82% 12 Missing and 12 partials ⚠️
cmd/oras/root/manifest/index/create.go 44.00% 7 Missing and 7 partials ⚠️
cmd/oras/internal/display/status/discard.go 75.00% 6 Missing ⚠️
cmd/oras/internal/display/status/text.go 90.47% 3 Missing and 1 partial ⚠️
...md/oras/internal/display/content/manifest_index.go 85.71% 1 Missing and 1 partial ⚠️
cmd/oras/internal/display/metadata/discard.go 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   86.10%   85.28%   -0.83%     
==========================================
  Files         118      119       +1     
  Lines        4224     4355     +131     
==========================================
+ Hits         3637     3714      +77     
- Misses        350      382      +32     
- Partials      237      259      +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Xiaoxuan Wang added 3 commits September 24, 2024 17:04
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
@@ -91,6 +89,10 @@ Example - Create an index and output the index to stdout, auto push will be disa
opts.RawReference = refs[0]
opts.extraRefs = refs[1:]
opts.sources = args[1:]
// ignore --pretty when output to a file
if opts.outputPath != "" && opts.outputPath != "-" {
opts.Pretty.Pretty = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should move this pretty reset within OnContentCreated, so you don't need to duplicate this snippet in index update

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the option handling in command maybe?


// NewManifestIndexCreateHandler creates a new handler.
func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string) ManifestIndexCreateHandler {
return &manifestIndexCreate{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset pretty here if output path is -

@@ -37,3 +38,8 @@ func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestInd
func (h *ManifestIndexCreateHandler) OnTagged(_ ocispec.Descriptor, tag string) error {
return h.printer.Println("Tagged", tag)
}

// OnCompleted implements ManifestIndexCreateHandler.
func (h *ManifestIndexCreateHandler) OnCompleted(digest digest.Digest) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering we are most likely to have format. Here should pass in a descriptor and output the digest of the descriptor

@@ -28,3 +31,13 @@ func NewDiscardHandler() discard {
func (discard) OnFetched(string, ocispec.Descriptor, []byte) error {
return nil
}

// OnTagged implements ManifestIndexCreateHandler.
func (discard) OnTagged(desc ocispec.Descriptor, tag string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT

Suggested change
func (discard) OnTagged(desc ocispec.Descriptor, tag string) error {
func (discard) OnTagged(ocispec.Descriptor, string) error {

}

// OnCompleted implements ManifestIndexCreateHandler.
func (discard) OnCompleted(digest digest.Digest) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (discard) OnCompleted(digest digest.Digest) error {
func (discard) OnCompleted(ocispec.Descriptor) error {

@@ -81,8 +82,12 @@ type ManifestPushHandler interface {
// ManifestIndexCreateHandler handles metadata output for index create events.
type ManifestIndexCreateHandler interface {
TaggedHandler
OnCompleted(digest digest.Digest) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OnCompleted(digest digest.Digest) error
OnCompleted(ocispec.Descriptor) error

type ManifestIndexCreateHandler interface {
OnSourceManifestFetching(source string) error
OnSourceManifestFetched(source string) error
OnIndexPacked(shortDigest string) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packed index should be moved to metadata output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packed, Updated, Pushed, Added, Removed, Merged should all be moved to metadata output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use one interface for packed and updated.

Comment on lines +69 to +70
OnSourceManifestFetching(source string) error
OnSourceManifestFetched(source string) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove Source from the naming? Will non-source manifest be fetched?

Comment on lines +81 to +83
OnManifestRemoved(digest digest.Digest) error
OnManifestAdded(manifestRef string, digest digest.Digest) error
OnIndexMerged(indexRef string, digest digest.Digest) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to metadata handler

}

// ManifestIndexUpdateHandler handles status output for manifest index update command.
type ManifestIndexUpdateHandler interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contains duplicated methods ManifestIndexCreateHandler as below

	OnIndexFetching(indexRef string) error
	OnIndexFetched(indexRef string, digest digest.Digest) error

You can create a new interface and use it in both interfaces.

Comment on lines +183 to +199
var statusHandler status.ManifestIndexCreateHandler
var metadataHandler metadata.ManifestIndexCreateHandler
var contentHandler content.ManifestIndexCreateHandler
switch outputPath {
case "":
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewDiscardHandler()
case "-":
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
default:
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var statusHandler status.ManifestIndexCreateHandler
var metadataHandler metadata.ManifestIndexCreateHandler
var contentHandler content.ManifestIndexCreateHandler
switch outputPath {
case "":
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewDiscardHandler()
case "-":
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
default:
statusHandler = status.NewTextManifestIndexCreateHandler(printer)
metadataHandler = text.NewManifestIndexCreateHandler(printer)
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
}
statusHandler := status.NewTextManifestIndexCreateHandler(printer)
metadataHandler := text.NewManifestIndexCreateHandler(printer)
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath)
switch outputPath {
case "":
contentHandler = content.NewDiscardHandler()
case "-":
statusHandler = status.NewDiscardHandler()
metadataHandler = metadata.NewDiscardHandler()
}

Comment on lines +82 to +85
// ignore --pretty when output to a file
if opts.outputPath != "" && opts.outputPath != "-" {
opts.Pretty.Pretty = false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be merged into the handler construction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a general option thing, maybe a method in otpions?

// OnContentCreated is called after index content is created.
func (h *manifestIndexCreate) OnContentCreated(manifest []byte) error {
out := h.stdout
if h.outputPath != "-" && h.outputPath != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you changed outputPath to "" for "-" this logic would look less odd.

}

// NewManifestIndexUpdateHandler returns status, metadata and content handlers for index update command.
func NewManifestIndexUpdateHandler(outputPath string, printer *output.Printer, pretty bool) (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there just be a NewManivestIndexHandler method for both for now and someone can add the other in the future if needed?

Comment on lines +212 to +215
tmich := TextManifestIndexCreateHandler{
printer: printer,
}
return &tmich
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tmich := TextManifestIndexCreateHandler{
printer: printer,
}
return &tmich
manifestHandler := TextManifestIndexCreateHandler{
printer: printer,
}
return &manifestHandler

Comment on lines +282 to +285
miuh := TextManifestIndexUpdateHandler{
printer: printer,
}
return &miuh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
miuh := TextManifestIndexUpdateHandler{
printer: printer,
}
return &miuh
manifestHandler := TextManifestIndexUpdateHandler{
printer: printer,
}
return &manifestHandler

@@ -91,6 +89,10 @@ Example - Create an index and output the index to stdout, auto push will be disa
opts.RawReference = refs[0]
opts.extraRefs = refs[1:]
opts.sources = args[1:]
// ignore --pretty when output to a file
if opts.outputPath != "" && opts.outputPath != "-" {
opts.Pretty.Pretty = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the option handling in command maybe?

@@ -188,7 +197,8 @@ func getPlatform(ctx context.Context, target oras.ReadOnlyTarget, manifestBytes
return &platform, nil
}

func pushIndex(ctx context.Context, target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path string, printer *output.Printer) error {
func pushIndex(ctx context.Context, onIndexPushed func(path string) error, onTagged func(desc ocispec.Descriptor, tag string) error,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private method here, seems like it would make more sense for it to take handlers than function pointers.

Another thing, it seems like an extremely long list of params like something is not right here.

Comment on lines +82 to +85
// ignore --pretty when output to a file
if opts.outputPath != "" && opts.outputPath != "-" {
opts.Pretty.Pretty = false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a general option thing, maybe a method in otpions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

oras manifest index create/update --output - should hide other stdout output
3 participants