mirror of
https://github.com/anchore/syft
synced 2024-11-10 06:14:16 +00:00
implement a text presenter
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
72f9091493
commit
2e6aef17e1
3 changed files with 133 additions and 9 deletions
56
imgbom/presenter/text/presenter.go
Normal file
56
imgbom/presenter/text/presenter.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
||||
)
|
||||
|
||||
// Presenter holds the Present method to produce output
|
||||
type Presenter struct{}
|
||||
|
||||
// NewPresenter is a constructor for a Presenter
|
||||
func NewPresenter() *Presenter {
|
||||
return &Presenter{}
|
||||
}
|
||||
|
||||
// Present is a method that is in charge of writing to an output buffer
|
||||
func (pres *Presenter) Present(output io.Writer, img *stereoscopeImg.Image, catalog *pkg.Catalog) error {
|
||||
tags := make([]string, len(img.Metadata.Tags))
|
||||
for idx, tag := range 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 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...
|
||||
for p := range 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
|
||||
}
|
74
imgbom/presenter/text/presenter_test.go
Normal file
74
imgbom/presenter/text/presenter_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"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")
|
||||
|
||||
type PackageInfo struct {
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
func TestTextPresenter(t *testing.T) {
|
||||
pres := NewPresenter()
|
||||
var buffer bytes.Buffer
|
||||
|
||||
catalog := pkg.NewCatalog()
|
||||
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-simple")
|
||||
defer cleanup()
|
||||
|
||||
// populate catalog with test data
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-1",
|
||||
Version: "1.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-1.txt"),
|
||||
},
|
||||
FoundBy: "dpkg",
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-2",
|
||||
Version: "2.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-2.txt"),
|
||||
},
|
||||
FoundBy: "dpkg",
|
||||
Metadata: PackageInfo{Name: "package-2", Version: "1.0.2"},
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
|
||||
// stub out all the digests so that they don't affect tests comparisons
|
||||
// TODO: update with go-testutils feature when issue #1 is resolved
|
||||
for _, l := range img.Layers {
|
||||
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
||||
}
|
||||
|
||||
// run presenter
|
||||
err := pres.Present(&buffer, img, catalog)
|
||||
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))
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +1,22 @@
|
|||
[Image]
|
||||
Layer: 0
|
||||
Digest: sha256:056c0789fa9ad629ceae6d09713fb035f84115af3c4a88a43aa60f13bc683053
|
||||
Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53
|
||||
Size: 22
|
||||
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
|
||||
|
||||
Layer: 1
|
||||
Digest: sha256:b461c48116592c570a66fed71d5b09662a8172e168b7938cf317af47872cdc9b
|
||||
Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53
|
||||
Size: 16
|
||||
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
|
||||
|
||||
Layer: 2
|
||||
Digest: sha256:00b80053e05c01da485015610d288ce3185fac00d251e2ada02b45a7a7c5f589
|
||||
Size: 27
|
||||
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
|
||||
|
||||
[package-1]
|
||||
Version: 1.0.1
|
||||
Type: deb
|
||||
Metadata: <nil>
|
||||
Found by: dpkg
|
||||
|
||||
[package-2]
|
||||
Version: 2.0.1
|
||||
Type: deb
|
||||
Metadata: {package-2 1.0.2}
|
||||
Metadata: {Name:package-2 Version:1.0.2}
|
||||
Found by: dpkg
|
||||
|
||||
|
|
Loading…
Reference in a new issue