syft/syft/presenter/json/document.go
Alex Goodman 817ce61036
Add detailed location info to json artifact (#127)
* add detailed location info to json artifact

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* decompose json presenter

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
2020-08-07 10:05:16 -04:00

41 lines
828 B
Go

package json
import (
"fmt"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope"
)
type Document struct {
Artifacts []Artifact `json:"artifacts"`
Image *Image `json:"image,omitempty"`
Directory *string `json:"directory,omitempty"`
}
func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
doc := Document{
Artifacts: make([]Artifact, 0),
}
srcObj := s.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
doc.Image = NewImage(src)
case scope.DirSource:
doc.Directory = &s.DirSrc.Path
default:
return Document{}, fmt.Errorf("unsupported source: %T", src)
}
for _, p := range catalog.Sorted() {
art, err := NewArtifact(p, s)
if err != nil {
return Document{}, err
}
doc.Artifacts = append(doc.Artifacts, art)
}
return doc, nil
}