-
Notifications
You must be signed in to change notification settings - Fork 23
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
Human readable output #1631
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
JSON = "json" | ||
YAML = "yaml" | ||
APPSTUDIO = "appstudio" | ||
Text = "text" |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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(` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
// For convenience since juggling the {{- and -}} gets tricky | ||
"NL": "\n", |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😅
internal/utils/helpers.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah good idea.
There was a problem hiding this comment.
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)))) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
cmd/validate/image.go
Outdated
@@ -72,6 +72,9 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command { | |||
}{ | |||
strict: true, | |||
} | |||
|
|||
validOutputFormats := applicationsnapshot.OutputFormats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
5db7690
to
b5349fc
Compare
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
b5349fc
to
a02cec8
Compare
6fe516d
to
7a0cc64
Compare
cmd/validate/image.go
Outdated
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(` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just --color
?
cmd.Flags().BoolVar(&data.forceColor, "force-color", data.info, hd.Doc(` | |
cmd.Flags().BoolVar(&data.forceColor, "color", data.info, hd.Doc(` |
7a0cc64
to
1f08226
Compare
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.
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.
Probably should have been included in enterprise-contract#1631.
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.
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:
Those could be in followup PRs, or could be included in this one.
Ref: EC-563.