diff --git a/templates/README.md b/templates/README.md new file mode 100644 index 00000000000..a9f521af0a7 --- /dev/null +++ b/templates/README.md @@ -0,0 +1,23 @@ +# Grype Templates + +This folder contains a set of "helper" go templates you can use for your own reports. + +Please feel free to extend and/or update the templates for your needs, be sure to contribute back into this folder any new templates! + +Current templates: + +
+. +├── README.md +└── table.tmpl ++ +## Table + +This template mimics the "default" table output of Grype, there are some drawbacks using the template vs the native output such as: + +- unsorted +- duplicate rows +- no (wont-fix) logic + +As you can see from the above list, it's not perfect but it's a start. diff --git a/templates/table.tmpl b/templates/table.tmpl new file mode 100644 index 00000000000..519cfef03d8 --- /dev/null +++ b/templates/table.tmpl @@ -0,0 +1,48 @@ +{{- $name_length := 4}} +{{- $installed_length := 9}} +{{- $fixed_in_length := 8}} +{{- $type_length := 4}} +{{- $vulnerability_length := 13}} +{{- $severity_length := 8}} +{{- range .Matches}} +{{- $temp_name_length := (len .Artifact.Name)}} +{{- $temp_installed_length := (len .Artifact.Version)}} +{{- $temp_fixed_in_length := (len (.Vulnerability.Fix.Versions | join " "))}} +{{- $temp_type_length := (len .Artifact.Type)}} +{{- $temp_vulnerability_length := (len .Vulnerability.ID)}} +{{- $temp_severity_length := (len .Vulnerability.Severity)}} +{{- if (lt $name_length $temp_name_length) }} +{{- $name_length = $temp_name_length}} +{{- end}} +{{- if (lt $installed_length $temp_installed_length) }} +{{- $installed_length = $temp_installed_length}} +{{- end}} +{{- if (lt $fixed_in_length $temp_fixed_in_length) }} +{{- $fixed_in_length = $temp_fixed_in_length}} +{{- end}} +{{- if (lt $type_length $temp_type_length) }} +{{- $type_length = $temp_type_length}} +{{- end}} +{{- if (lt $vulnerability_length $temp_vulnerability_length) }} +{{- $vulnerability_length = $temp_vulnerability_length}} +{{- end}} +{{- if (lt $severity_length $temp_severity_length) }} +{{- $severity_length = $temp_severity_length}} +{{- end}} +{{- end}} +{{- $name_length = add $name_length 2}} +{{- $pad_name := repeat (int $name_length) " "}} +{{- $installed_length = add $installed_length 2}} +{{- $pad_installed := repeat (int $installed_length) " "}} +{{- $fixed_in_length = add $fixed_in_length 2}} +{{- $pad_fixed_in := repeat (int $fixed_in_length) " "}} +{{- $type_length = add $type_length 2}} +{{- $pad_type := repeat (int $type_length) " "}} +{{- $vulnerability_length = add $vulnerability_length 2}} +{{- $pad_vulnerability := repeat (int $vulnerability_length) " "}} +{{- $severity_length = add $severity_length 2}} +{{- $pad_severity := repeat (int $severity_length) " "}} +{{cat "NAME" (substr 5 (int $name_length) $pad_name)}}{{cat "INSTALLED" (substr 10 (int $installed_length) $pad_installed)}}{{cat "FIXED-IN" (substr 9 (int $fixed_in_length) $pad_fixed_in)}}{{cat "TYPE" (substr 5 (int $type_length) $pad_type)}}{{cat "VULNERABILITY" (substr 14 (int $vulnerability_length) $pad_vulnerability)}}{{cat "SEVERITY" (substr 9 (int $severity_length) $pad_severity)}} +{{- range .Matches}} +{{cat .Artifact.Name (substr (int (add (len .Artifact.Name) 1)) (int $name_length) $pad_name)}}{{cat .Artifact.Version (substr (int (add (len .Artifact.Version) 1)) (int $installed_length) $pad_installed)}}{{cat (.Vulnerability.Fix.Versions | join " ") (substr (int (add (len (.Vulnerability.Fix.Versions | join " ")) 1)) (int $fixed_in_length) $pad_fixed_in)}}{{cat .Artifact.Type (substr (int (add (len .Artifact.Type) 1)) (int $type_length) $pad_type)}}{{cat .Vulnerability.ID (substr (int (add (len .Vulnerability.ID) 1)) (int $vulnerability_length) $pad_vulnerability)}}{{cat .Vulnerability.Severity (substr (int (add (len .Vulnerability.Severity) 1)) (int $severity_length) $pad_severity)}} +{{- end}} \ No newline at end of file