Skip to content

Commit

Permalink
port text presenter to a format object (anchore#604)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Oct 29, 2021
1 parent b3f1bd7 commit 0f89951
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
2 changes: 2 additions & 0 deletions internal/formats/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -17,6 +18,7 @@ func All() []format.Format {
table.Format(),
cyclonedx12xml.Format(),
spdx22json.Format(),
text.Format(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down
12 changes: 12 additions & 0 deletions internal/formats/text/format.go
Original file line number Diff line number Diff line change
@@ -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,
)
}
4 changes: 4 additions & 0 deletions internal/formats/text/test-fixtures/image-simple/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this file has contents
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file-2 contents!
Binary file not shown.
2 changes: 0 additions & 2 deletions syft/presenter/packages/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0f89951

Please sign in to comment.