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: extract inspect rendering logic to be display handlers #1150

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

Conversation

JeyJeyGao
Copy link
Contributor

@JeyJeyGao JeyJeyGao commented Jan 16, 2025

Depends on spec change: #1156

Refactor:

  • Extract output formatting logic from the inspect command layer to an isolated display handler layer for processing rendering.
  • Add json and tree inspect handlers.

Fix:

  • For tree output, make the key names with multiple words separated by space characters rather than capitalizing the words, which is defined in the inspect command spec.
  • For json output, default to rendering time in RFC3339 with nanoseconds (Notation expiry, signing time and certificate expiry are accurate to 1 second. Timestamp RFC 3161 can have fraction-of-second time value).

E2E Test:

  • inspect signature with timestamp, signature expiry and user metadata (text, json)
  • inspect signatures with invalid timestamp (test, json)
  • inspect with -o shorthand.

Resolves part of #1151

Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 88.69863% with 33 lines in your changes missing coverage. Please review.

Project coverage is 74.71%. Comparing base (2920cae) to head (da16190).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...notation/internal/display/metadata/tree/inspect.go 87.12% 9 Missing and 4 partials ⚠️
...notation/internal/display/metadata/json/inspect.go 89.32% 8 Missing and 3 partials ⚠️
...md/notation/internal/display/metadata/tree/tree.go 40.00% 4 Missing and 2 partials ⚠️
cmd/notation/inspect.go 85.71% 0 Missing and 2 partials ⚠️
internal/envelope/envelope.go 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1150      +/-   ##
==========================================
+ Coverage   73.30%   74.71%   +1.41%     
==========================================
  Files          53       59       +6     
  Lines        3240     3338      +98     
==========================================
+ Hits         2375     2494     +119     
+ Misses        671      651      -20     
+ Partials      194      193       -1     

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

Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
cmd/notation/internal/output/print.go Outdated Show resolved Hide resolved
cmd/notation/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/interface.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/interface.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/interface.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/model/signature.go Outdated Show resolved Hide resolved
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
@JeyJeyGao JeyJeyGao marked this pull request as ready for review January 20, 2025 08:39
@JeyJeyGao JeyJeyGao changed the title refactor: add inspect display handler refactor: extract inspect rendering logic to be display handlers Jan 20, 2025
cmd/notation/internal/option/format.go Show resolved Hide resolved
cmd/notation/internal/option/format.go Outdated Show resolved Hide resolved
Comment on lines +36 to +39
// Common option struct.
type Common 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.

Will you refactor --debug in later PRs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is part of this issue #1151

Comment on lines 68 to 73
if err := opts.Format.Parse(cmd); err != nil {
return err
}
if err := opts.Common.Parse(cmd); err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I noticed that, but I chose to include it in future PRs. It is part of this issue: #1151

cmd/notation/internal/display/metadata/interface.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/tree/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/tree/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/tree/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/tree/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/tree/inspect.go Outdated Show resolved Hide resolved
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
@JeyJeyGao JeyJeyGao requested a review from shizhMSFT January 21, 2025 01:54
@JeyJeyGao JeyJeyGao marked this pull request as draft January 21, 2025 03:35
Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
@JeyJeyGao JeyJeyGao marked this pull request as ready for review January 21, 2025 06:38
Signed-off-by: Junjie Gao <[email protected]>
@JeyJeyGao JeyJeyGao requested a review from Two-Hearts January 21, 2025 07:42
cmd/notation/internal/output/print.go Outdated Show resolved Hide resolved
cmd/notation/internal/output/print.go Outdated Show resolved Hide resolved
cmd/notation/internal/output/print.go Outdated Show resolved Hide resolved
cmd/notation/internal/output/print.go Outdated Show resolved Hide resolved
cmd/notation/internal/display/metadata/json/inspect.go Outdated Show resolved Hide resolved
cmd/notation/internal/option/format.go Outdated Show resolved Hide resolved
cmd/notation/internal/output/json.go Outdated Show resolved Hide resolved
@@ -61,21 +61,21 @@ func ValidatePayloadContentType(payload *signature.Payload) error {

// DescriptorFromPayload parses a signature payload and returns the descriptor
// that was signed. Note: the descriptor was signed but may not be trusted
func DescriptorFromSignaturePayload(payload *signature.Payload) (*ocispec.Descriptor, error) {
func DescriptorFromSignaturePayload(payload *signature.Payload) (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.

Any reason to make this change? It seems not related to this PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was suggested here: #1150 (comment). I think this is a convention of ORAS.

Copy link
Contributor Author

@JeyJeyGao JeyJeyGao Jan 23, 2025

Choose a reason for hiding this comment

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

OK, I removed the update to envelope.go as it is not closely related to this refactor work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Two-Hearts ocispec.Descriptor should be used as a structure is a convention start from image-spec. We should follow the same patten: https://pkg.go.dev/github.com/opencontainers/[email protected]/specs-go/v1#Manifest

Signed-off-by: Junjie Gao <[email protected]>
Signed-off-by: Junjie Gao <[email protected]>
shizhMSFT
shizhMSFT previously approved these changes Jan 22, 2025
Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Junjie Gao <[email protected]>
limitations under the License.
*/

package output
Copy link
Contributor

Choose a reason for hiding this comment

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

Now we have a folder called display, then another folder called output.
I have a feeling that we are making the code structure more and more complicated. And this may bring in difficulty for other developers to read through and understand such complicity 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.

Refactored and moved output to display/output

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. We have a folder called tree, then another folder with the same name under cmd/notation/internal/display/metadata. Can we try to reduce such complicity during the design phase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored.

Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

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

I noticed that handlers are grouped by command names instead of format types. Although both are acceptable, grouping by format types can have more benefits in the long run, especially we have growing commands but fixed set of format types.

One important point is that grouping by format types make developers easier to have consistent output for a certain format type across all commands.

Also, it is easier to have common utility functions without creating a utility package, which is discouraged.

@JeyJeyGao
Copy link
Contributor Author

I noticed that handlers are grouped by command names instead of format types. Although both are acceptable, grouping by format types can have more benefits in the long run, especially we have growing commands but fixed set of format types.

One important point is that grouping by format types make developers easier to have consistent output for a certain format type across all commands.

Also, it is easier to have common utility functions without creating a utility package, which is discouraged.

Updated to group the handlers by format types.

func NewInpsectHandler(printer *output.Printer, format option.Format) (metadata.InspectHandler, error) {
// NewMetadataInpsectHandler creates a new InspectHandler based on the output
// format.
func NewMetadataInpsectHandler(printer *output.Printer, format option.Format) (metadata.InspectHandler, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call it NewInpsectHandler.

In the package display, the function name is intended to be

func New{{.CommandName}}Handler({{.parameters}}) (status.{{.CommandName}}Handler, metadata.{{.CommandName}}Handler, error)

although we dont have status support yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to NewInpsectHandler

shizhMSFT
shizhMSFT previously approved these changes Jan 23, 2025
Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

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

LGTM

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.

3 participants