Normalize the json image/dir source (#180)

* normalize the json image/dir source

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

* update json image presenter golden file

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-09-25 14:07:38 -04:00 committed by GitHub
parent c46d004a3b
commit 49800b6747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 81 deletions

View File

@ -200,6 +200,9 @@
}
]
},
"name": {
"type": "string"
},
"originPackage": {
"type": "string"
},
@ -444,9 +447,15 @@
"source": {
"type": "string"
},
"sourceRpm": {
"type": "string"
},
"url": {
"type": "string"
},
"vendor": {
"type": "string"
},
"version": {
"type": "string"
}
@ -474,10 +483,14 @@
},
"type": "array"
},
"directory": {
"source": {
"properties": {
"target": {
"anyOf": [
{
"type": "string"
},
"image": {
{
"properties": {
"digest": {
"type": "string"
@ -526,9 +539,22 @@
],
"type": "object"
}
]
},
"type": {
"type": "string"
}
},
"required": [
"artifacts"
"target",
"type"
],
"type": "object"
}
},
"required": [
"artifacts",
"source"
],
"type": "object"
}

View File

@ -3,7 +3,7 @@ A "one-stop-shop" for helper utilities for all major functionality provided by c
Here is what the main execution path for syft does:
1. Parse a user image string to get a stereoscope image.Image object
1. Parse a user image string to get a stereoscope image.Source object
2. Invoke all catalogers to catalog the image, adding discovered packages to a single catalog object
3. Invoke a single presenter to show the contents of the catalog

View File

@ -1,16 +1,13 @@
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"`
Source Source `json:"source"`
}
func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
@ -18,15 +15,11 @@ func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
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)
src, err := NewSource(s)
if err != nil {
return Document{}, nil
}
doc.Source = src
for _, p := range catalog.Sorted() {
art, err := NewArtifact(p, s)

View File

@ -0,0 +1,30 @@
package json
import (
"fmt"
"github.com/anchore/syft/syft/scope"
)
type Source struct {
Type string `json:"type"`
Target interface{} `json:"target"`
}
func NewSource(s scope.Scope) (Source, error) {
srcObj := s.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
return Source{
Type: "image",
Target: NewImage(src),
}, nil
case scope.DirSource:
return Source{
Type: "directory",
Target: s.DirSrc.Path,
}, nil
default:
return Source{}, fmt.Errorf("unsupported source: %T", src)
}
}

View File

@ -23,5 +23,8 @@
]
}
],
"directory": "/some/path"
"source": {
"type": "directory",
"target": "/some/path"
}
}

View File

@ -29,7 +29,9 @@
]
}
],
"image": {
"source": {
"type": "image",
"target": {
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
@ -55,3 +57,4 @@
]
}
}
}