mirror of
https://github.com/anchore/syft.git
synced 2026-11-02 15:36:41 +01:00
finalize json output & add schema (#118)
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/anchore/syft/internal/log"
|
||||
"github.com/anchore/syft/syft/pkg"
|
||||
"github.com/anchore/syft/syft/scope"
|
||||
)
|
||||
@@ -24,36 +23,35 @@ func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter {
|
||||
|
||||
type document struct {
|
||||
Artifacts []artifact `json:"artifacts"`
|
||||
Image image `json:"image"`
|
||||
Source string
|
||||
Image *image `json:"image,omitempty"`
|
||||
Directory *string `json:"directory,omitempty"`
|
||||
}
|
||||
|
||||
type image struct {
|
||||
Layers []layer `json:"layers"`
|
||||
Size int64 `json:"size"`
|
||||
Digest string `json:"digest"`
|
||||
MediaType string `json:"mediaType"`
|
||||
MediaType string `json:"media-type"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
type layer struct {
|
||||
MediaType string `json:"mediaType"`
|
||||
MediaType string `json:"media-type"`
|
||||
Digest string `json:"digest"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type source struct {
|
||||
FoundBy string `json:"foundBy"`
|
||||
Effects []string `json:"effects"`
|
||||
FoundBy string `json:"found-by"`
|
||||
Locations []string `json:"locations"`
|
||||
}
|
||||
|
||||
type artifact struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Type string `json:"type"`
|
||||
Cataloger string `json:"cataloger"`
|
||||
Sources []source `json:"sources"`
|
||||
Metadata interface{} `json:"metadata,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Type string `json:"type"`
|
||||
Sources []source `json:"sources"`
|
||||
Metadata interface{} `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
func (pres *Presenter) Present(output io.Writer) error {
|
||||
@@ -69,15 +67,25 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
for idx, tag := range src.Img.Metadata.Tags {
|
||||
tags[idx] = tag.String()
|
||||
}
|
||||
doc.Image = image{
|
||||
doc.Image = &image{
|
||||
Digest: src.Img.Metadata.Digest,
|
||||
Size: src.Img.Metadata.Size,
|
||||
MediaType: string(src.Img.Metadata.MediaType),
|
||||
Tags: tags,
|
||||
Layers: make([]layer, len(src.Img.Layers)),
|
||||
}
|
||||
|
||||
// populate image metadata
|
||||
for idx, l := range src.Img.Layers {
|
||||
doc.Image.Layers[idx] = layer{
|
||||
MediaType: string(l.Metadata.MediaType),
|
||||
Digest: l.Metadata.Digest,
|
||||
Size: l.Metadata.Size,
|
||||
}
|
||||
}
|
||||
|
||||
case scope.DirSource:
|
||||
doc.Source = pres.scope.DirSrc.Path
|
||||
doc.Directory = &pres.scope.DirSrc.Path
|
||||
default:
|
||||
return fmt.Errorf("unsupported source: %T", src)
|
||||
}
|
||||
@@ -93,8 +101,8 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
|
||||
for idx := range p.Source {
|
||||
srcObj := source{
|
||||
FoundBy: p.FoundBy,
|
||||
Effects: []string{}, // TODO
|
||||
FoundBy: p.FoundBy,
|
||||
Locations: []string{string(p.Source[idx].Path)},
|
||||
}
|
||||
art.Sources[idx] = srcObj
|
||||
}
|
||||
@@ -102,11 +110,9 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
doc.Artifacts = append(doc.Artifacts, art)
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(&doc)
|
||||
if err != nil {
|
||||
log.Errorf("failed to marshal json (presenter=json): %w", err)
|
||||
}
|
||||
|
||||
_, err = output.Write(bytes)
|
||||
return err
|
||||
enc := json.NewEncoder(output)
|
||||
// prevent > and < from being escaped in the payload
|
||||
enc.SetEscapeHTML(false)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(&doc)
|
||||
}
|
||||
|
||||
@@ -109,7 +109,4 @@ func TestJsonImgsPresenter(t *testing.T) {
|
||||
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
||||
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
// TODO: add me back in when there is a JSON schema
|
||||
// validateAgainstV1Schema(t, string(actual))
|
||||
}
|
||||
|
||||
@@ -1 +1,17 @@
|
||||
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[]},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[]}],"image":{"layers":null,"size":0,"digest":"","mediaType":"","tags":null},"Source":"/some/path"}
|
||||
{
|
||||
"artifacts": [
|
||||
{
|
||||
"name": "package-1",
|
||||
"version": "1.0.1",
|
||||
"type": "deb",
|
||||
"sources": []
|
||||
},
|
||||
{
|
||||
"name": "package-2",
|
||||
"version": "2.0.1",
|
||||
"type": "deb",
|
||||
"sources": []
|
||||
}
|
||||
],
|
||||
"directory": "/some/path"
|
||||
}
|
||||
|
||||
@@ -1 +1,55 @@
|
||||
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","effects":[]}]},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","effects":[]}]}],"image":{"layers":[{"mediaType":"","digest":"","size":0},{"mediaType":"","digest":"","size":0},{"mediaType":"","digest":"","size":0}],"size":65,"digest":"sha256:3c53d2d891940f8d8e95acb77b58752f54dc5de9d91d19dd90ced2db76256cea","mediaType":"application/vnd.docker.distribution.manifest.v2+json","tags":["anchore-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7"]},"Source":""}
|
||||
{
|
||||
"artifacts": [
|
||||
{
|
||||
"name": "package-1",
|
||||
"version": "1.0.1",
|
||||
"type": "deb",
|
||||
"sources": [
|
||||
{
|
||||
"found-by": "",
|
||||
"locations": [
|
||||
"/somefile-1.txt"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "package-2",
|
||||
"version": "2.0.1",
|
||||
"type": "deb",
|
||||
"sources": [
|
||||
{
|
||||
"found-by": "",
|
||||
"locations": [
|
||||
"/somefile-2.txt"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"image": {
|
||||
"layers": [
|
||||
{
|
||||
"media-type": "application/vnd.docker.image.rootfs.diff.tar.gzip",
|
||||
"digest": "sha256:056c0789fa9ad629ceae6d09713fb035f84115af3c4a88a43aa60f13bc683053",
|
||||
"size": 22
|
||||
},
|
||||
{
|
||||
"media-type": "application/vnd.docker.image.rootfs.diff.tar.gzip",
|
||||
"digest": "sha256:b461c48116592c570a66fed71d5b09662a8172e168b7938cf317af47872cdc9b",
|
||||
"size": 16
|
||||
},
|
||||
{
|
||||
"media-type": "application/vnd.docker.image.rootfs.diff.tar.gzip",
|
||||
"digest": "sha256:00b80053e05c01da485015610d288ce3185fac00d251e2ada02b45a7a7c5f589",
|
||||
"size": 27
|
||||
}
|
||||
],
|
||||
"size": 65,
|
||||
"digest": "sha256:3c53d2d891940f8d8e95acb77b58752f54dc5de9d91d19dd90ced2db76256cea",
|
||||
"media-type": "application/vnd.docker.distribution.manifest.v2+json",
|
||||
"tags": [
|
||||
"anchore-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,4 @@ func TestTablePresenter(t *testing.T) {
|
||||
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
||||
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
// TODO: add me back in when there is a JSON schema
|
||||
// validateAgainstV1Schema(t, string(actual))
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||
var update = flag.Bool("update", false, "update the *.golden files for text presenters")
|
||||
|
||||
func TestTextDirPresenter(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
Reference in New Issue
Block a user