mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 00:43:20 +01:00
port text presenter to a format object (#604)
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
parent
9aca23f766
commit
358b3a2cf8
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/anchore/syft/internal/formats/spdx22json"
|
"github.com/anchore/syft/internal/formats/spdx22json"
|
||||||
"github.com/anchore/syft/internal/formats/syftjson"
|
"github.com/anchore/syft/internal/formats/syftjson"
|
||||||
"github.com/anchore/syft/internal/formats/table"
|
"github.com/anchore/syft/internal/formats/table"
|
||||||
|
"github.com/anchore/syft/internal/formats/text"
|
||||||
"github.com/anchore/syft/syft/format"
|
"github.com/anchore/syft/syft/format"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,6 +18,7 @@ func All() []format.Format {
|
|||||||
table.Format(),
|
table.Format(),
|
||||||
cyclonedx12xml.Format(),
|
cyclonedx12xml.Format(),
|
||||||
spdx22json.Format(),
|
spdx22json.Format(),
|
||||||
|
text.Format(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,42 +1,27 @@
|
|||||||
package packages
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
|
"github.com/anchore/syft/syft/distro"
|
||||||
"github.com/anchore/syft/syft/pkg"
|
"github.com/anchore/syft/syft/pkg"
|
||||||
"github.com/anchore/syft/syft/source"
|
"github.com/anchore/syft/syft/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TextPresenter is a human-friendly text presenter to represent package and source data.
|
func encoder(output io.Writer, catalog *pkg.Catalog, srcMetadata *source.Metadata, _ *distro.Distro, _ source.Scope) error {
|
||||||
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 {
|
|
||||||
// init the tabular writer
|
// init the tabular writer
|
||||||
w := new(tabwriter.Writer)
|
w := new(tabwriter.Writer)
|
||||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
||||||
|
|
||||||
switch pres.srcMetadata.Scheme {
|
switch srcMetadata.Scheme {
|
||||||
case source.DirectoryScheme:
|
case source.DirectoryScheme:
|
||||||
fmt.Fprintf(w, "[Path: %s]\n", pres.srcMetadata.Path)
|
fmt.Fprintf(w, "[Path: %s]\n", srcMetadata.Path)
|
||||||
case source.ImageScheme:
|
case source.ImageScheme:
|
||||||
fmt.Fprintln(w, "[Image]")
|
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, " Layer:\t", idx)
|
||||||
fmt.Fprintln(w, " Digest:\t", l.Digest)
|
fmt.Fprintln(w, " Digest:\t", l.Digest)
|
||||||
fmt.Fprintln(w, " Size:\t", l.Size)
|
fmt.Fprintln(w, " Size:\t", l.Size)
|
||||||
@ -45,12 +30,12 @@ func (pres *TextPresenter) Present(output io.Writer) error {
|
|||||||
w.Flush()
|
w.Flush()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported source: %T", pres.srcMetadata.Scheme)
|
return fmt.Errorf("unsupported source: %T", srcMetadata.Scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
// populate artifacts...
|
// populate artifacts...
|
||||||
rows := 0
|
rows := 0
|
||||||
for _, p := range pres.catalog.Sorted() {
|
for _, p := range catalog.Sorted() {
|
||||||
fmt.Fprintf(w, "[%s]\n", p.Name)
|
fmt.Fprintf(w, "[%s]\n", p.Name)
|
||||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
fmt.Fprintln(w, " Version:\t", p.Version)
|
||||||
fmt.Fprintln(w, " Type:\t", string(p.Type))
|
fmt.Fprintln(w, " Type:\t", string(p.Type))
|
||||||
@ -1,27 +1,30 @@
|
|||||||
package packages
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anchore/syft/syft/source"
|
||||||
|
|
||||||
"github.com/anchore/syft/internal/formats/common/testutils"
|
"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")
|
var updateTextPresenterGoldenFiles = flag.Bool("update-text", false, "update the *.golden files for text presenters")
|
||||||
|
|
||||||
func TestTextDirectoryPresenter(t *testing.T) {
|
func TestTextDirectoryPresenter(t *testing.T) {
|
||||||
catalog, metadata, _ := testutils.DirectoryInput(t)
|
catalog, metadata, d := testutils.DirectoryInput(t)
|
||||||
testutils.AssertPresenterAgainstGoldenSnapshot(t,
|
testutils.AssertPresenterAgainstGoldenSnapshot(t,
|
||||||
NewTextPresenter(catalog, metadata),
|
format.NewPresenter(encoder, catalog, &metadata, d, source.UnknownScope),
|
||||||
*updateTextPresenterGoldenFiles,
|
*updateTextPresenterGoldenFiles,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTextImagePresenter(t *testing.T) {
|
func TestTextImagePresenter(t *testing.T) {
|
||||||
testImage := "image-simple"
|
testImage := "image-simple"
|
||||||
catalog, metadata, _ := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
|
catalog, metadata, d := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
|
||||||
testutils.AssertPresenterAgainstGoldenImageSnapshot(t,
|
testutils.AssertPresenterAgainstGoldenImageSnapshot(t,
|
||||||
NewTextPresenter(catalog, metadata),
|
format.NewPresenter(encoder, catalog, &metadata, d, source.SquashedScope),
|
||||||
testImage,
|
testImage,
|
||||||
*updateTextPresenterGoldenFiles,
|
*updateTextPresenterGoldenFiles,
|
||||||
)
|
)
|
||||||
12
internal/formats/text/format.go
Normal file
12
internal/formats/text/format.go
Normal 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!
|
||||||
Binary file not shown.
@ -14,8 +14,6 @@ import (
|
|||||||
// Presenter returns a presenter for images or directories
|
// Presenter returns a presenter for images or directories
|
||||||
func Presenter(option format.Option, config PresenterConfig) presenter.Presenter {
|
func Presenter(option format.Option, config PresenterConfig) presenter.Presenter {
|
||||||
switch option {
|
switch option {
|
||||||
case format.TextOption:
|
|
||||||
return packages.NewTextPresenter(config.Catalog, config.SourceMetadata)
|
|
||||||
case format.SPDXTagValueOption:
|
case format.SPDXTagValueOption:
|
||||||
return packages.NewSPDXTagValuePresenter(config.Catalog, config.SourceMetadata)
|
return packages.NewSPDXTagValuePresenter(config.Catalog, config.SourceMetadata)
|
||||||
default:
|
default:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user