mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
create text-based presenters for dirs and imgs
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
268cfef0ee
commit
e38e3e9ff4
@ -10,25 +10,23 @@ import (
|
||||
|
||||
type Presenter struct {
|
||||
catalog *pkg.Catalog
|
||||
path string
|
||||
}
|
||||
|
||||
func NewPresenter(catalog *pkg.Catalog) *Presenter {
|
||||
func NewPresenter(catalog *pkg.Catalog, path string) *Presenter {
|
||||
return &Presenter{
|
||||
catalog: catalog,
|
||||
path: path,
|
||||
}
|
||||
}
|
||||
|
||||
type document struct {
|
||||
Artifacts []artifact `json:"artifacts"`
|
||||
}
|
||||
|
||||
type dir struct {
|
||||
Path string `json:"path"`
|
||||
Source string
|
||||
}
|
||||
|
||||
type source struct {
|
||||
FoundBy string `json:"foundBy"`
|
||||
Layer int `json:"layer"`
|
||||
Effects []string `json:"effects"`
|
||||
}
|
||||
|
||||
@ -44,9 +42,11 @@ type artifact struct {
|
||||
func (pres *Presenter) Present(output io.Writer) error {
|
||||
doc := document{
|
||||
Artifacts: make([]artifact, 0),
|
||||
Source: pres.path,
|
||||
}
|
||||
|
||||
// populate artifacts...
|
||||
// TODO: move this into a common package so that other text presenters can reuse
|
||||
for p := range pres.catalog.Enumerate() {
|
||||
art := artifact{
|
||||
Name: p.Name,
|
||||
@ -56,17 +56,9 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
Metadata: p.Metadata,
|
||||
}
|
||||
|
||||
// FIXME: there is no image in a dir-based scan
|
||||
for idx := range p.Source {
|
||||
// fileMetadata, err := pres.img.FileCatalog.Get(src)
|
||||
// if err != nil {
|
||||
// // TODO: test case
|
||||
// log.Errorf("could not get metadata from catalog (presenter=json): %+v", src)
|
||||
// }
|
||||
|
||||
srcObj := source{
|
||||
FoundBy: "FoundBy",
|
||||
Layer: 0,
|
||||
FoundBy: p.FoundBy,
|
||||
Effects: []string{}, // TODO
|
||||
}
|
||||
art.Sources[idx] = srcObj
|
||||
|
||||
45
imgbom/presenter/text/dirs/presenter.go
Normal file
45
imgbom/presenter/text/dirs/presenter.go
Normal file
@ -0,0 +1,45 @@
|
||||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
)
|
||||
|
||||
type Presenter struct {
|
||||
catalog *pkg.Catalog
|
||||
path string
|
||||
}
|
||||
|
||||
func NewPresenter(catalog *pkg.Catalog, path string) *Presenter {
|
||||
return &Presenter{
|
||||
catalog: catalog,
|
||||
path: path,
|
||||
}
|
||||
}
|
||||
|
||||
// Present is a method that is in charge of writing to an output buffer
|
||||
func (pres *Presenter) Present(output io.Writer) error {
|
||||
// init the tabular writer
|
||||
w := new(tabwriter.Writer)
|
||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
||||
fmt.Fprintln(w, fmt.Sprintf("[Path: %s]", pres.path))
|
||||
|
||||
// populate artifacts...
|
||||
// TODO: move this into a common package so that other text presenters can reuse
|
||||
for p := range pres.catalog.Enumerate() {
|
||||
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
||||
fmt.Fprintln(w, " Type:\t", p.Type.String())
|
||||
if p.Metadata != nil {
|
||||
fmt.Fprintf(w, " Metadata:\t%+v\n", p.Metadata)
|
||||
}
|
||||
fmt.Fprintln(w, " Found by:\t", p.FoundBy)
|
||||
fmt.Fprintln(w)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
53
imgbom/presenter/text/dirs/presenter_test.go
Normal file
53
imgbom/presenter/text/dirs/presenter_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package text
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||
|
||||
func TestTextPresenter(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
catalog := pkg.NewCatalog()
|
||||
|
||||
// populate catalog with test data
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-1",
|
||||
Version: "1.0.1",
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-2",
|
||||
Version: "2.0.1",
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
|
||||
pres := NewPresenter(catalog, "/some/path")
|
||||
|
||||
// run presenter
|
||||
err := pres.Present(&buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual := buffer.Bytes()
|
||||
|
||||
if *update {
|
||||
testutils.UpdateGoldenFileContents(t, actual)
|
||||
}
|
||||
|
||||
var expected = testutils.GetGoldenFileContents(t)
|
||||
|
||||
if !bytes.Equal(expected, actual) {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
||||
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[],"metadata":null},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[],"metadata":null}],"Source":"/some/path"}
|
||||
@ -0,0 +1,11 @@
|
||||
[Path: /some/path]
|
||||
[package-1]
|
||||
Version: 1.0.1
|
||||
Type: deb
|
||||
Found by:
|
||||
|
||||
[package-2]
|
||||
Version: 2.0.1
|
||||
Type: deb
|
||||
Found by:
|
||||
|
||||
61
imgbom/presenter/text/imgs/presenter.go
Normal file
61
imgbom/presenter/text/imgs/presenter.go
Normal file
@ -0,0 +1,61 @@
|
||||
package imgs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
||||
)
|
||||
|
||||
type Presenter struct {
|
||||
img *stereoscopeImg.Image
|
||||
catalog *pkg.Catalog
|
||||
}
|
||||
|
||||
func NewPresenter(img *stereoscopeImg.Image, catalog *pkg.Catalog) *Presenter {
|
||||
return &Presenter{
|
||||
img: img,
|
||||
catalog: catalog,
|
||||
}
|
||||
}
|
||||
|
||||
// Present is a method that is in charge of writing to an output buffer
|
||||
func (pres *Presenter) Present(output io.Writer) error {
|
||||
tags := make([]string, len(pres.img.Metadata.Tags))
|
||||
for idx, tag := range pres.img.Metadata.Tags {
|
||||
tags[idx] = tag.String()
|
||||
}
|
||||
|
||||
// init the tabular writer
|
||||
w := new(tabwriter.Writer)
|
||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
||||
|
||||
fmt.Fprintln(w, "[Image]")
|
||||
|
||||
for idx, l := range pres.img.Layers {
|
||||
fmt.Fprintln(w, " Layer:\t", idx)
|
||||
fmt.Fprintln(w, " Digest:\t", l.Metadata.Digest)
|
||||
fmt.Fprintln(w, " Size:\t", l.Metadata.Size)
|
||||
fmt.Fprintln(w, " MediaType:\t", l.Metadata.MediaType)
|
||||
fmt.Fprintln(w)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
// populate artifacts...
|
||||
// TODO: move this into a common package so that other text presenters can reuse
|
||||
for p := range pres.catalog.Enumerate() {
|
||||
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
||||
fmt.Fprintln(w, " Type:\t", p.Type.String())
|
||||
if p.Metadata != nil {
|
||||
fmt.Fprintf(w, " Metadata:\t%+v\n", p.Metadata)
|
||||
}
|
||||
fmt.Fprintln(w, " Found by:\t", p.FoundBy)
|
||||
fmt.Fprintln(w)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package text
|
||||
package imgs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user