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 17 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 <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
@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 63.10160% with 69 lines in your changes missing coverage. Please review.

Project coverage is 84.86%. Comparing base (304a89f) to head (3a2fb99).

Files with missing lines Patch % Lines
cmd/oras/root/manifest/index/update.go 42.85% 12 Missing and 12 partials ⚠️
cmd/oras/root/manifest/index/create.go 39.13% 7 Missing and 7 partials ⚠️
cmd/oras/internal/display/status/discard.go 25.00% 12 Missing ⚠️
cmd/oras/internal/display/metadata/discard.go 42.85% 8 Missing ⚠️
cmd/oras/internal/display/status/text.go 75.00% 6 Missing ⚠️
...nal/display/metadata/text/manifest_index_update.go 85.71% 2 Missing and 1 partial ⚠️
...md/oras/internal/display/content/manifest_index.go 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   86.01%   84.86%   -1.15%     
==========================================
  Files         118      120       +2     
  Lines        4225     4348     +123     
==========================================
+ Hits         3634     3690      +56     
- Misses        353      399      +46     
- Partials      238      259      +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 <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
Signed-off-by: Xiaoxuan Wang <[email protected]>
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.

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 <[email protected]>
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

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

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

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.

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.

import (
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

type discard struct{}
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 export it just like other discard handlers?

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?

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?

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?

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