replace table presenter with format object (#586)

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-10-24 08:09:27 -04:00 committed by GitHub
parent d5b425e1b5
commit fb588ff500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 27 deletions

View File

@ -3,6 +3,8 @@ package formats
import (
"bytes"
"github.com/anchore/syft/internal/formats/table"
"github.com/anchore/syft/internal/formats/syftjson"
"github.com/anchore/syft/syft/format"
)
@ -11,6 +13,7 @@ import (
func All() []format.Format {
return []format.Format{
syftjson.Format(),
table.Format(),
}
}

View File

@ -1,4 +1,4 @@
package packages
package table
import (
"fmt"
@ -8,24 +8,16 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
type TablePresenter struct {
catalog *pkg.Catalog
}
func NewTablePresenter(catalog *pkg.Catalog) *TablePresenter {
return &TablePresenter{
catalog: catalog,
}
}
func (pres *TablePresenter) Present(output io.Writer) error {
rows := make([][]string, 0)
func encoder(output io.Writer, catalog *pkg.Catalog, _ *source.Metadata, _ *distro.Distro, _ source.Scope) error {
var rows [][]string
columns := []string{"Name", "Version", "Type"}
for _, p := range pres.catalog.Sorted() {
for _, p := range catalog.Sorted() {
row := []string{
p.Name,
p.Version,
@ -35,8 +27,8 @@ func (pres *TablePresenter) Present(output io.Writer) error {
}
if len(rows) == 0 {
fmt.Fprintln(output, "No packages discovered")
return nil
_, err := fmt.Fprintln(output, "No packages discovered")
return err
}
// sort by name, version, then type

View File

@ -1,23 +1,22 @@
package packages
package table
import (
"flag"
"testing"
"github.com/anchore/syft/internal/formats/common/testutils"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/source"
"github.com/go-test/deep"
)
var updateTablePresenterGoldenFiles = flag.Bool("update-table", false, "update the *.golden files for table presenters")
var updateTableGoldenFiles = flag.Bool("update-table", false, "update the *.golden files for table format")
func TestTablePresenter(t *testing.T) {
testImage := "image-simple"
catalog, _, _ := testutils.ImageInput(t, testImage)
testutils.AssertPresenterAgainstGoldenImageSnapshot(t,
NewTablePresenter(catalog),
testImage,
*updateTablePresenterGoldenFiles,
catalog, metadata, distro := testutils.DirectoryInput(t)
testutils.AssertPresenterAgainstGoldenSnapshot(t,
format.NewPresenter(encoder, catalog, &metadata, distro, source.SquashedScope),
*updateTableGoldenFiles,
)
}

View File

@ -0,0 +1,12 @@
package table
import "github.com/anchore/syft/syft/format"
func Format() format.Format {
return format.NewFormat(
format.TableOption,
encoder,
nil,
nil,
)
}

View File

@ -16,8 +16,6 @@ func Presenter(option format.Option, config PresenterConfig) presenter.Presenter
switch option {
case format.TextOption:
return packages.NewTextPresenter(config.Catalog, config.SourceMetadata)
case format.TableOption:
return packages.NewTablePresenter(config.Catalog)
case format.CycloneDxOption:
return packages.NewCycloneDxPresenter(config.Catalog, config.SourceMetadata)
case format.SPDXTagValueOption: