json: update the document to include distro information

Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
Alfredo Deza 2020-11-05 16:04:38 -05:00
parent 3699a917fd
commit 1e79986188
4 changed files with 28 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package json package json
import ( import (
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope" "github.com/anchore/syft/syft/scope"
) )
@ -8,9 +9,16 @@ import (
type Document struct { type Document struct {
Artifacts []Artifact `json:"artifacts"` Artifacts []Artifact `json:"artifacts"`
Source Source `json:"source"` Source Source `json:"source"`
Distro Distribution `json:"distro"`
} }
func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) { // Distritbution provides information about a detected Linux Distribution
type Distribution struct {
Name string `json:"name"`
Version string `json:"version"`
}
func NewDocument(catalog *pkg.Catalog, s scope.Scope, d distro.Distro) (Document, error) {
doc := Document{ doc := Document{
Artifacts: make([]Artifact, 0), Artifacts: make([]Artifact, 0),
} }
@ -20,6 +28,14 @@ func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
return Document{}, nil return Document{}, nil
} }
doc.Source = src doc.Source = src
distroName := d.Name()
if distroName == "UnknownDistroType" {
distroName = ""
}
doc.Distro = Distribution{
Name: distroName,
Version: d.FullVersion(),
}
for _, p := range catalog.Sorted() { for _, p := range catalog.Sorted() {
art, err := NewArtifact(p, s) art, err := NewArtifact(p, s)

View File

@ -1,6 +1,8 @@
package json package json
import "github.com/anchore/syft/syft/scope" import (
"github.com/anchore/syft/syft/scope"
)
type Image struct { type Image struct {
Layers []Layer `json:"layers"` Layers []Layer `json:"layers"`

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope" "github.com/anchore/syft/syft/scope"
) )
@ -11,17 +12,19 @@ import (
type Presenter struct { type Presenter struct {
catalog *pkg.Catalog catalog *pkg.Catalog
scope scope.Scope scope scope.Scope
distro distro.Distro
} }
func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter { func NewPresenter(catalog *pkg.Catalog, s scope.Scope, d distro.Distro) *Presenter {
return &Presenter{ return &Presenter{
catalog: catalog, catalog: catalog,
scope: s, scope: s,
distro: d,
} }
} }
func (pres *Presenter) Present(output io.Writer) error { func (pres *Presenter) Present(output io.Writer) error {
doc, err := NewDocument(pres.catalog, pres.scope) doc, err := NewDocument(pres.catalog, pres.scope, pres.distro)
if err != nil { if err != nil {
return err return err
} }

View File

@ -28,7 +28,7 @@ type Presenter interface {
func GetPresenter(option Option, s scope.Scope, catalog *pkg.Catalog, d *distro.Distro) Presenter { func GetPresenter(option Option, s scope.Scope, catalog *pkg.Catalog, d *distro.Distro) Presenter {
switch option { switch option {
case JSONPresenter: case JSONPresenter:
return json.NewPresenter(catalog, s) return json.NewPresenter(catalog, s, *d)
case TextPresenter: case TextPresenter:
return text.NewPresenter(catalog, s) return text.NewPresenter(catalog, s)
case TablePresenter: case TablePresenter: