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 12 commits into
base: main
Choose a base branch
from

Conversation

JeyJeyGao
Copy link
Contributor

@JeyJeyGao JeyJeyGao commented Jan 16, 2025

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

Test:

  • added E2E test

Resolves part of #1151

Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 88.37209% with 35 lines in your changes missing coverage. Please review.

Project coverage is 74.48%. Comparing base (2920cae) to head (0640165).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...notation/internal/display/metadata/tree/inspect.go 88.39% 9 Missing and 4 partials ⚠️
...notation/internal/display/metadata/json/inspect.go 89.52% 8 Missing and 3 partials ⚠️
internal/tree/tree.go 40.00% 4 Missing and 2 partials ⚠️
cmd/notation/inspect.go 70.58% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1150      +/-   ##
==========================================
+ Coverage   73.30%   74.48%   +1.18%     
==========================================
  Files          53       60       +7     
  Lines        3240     3363     +123     
==========================================
+ Hits         2375     2505     +130     
+ Misses        671      664       -7     
  Partials      194      194              

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

Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
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 <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
@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
"github.com/spf13/pflag"
)

type FormatType string
Copy link
Contributor

Choose a reason for hiding this comment

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

code style: require docs on exposed type

Comment on lines +60 to +68
func (f *Format) ApplyFlags(fs *pflag.FlagSet) {
var quotedAllowedTypes []string
for _, t := range f.allowedTypes {
quotedAllowedTypes = append(quotedAllowedTypes, fmt.Sprintf("'%s'", t))
}
usage := fmt.Sprintf("output format, options: %s", strings.Join(quotedAllowedTypes, ", "))
// apply flags
fs.StringVar(&f.FormatFlag, "output", f.FormatFlag, usage)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

note: we need to enhance this later in v2

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?

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.

OnReferenceResolved(reference, mediaType string)

// InspectSignature inspects a signature to get it ready to be rendered.
InspectSignature(digest string, envelopeMediaType string, sigEnvelope signature.Envelope) error
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is not updated as expected as #1150 (comment)

Note: It is OK to pass the related object which is not fully printed.

printer *output.Printer

root *tree.Node
cncfSigNode *tree.Node
Copy link
Contributor

Choose a reason for hiding this comment

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

The field name cncfSigNode is confusing. Let's consider a better name.

Comment on lines +55 to +56
// mediaType is a no-op for this handler.
func (h *InspectHandler) OnReferenceResolved(reference, mediaType string) {
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
// mediaType is a no-op for this handler.
func (h *InspectHandler) OnReferenceResolved(reference, mediaType string) {
func (h *InspectHandler) OnReferenceResolved(reference, _ string) {

Comment on lines +65 to +67
if h.root == nil || h.cncfSigNode == nil {
return fmt.Errorf("artifact reference is not set")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

They are not reachable, aren't they?

Comment on lines +74 to +76
if h.root == nil {
return fmt.Errorf("artifact reference is not set")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

They are not reachable, aren't they?

//
// mediaType is a no-op for this handler.
func (h *InspectHandler) OnReferenceResolved(reference, mediaType string) {
if h.root == nil {
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 unnecessary to check h.root.

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.

2 participants