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 ( import (
"bytes" "bytes"
"github.com/anchore/syft/internal/formats/table"
"github.com/anchore/syft/internal/formats/syftjson" "github.com/anchore/syft/internal/formats/syftjson"
"github.com/anchore/syft/syft/format" "github.com/anchore/syft/syft/format"
) )
@ -11,6 +13,7 @@ import (
func All() []format.Format { func All() []format.Format {
return []format.Format{ return []format.Format{
syftjson.Format(), syftjson.Format(),
table.Format(),
} }
} }

View File

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

View File

@ -1,23 +1,22 @@
package packages package table
import ( import (
"flag" "flag"
"testing" "testing"
"github.com/anchore/syft/internal/formats/common/testutils" "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" "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) { func TestTablePresenter(t *testing.T) {
testImage := "image-simple" catalog, metadata, distro := testutils.DirectoryInput(t)
catalog, _, _ := testutils.ImageInput(t, testImage) testutils.AssertPresenterAgainstGoldenSnapshot(t,
testutils.AssertPresenterAgainstGoldenImageSnapshot(t, format.NewPresenter(encoder, catalog, &metadata, distro, source.SquashedScope),
NewTablePresenter(catalog), *updateTableGoldenFiles,
testImage,
*updateTablePresenterGoldenFiles,
) )
} }

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 { switch option {
case format.TextOption: case format.TextOption:
return packages.NewTextPresenter(config.Catalog, config.SourceMetadata) return packages.NewTextPresenter(config.Catalog, config.SourceMetadata)
case format.TableOption:
return packages.NewTablePresenter(config.Catalog)
case format.CycloneDxOption: case format.CycloneDxOption:
return packages.NewCycloneDxPresenter(config.Catalog, config.SourceMetadata) return packages.NewCycloneDxPresenter(config.Catalog, config.SourceMetadata)
case format.SPDXTagValueOption: case format.SPDXTagValueOption: