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 19 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 64.32161% with 71 lines in your changes missing coverage. Please review.

Project coverage is 82.90%. Comparing base (97cb376) to head (f6c0790).

Files with missing lines Patch % Lines
cmd/oras/root/manifest/index/update.go 35.18% 23 Missing and 12 partials ⚠️
cmd/oras/root/manifest/index/create.go 30.00% 14 Missing and 7 partials ⚠️
cmd/oras/internal/display/status/discard.go 57.14% 6 Missing ⚠️
cmd/oras/internal/display/status/text.go 90.24% 3 Missing and 1 partial ⚠️
...md/oras/internal/display/content/manifest_index.go 85.00% 2 Missing and 1 partial ⚠️
cmd/oras/internal/display/metadata/discard.go 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   83.84%   82.90%   -0.95%     
==========================================
  Files         118      119       +1     
  Lines        5156     5281     +125     
==========================================
+ Hits         4323     4378      +55     
- Misses        592      641      +49     
- Partials      241      262      +21     

☔ 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>
cmd/oras/root/manifest/index/create.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/metadata/discard.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/metadata/discard.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/metadata/interface.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/status/interface.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/status/interface.go Outdated Show resolved Hide resolved
cmd/oras/internal/display/status/interface.go Outdated Show resolved Hide resolved
}

// 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an interface ManifestReferenceFetchHandler and both index create and index update handlers contain it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on this comment, this interface is now removed.

cmd/oras/internal/display/handler.go Outdated Show resolved Hide resolved
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?

Copy link
Contributor Author

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?

@TerryHowe We can move it to content handler to avoid code repeat.

// 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you mean to put "" before "-"? Changed the if condition to if outputPath != "" && outputPath != "-"

}

// 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently we need both create and update handlers.

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

Choose a reason for hiding this comment

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

The current naming style is aligned with the rest of the file.

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

Choose a reason for hiding this comment

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

The current naming style is aligned with the rest of the file.

cmd/oras/root/manifest/index/create.go Outdated Show resolved Hide resolved
@@ -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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since both create and update use this method and index create handlers and index update handlers have different types, the function argument can't take handler argument.

Yeah this is an extremely long list of parameters, but I can't find another way :(

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?

Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Comment on lines +212 to +215
tmich := TextManifestIndexCreateHandler{
printer: printer,
}
return &tmich
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current naming style is aligned with the rest of the file.

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

Choose a reason for hiding this comment

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

The current naming style is aligned with the rest of the file.

Xiaoxuan Wang added 5 commits October 9, 2024 15:01
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>

// ManifestIndexUpdateHandler handles text metadata output for index update events.
type ManifestIndexUpdateHandler struct {
printer *output.Printer
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you help separate ManifestIndexUpdateHandler and ManifestIndexCreateHandler into manifest_index_update.go and manifest_index_create.go?

Comment on lines 98 to 101
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.

Change digest to descriptor since it will be used for formatted output in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The parameter of OnManifestRemoved(digest digest.Digest) error must be a digest, since the descriptor is unknown. Changed the parameter of the other two functions as descriptors.

status.ManifestIndexCreateHandler,
metadata.ManifestIndexCreateHandler,
content.ManifestIndexCreateHandler,
error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When will an error be returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch. Removed the error return.

@@ -52,12 +52,6 @@ const (
const (
IndexPromptFetching = "Fetching "
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra space, same for fetched.

Suggested change
IndexPromptFetching = "Fetching "
IndexPromptFetching = "Fetching"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are not doing status output indentation align anymore?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, there are still aligning work but there is trailing space for both Fetching and Fetched. Should remove one space from both prompts.

Comment on lines 196 to 198
func pushIndex(ctx context.Context, onIndexPushed func(path string) error, onTagged func(desc ocispec.Descriptor, tag string) error,
target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path 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.

Try using handlers like:

Suggested change
func pushIndex(ctx context.Context, onIndexPushed func(path string) error, onTagged func(desc ocispec.Descriptor, tag string) error,
target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path string) error {
func pushIndex(ctx context.Context, pmh metadata.ManifestPackHandler, th metadata.TaggedHandler,target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path string) error {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function signature is now func pushIndex(ctx context.Context, displayStatus status.ManifestIndexCreateHandler, taggedHandler metadata.TaggedHandler, target oras.Target, desc ocispec.Descriptor, content []byte, ref string, extraRefs []string, path string) after resolving this comment. Still long param list.

// NewDiscardHandler returns a new discard handler.
func NewDiscardHandler() ManifestFetchHandler {
func NewDiscardHandler() discardHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we export discardHandler just like status.DiscardHandler?

Copy link
Contributor Author

@wangxiaoxuan273 wangxiaoxuan273 Oct 21, 2024

Choose a reason for hiding this comment

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

As NewDiscardHandler is a public method, its returned type should be exported. Exported all discard handlers in content, metadata and status packages.

if err != nil {
return fmt.Errorf("failed to open %q: %w", h.outputPath, err)
}
defer f.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to create an issue to track handling errors returned by Close() in all write paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #1516

Comment on lines +49 to +52
// ignore --pretty when output to a file
if outputPath != "" && outputPath != "-" {
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.

It is different from NewManifestFetchHandler. Is NewManifestFetchHandler wrong? /cc @qweeah

Copy link
Contributor

Choose a reason for hiding this comment

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

NewManifestFetchHandler is used only once by oras manifest fetch and such code snippet is in command code

// ignore --pretty when output to a file
case opts.outputPath != "" && opts.outputPath != "-":
opts.Pretty.Pretty = false
}

I think we need to remove it and change the flag reset to NewManifestFetchHandler too.

Copy link
Contributor Author

@wangxiaoxuan273 wangxiaoxuan273 Oct 21, 2024

Choose a reason for hiding this comment

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

Added pretty value reset in manifest fetch handler too.

cmd/oras/internal/display/metadata/discard.go Outdated Show resolved Hide resolved
Comment on lines 82 to 86
type manifestPackHandler interface {
OnIndexPacked(desc ocispec.Descriptor) error
OnIndexPushed(path string) error
OnCompleted(desc ocispec.Descriptor) error
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to merge this to ManifestIndexCreateHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this interface and now moved these three methods to index create handler.

Comment on lines 66 to 70
// referenceFetchHandler handles status output for reference fetch events.
type referenceFetchHandler interface {
OnFetching(manifestRef string) error
OnFetched(manifestRef string, desc ocispec.Descriptor) error
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to merge it to ManifestIndexCreateHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this interface and now moved these three methods to index create handler.

Comment on lines 53 to 54
IndexPromptFetching = "Fetching "
IndexPromptFetched = "Fetched "
Copy link
Contributor

Choose a reason for hiding this comment

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

Why those 2 are left?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As per offline discussion, added back "added, removed, merged, pushed, packed"

Xiaoxuan Wang added 2 commits October 14, 2024 14:55
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
@wangxiaoxuan273
Copy link
Contributor Author

As per offline discussion, added, merged, removed, pushed, packed should exist in both status and metadata output. And Status output is needed in the PR, metadata output can be added in a future pr when needed.

Xiaoxuan Wang added 2 commits October 21, 2024 15:24
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
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
4 participants