From c36f6a7f46f2af7d0cf2761421c0cbd898d127f7 Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Tue, 4 Jun 2024 11:36:08 -0400 Subject: [PATCH 1/2] Don't wrap image ref in text output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wrapping only wraps on whitespace, so before this patch we'd end up with output like this for long image refs, which IMO looks untidy. Let's just have an over-long line rather than wrapping it like that. ``` › [Warning] cve.unpatched_cve_warnings ImageRef: quay.io/redhat-user-workloads/somelong-app-name/some-long-component-name@etc... ``` There are a few ways to do this it, but I felt like the indent helper works best with the agressive space eating done by {{- and -}}, and keeps it consistent with the other things we're outputing. This is a followup tweak for the functionality introduced in #1656. --- .../applicationsnapshot/templates/_results.tmpl | 2 +- internal/utils/templates.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/applicationsnapshot/templates/_results.tmpl b/internal/applicationsnapshot/templates/_results.tmpl index be2d6017f..a5963debb 100644 --- a/internal/applicationsnapshot/templates/_results.tmpl +++ b/internal/applicationsnapshot/templates/_results.tmpl @@ -16,7 +16,7 @@ {{- colorIndicator $type }} {{ colorText $type (printf "[%s] %s" $type .Metadata.code) }}{{ nl -}} {{- if $imageRef -}} - {{- indentWrap $indent $wrap (printf "ImageRef: %s" $imageRef ) }}{{ nl -}} + {{- indent $indent (printf "ImageRef: %s" $imageRef ) }}{{ nl -}} {{- end -}} {{/* For a success the message is generally just "Pass" so don't show it */}} diff --git a/internal/utils/templates.go b/internal/utils/templates.go index 64b815553..9f5c2a706 100644 --- a/internal/utils/templates.go +++ b/internal/utils/templates.go @@ -125,10 +125,19 @@ func wrap(width int, s string) string { return wordwrap.WrapString(s, uint(width)) } +// A string with n spaces +func indentStr(n int) string { + return strings.Repeat(" ", n) +} + +// Indent a certain number of spaces +func indent(n int, s string) string { + return indentStr(n) + s +} + // Indent with spaces and also wrap -func indentWrap(indent int, width int, s string) string { - indentStr := strings.Repeat(" ", indent) - return indentStr + strings.ReplaceAll(wrap(width-indent, s), "\n", "\n"+indentStr) +func indentWrap(n int, width int, s string) string { + return indent(n, strings.ReplaceAll(wrap(width-n, s), "\n", "\n"+indentStr(n))) } // A way to assemble a map from keys and values in a template @@ -159,6 +168,7 @@ var templateHelpers = template.FuncMap{ "indicator": indicator, "colorIndicator": colorIndicator, "wrap": wrap, + "indent": indent, "indentWrap": indentWrap, "toMap": toMap, "nl": nl, From faba9071647226e17847713337853608afd48619 Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Wed, 5 Jun 2024 16:46:28 -0400 Subject: [PATCH 2/2] Remove usless error in template func return For some reason I initially thought template functions needed that signature. --- internal/utils/templates.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/utils/templates.go b/internal/utils/templates.go index 9f5c2a706..34fceaf16 100644 --- a/internal/utils/templates.go +++ b/internal/utils/templates.go @@ -157,8 +157,8 @@ func toMap(values ...interface{}) (map[string]interface{}, error) { } // Can make it easier to get the right number of line breaks -func nl() (string, error) { - return "\n", nil +func nl() string { + return "\n" } // For use in template.Funcs above