mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
presenters: abstract into text only
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
@@ -1,50 +1,53 @@
|
||||
package imgs
|
||||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
||||
"github.com/anchore/imgbom/imgbom/scope"
|
||||
)
|
||||
|
||||
type Presenter struct {
|
||||
img *stereoscopeImg.Image
|
||||
catalog *pkg.Catalog
|
||||
scope scope.Scope
|
||||
}
|
||||
|
||||
func NewPresenter(img *stereoscopeImg.Image, catalog *pkg.Catalog) *Presenter {
|
||||
func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter {
|
||||
return &Presenter{
|
||||
img: img,
|
||||
catalog: catalog,
|
||||
scope: s,
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
srcObj := pres.scope.Source()
|
||||
|
||||
fmt.Fprintln(w, "[Image]")
|
||||
switch src := srcObj.(type) {
|
||||
case scope.DirSource:
|
||||
fmt.Fprintln(w, fmt.Sprintf("[Path: %s]", src.Path))
|
||||
case scope.ImageSource:
|
||||
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()
|
||||
for idx, l := range src.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()
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported source: %T", src)
|
||||
}
|
||||
|
||||
// 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)
|
||||
+53
-4
@@ -1,4 +1,4 @@
|
||||
package imgs
|
||||
package text
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -7,18 +7,63 @@ import (
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/imgbom/imgbom/scope"
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||
|
||||
func TestTextDirPresenter(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,
|
||||
})
|
||||
|
||||
s, err := scope.NewScopeFromDir("/some/path", scope.AllLayersScope)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create scope: %+v", err)
|
||||
}
|
||||
pres := NewPresenter(catalog, s)
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type PackageInfo struct {
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
func TestTextPresenter(t *testing.T) {
|
||||
func TestTextImgPresenter(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
catalog := pkg.NewCatalog()
|
||||
@@ -52,9 +97,13 @@ func TestTextPresenter(t *testing.T) {
|
||||
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
||||
}
|
||||
|
||||
pres := NewPresenter(img, catalog)
|
||||
s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pres := NewPresenter(catalog, s)
|
||||
// run presenter
|
||||
err := pres.Present(&buffer)
|
||||
err = pres.Present(&buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user