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

Use the json schema as input for templating #2542

Merged
merged 2 commits into from
Jan 25, 2024

Conversation

wagoodman
Copy link
Contributor

The template functionality today uses the go struct fields from the syft/format/syftjson/model directory as input, which means that in order to write a template you need to read the syft source code. For instance:

"Package","Version Installed", "Found by"
{{- range .Artifacts}}
"{{.Name}}","{{.Version}}","{{.FoundBy}}"
{{- end}}
  • Artifacts approximates to artifacts in the json schema
  • Name approximates to name in the json schema
  • Version approximates to version in the json schema
  • FoundBy approximates to foundBy in the json schema

This change adapts the template input to require the same fields as what you will find in the JSON output of syft (exactly what's in the json schema):

"Package","Version Installed", "Found by"
{{- range .artifacts}}
"{{.name}}","{{.version}}","{{.foundBy}}"
{{- end}}

Caveats are that struct helpers cannot be used on maps, which is what ultimately is used for templating. This means that some alterations will need to be made, for example replacing hasField for index:

2,3c2,3
< {{- range .Artifacts}}
< "{{.Name}}","{{.Version}}","{{.FoundBy}}","{{ if hasField .Metadata "Author" }}{{.Metadata.Author}}{{ else }}NO AUTHOR SUPPLIED{{end}}"
---
> {{- range .artifacts}}
> "{{.name}}","{{.version}}","{{.foundBy}}","{{ if index .metadata "author" }}{{.metadata.author}}{{ else }}NO AUTHOR SUPPLIED{{end}}"

Since this will impact all templates a new SYFT_FORMAT_TEMPLATE_LEGACY/format.template.legacy option has been provided that, when enabled, will use the original struct-approach.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman added the breaking-change Change is not backwards compatible label Jan 24, 2024
@wagoodman wagoodman added this to the Syft 1.0 milestone Jan 24, 2024
@wagoodman wagoodman requested a review from a team January 24, 2024 21:06
@wagoodman wagoodman marked this pull request as ready for review January 24, 2024 21:13
@wagoodman wagoodman self-assigned this Jan 24, 2024
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman enabled auto-merge (squash) January 25, 2024 13:50
@wagoodman wagoodman merged commit a32b8d7 into main Jan 25, 2024
11 checks passed
@wagoodman wagoodman deleted the use-json-schema-for-template branch January 25, 2024 14:00
GijsCalis pushed a commit to GijsCalis/syft that referenced this pull request Feb 19, 2024
* use the json schema as input for templating

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix cli tests

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Change is not backwards compatible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants