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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 30 deletions

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(),
}
}

View File

@ -1,42 +1,27 @@
package packages
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"
)
// TextPresenter is a human-friendly text presenter to represent package and source data.
type TextPresenter struct {
catalog *pkg.Catalog
srcMetadata source.Metadata
}
// NewTextPresenter creates a new presenter for the given set of catalog and image data.
func NewTextPresenter(catalog *pkg.Catalog, srcMetadata source.Metadata) *TextPresenter {
return &TextPresenter{
catalog: catalog,
srcMetadata: srcMetadata,
}
}
// Present is a method that is in charge of writing to an output buffer
func (pres *TextPresenter) Present(output io.Writer) error {
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 pres.srcMetadata.Scheme {
switch srcMetadata.Scheme {
case source.DirectoryScheme:
fmt.Fprintf(w, "[Path: %s]\n", pres.srcMetadata.Path)
fmt.Fprintf(w, "[Path: %s]\n", srcMetadata.Path)
case source.ImageScheme:
fmt.Fprintln(w, "[Image]")
for idx, l := range pres.srcMetadata.ImageMetadata.Layers {
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)
@ -45,12 +30,12 @@ func (pres *TextPresenter) Present(output io.Writer) error {
w.Flush()
}
default:
return fmt.Errorf("unsupported source: %T", pres.srcMetadata.Scheme)
return fmt.Errorf("unsupported source: %T", srcMetadata.Scheme)
}
// populate artifacts...
rows := 0
for _, p := range pres.catalog.Sorted() {
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))

View File

@ -1,27 +1,30 @@
package packages
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, _ := testutils.DirectoryInput(t)
catalog, metadata, d := testutils.DirectoryInput(t)
testutils.AssertPresenterAgainstGoldenSnapshot(t,
NewTextPresenter(catalog, metadata),
format.NewPresenter(encoder, catalog, &metadata, d, source.UnknownScope),
*updateTextPresenterGoldenFiles,
)
}
func TestTextImagePresenter(t *testing.T) {
testImage := "image-simple"
catalog, metadata, _ := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
catalog, metadata, d := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
testutils.AssertPresenterAgainstGoldenImageSnapshot(t,
NewTextPresenter(catalog, metadata),
format.NewPresenter(encoder, catalog, &metadata, d, source.SquashedScope),
testImage,
*updateTextPresenterGoldenFiles,
)

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,
)
}

View File

@ -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

View File

@ -0,0 +1 @@
this file has contents

View File

@ -0,0 +1 @@
file-2 contents!

View File

@ -14,8 +14,6 @@ import (
// Presenter returns a presenter for images or directories
func Presenter(option format.Option, config PresenterConfig) presenter.Presenter {
switch option {
case format.TextOption:
return packages.NewTextPresenter(config.Catalog, config.SourceMetadata)
case format.SPDXTagValueOption:
return packages.NewSPDXTagValuePresenter(config.Catalog, config.SourceMetadata)
default: