port text presenter to a format object (#604)

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2021-10-29 11:31:15 -04:00
committed by GitHub
parent 9aca23f766
commit 358b3a2cf8
11 changed files with 36 additions and 30 deletions
+2
View File
@@ -7,6 +7,7 @@ import (
"github.com/anchore/syft/internal/formats/spdx22json"
"github.com/anchore/syft/internal/formats/syftjson"
"github.com/anchore/syft/internal/formats/table"
"github.com/anchore/syft/internal/formats/text"
"github.com/anchore/syft/syft/format"
)
@@ -17,6 +18,7 @@ func All() []format.Format {
table.Format(),
cyclonedx12xml.Format(),
spdx22json.Format(),
text.Format(),
}
}
+54
View File
@@ -0,0 +1,54 @@
package text
import (
"fmt"
"io"
"text/tabwriter"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
func encoder(output io.Writer, catalog *pkg.Catalog, srcMetadata *source.Metadata, _ *distro.Distro, _ source.Scope) error {
// init the tabular writer
w := new(tabwriter.Writer)
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
switch srcMetadata.Scheme {
case source.DirectoryScheme:
fmt.Fprintf(w, "[Path: %s]\n", srcMetadata.Path)
case source.ImageScheme:
fmt.Fprintln(w, "[Image]")
for idx, l := range srcMetadata.ImageMetadata.Layers {
fmt.Fprintln(w, " Layer:\t", idx)
fmt.Fprintln(w, " Digest:\t", l.Digest)
fmt.Fprintln(w, " Size:\t", l.Size)
fmt.Fprintln(w, " MediaType:\t", l.MediaType)
fmt.Fprintln(w)
w.Flush()
}
default:
return fmt.Errorf("unsupported source: %T", srcMetadata.Scheme)
}
// populate artifacts...
rows := 0
for _, p := range catalog.Sorted() {
fmt.Fprintf(w, "[%s]\n", p.Name)
fmt.Fprintln(w, " Version:\t", p.Version)
fmt.Fprintln(w, " Type:\t", string(p.Type))
fmt.Fprintln(w, " Found by:\t", p.FoundBy)
fmt.Fprintln(w)
w.Flush()
rows++
}
if rows == 0 {
fmt.Fprintln(output, "No packages discovered")
return nil
}
return nil
}
+31
View File
@@ -0,0 +1,31 @@
package text
import (
"flag"
"testing"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/internal/formats/common/testutils"
"github.com/anchore/syft/syft/format"
)
var updateTextPresenterGoldenFiles = flag.Bool("update-text", false, "update the *.golden files for text presenters")
func TestTextDirectoryPresenter(t *testing.T) {
catalog, metadata, d := testutils.DirectoryInput(t)
testutils.AssertPresenterAgainstGoldenSnapshot(t,
format.NewPresenter(encoder, catalog, &metadata, d, source.UnknownScope),
*updateTextPresenterGoldenFiles,
)
}
func TestTextImagePresenter(t *testing.T) {
testImage := "image-simple"
catalog, metadata, d := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
testutils.AssertPresenterAgainstGoldenImageSnapshot(t,
format.NewPresenter(encoder, catalog, &metadata, d, source.SquashedScope),
testImage,
*updateTextPresenterGoldenFiles,
)
}
+12
View File
@@ -0,0 +1,12 @@
package text
import "github.com/anchore/syft/syft/format"
func Format() format.Format {
return format.NewFormat(
format.TextOption,
encoder,
nil,
nil,
)
}
@@ -0,0 +1,4 @@
# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one.
FROM scratch
ADD file-1.txt /somefile-1.txt
ADD file-2.txt /somefile-2.txt
@@ -0,0 +1 @@
this file has contents
@@ -0,0 +1 @@
file-2 contents!
@@ -0,0 +1,11 @@
[Path: /some/path]
[package-1]
Version: 1.0.1
Type: python
Found by: the-cataloger-1
[package-2]
Version: 2.0.1
Type: deb
Found by: the-cataloger-2
@@ -0,0 +1,21 @@
[Image]
Layer: 0
Digest: sha256:fb6beecb75b39f4bb813dbf177e501edd5ddb3e69bb45cedeb78c676ee1b7a59
Size: 22
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
Layer: 1
Digest: sha256:319b588ce64253a87b533c8ed01cf0025e0eac98e7b516e12532957e1244fdec
Size: 16
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
[package-1]
Version: 1.0.1
Type: python
Found by: the-cataloger-1
[package-2]
Version: 2.0.1
Type: deb
Found by: the-cataloger-2