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

Human readable output #1631

Merged
merged 3 commits into from
May 23, 2024

Conversation

simonbaird
Copy link
Member

@simonbaird simonbaird commented May 16, 2024

Cleaned up and ready for serious review now.

I think this could be merged as is, but it does need some extra work on the following:

  • Instead of top level grouping by component, it should be top level grouping by violation/warning/success.
  • There should be a way to disable/enable colors, ideally with auto detection of terminal color support (as @zregvart suggested in the review here).

Those could be in followup PRs, or could be included in this one.

Ref: EC-563.

Copy link

codecov bot commented May 16, 2024

Codecov Report

Attention: Patch coverage is 45.33333% with 41 lines in your changes are missing coverage. Please review.

Project coverage is 80.14%. Comparing base (d4a939b) to head (1f08226).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1631      +/-   ##
==========================================
- Coverage   80.69%   80.14%   -0.56%     
==========================================
  Files          64       65       +1     
  Lines        4704     4779      +75     
==========================================
+ Hits         3796     3830      +34     
- Misses        908      949      +41     
Flag Coverage Δ
generative 80.14% <45.33%> (-0.56%) ⬇️
integration 80.14% <45.33%> (-0.56%) ⬇️
unit 80.14% <45.33%> (-0.56%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
cmd/validate/image.go 93.58% <100.00%> (+0.14%) ⬆️
internal/applicationsnapshot/report.go 70.34% <0.00%> (-1.68%) ⬇️
internal/utils/helpers.go 67.56% <0.00%> (-14.40%) ⬇️
internal/utils/templates.go 52.94% <52.94%> (ø)

JSON = "json"
YAML = "yaml"
APPSTUDIO = "appstudio"
Text = "text"
Copy link
Member

Choose a reason for hiding this comment

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

As you're refactoring, we could define a type OutputFormatType string and have these typed like that...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I was thinking about that. Maybe in another commit since I merged #1640 already.

@@ -289,6 +309,67 @@ func generateMarkdownSummary(r *Report) ([]byte, error) {
return markdownBuffer.Bytes(), nil
}

func generateTextReport(r *Report) ([]byte, error) {

mainTemplate := hd.Doc(`
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps use embed and have the template in a separate file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's done in the latest revision.

Comment on lines 335 to 336
// For convenience since juggling the {{- and -}} gets tricky
"NL": "\n",
Copy link
Member

Choose a reason for hiding this comment

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

Yes, though much less so once you get into the rhythm of using only/mostly {{-, see the templates for the docs for inspiration

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I didn't end up using it 😅

@@ -155,3 +155,19 @@ func HasSuffix(str string, extensions []string) bool {
func HasJsonOrYamlExt(str string) bool {
return HasSuffix(str, []string{".json", ".yaml", ".yml"})
}

func ansiColorText(str string, color int) string {
Copy link
Member

Choose a reason for hiding this comment

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

We use https://pkg.go.dev/github.com/doiit/picocolors (that no longer seems to exist) in the acceptance tests. We might want to think about disabling colors based on the environment, picocolors does something like this:

func isColorSupported() bool {
        noColor := os.Getenv("NO_COLOR") != "" || contains(os.Args, "--no-color")
        forceColor := os.Getenv("FORCE_COLOR") != "" || os.Getenv("CLICOLOR_FORCE") != "" || contains(os.Args, "--color")
        terminal := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) || os.Getenv("TERM") == "dumb"
        ci := os.Getenv("CI") != ""

        return !noColor && (forceColor || terminal || ci)
}

There are terminfo packages we could also use to se if the terminal has color support (think CI)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

Third commit has this now.

t = template.Must(t.Parse(mainTemplate))

for k, v := range partials {
t = template.Must(t.Parse(fmt.Sprintf("{{- define \"%s\" -}}\n%s\n{{- end -}}", k, hd.Doc(v))))
Copy link
Member

Choose a reason for hiding this comment

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

Could put the nested templates in that same file that gets embedded, or use https://pkg.go.dev/text/template#ParseFS with embed to keep them in separate files

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I discovered ParseFS, using that now with embed.

@@ -72,6 +72,9 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
}{
strict: true,
}

validOutputFormats := applicationsnapshot.OutputFormats
Copy link
Member

Choose a reason for hiding this comment

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

👍

I'll be using this in an upcoming commit for text format output.

(Creating in a separate commit for easier review.)

Ref: https://issues.redhat.com/browse/EC-563
Use the new templating functionality from the previous commit.

Currently it's split up by component, but that's likely to change in
future.

Ref: https://issues.redhat.com/browse/EC-563
@simonbaird simonbaird marked this pull request as ready for review May 21, 2024 19:22
@simonbaird simonbaird force-pushed the plain-text-output branch 2 times, most recently from 6fe516d to 7a0cc64 Compare May 21, 2024 21:49
cmd.Flags().BoolVar(&data.noColor, "no-color", data.info, hd.Doc(`
Disable color when using text output even when the current terminal supports it`))

cmd.Flags().BoolVar(&data.forceColor, "force-color", data.info, hd.Doc(`
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps just --color?

Suggested change
cmd.Flags().BoolVar(&data.forceColor, "force-color", data.info, hd.Doc(`
cmd.Flags().BoolVar(&data.forceColor, "color", data.info, hd.Doc(`

@simonbaird simonbaird merged commit 5585270 into enterprise-contract:main May 23, 2024
9 of 10 checks passed
simonbaird added a commit to simonbaird/ec-cli that referenced this pull request May 23, 2024
The new templating helpers were introduced in enterprise-contract#1631. I want to use
the same technique in some older templating.

This required a little refactor of the helpers because we want to be
able to use io.Writer directly rather than write to a buffer.

Also add some extra unit tests.
simonbaird added a commit to simonbaird/ec-cli that referenced this pull request May 23, 2024
The new templating helpers were introduced in enterprise-contract#1631. I want to use
the same technique in some older templating.

This required a little refactor of the helpers because we want to be
able to use io.Writer directly rather than write to a buffer.

Also add some extra unit tests.
simonbaird added a commit to simonbaird/ec-cli that referenced this pull request May 23, 2024
simonbaird added a commit to simonbaird/ec-cli that referenced this pull request May 23, 2024
The new templating helpers were introduced in enterprise-contract#1631. I want to use
the same technique in some older templating.

This required a little refactor of the helpers because we want to be
able to use io.Writer directly rather than write to a buffer.

Also add some extra unit tests.
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.

None yet

2 participants