From 569a598df703b67a99d12eaff1f6cd620778d9b6 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 17 Nov 2020 12:26:27 -0500 Subject: [PATCH] minimize pointer usage & order return types consistently Signed-off-by: Alex Goodman --- cmd/cmd.go | 2 +- cmd/root.go | 4 ++-- syft/cataloger/catalog.go | 2 -- syft/lib.go | 20 ++++++++++---------- syft/presenter/presenter.go | 6 +++--- test/integration/distro_test.go | 10 ++-------- test/integration/document_import_test.go | 6 +++--- test/integration/json_schema_test.go | 12 ++++++------ test/integration/pkg_coverage_test.go | 4 ++-- 9 files changed, 29 insertions(+), 37 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index e39ec4efe..4141504a9 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -112,7 +112,7 @@ func logAppConfig() { if err != nil { log.Debugf("Could not display application config: %+v", err) } else { - log.Debugf("Application config:\n%+v", color.Magenta.Sprint(appCfgStr)) + log.Debugf("Application config:\n%+v", color.Magenta.Sprint(string(appCfgStr))) } } diff --git a/cmd/root.go b/cmd/root.go index 3abccd404..7b72fafdd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -92,7 +92,7 @@ func startWorker(userInput string) <-chan error { } } - catalog, scope, distro, err := syft.Catalog(userInput, appConfig.ScopeOpt) + src, catalog, distro, err := syft.Catalog(userInput, appConfig.ScopeOpt) if err != nil { errs <- fmt.Errorf("failed to catalog input: %+v", err) return @@ -100,7 +100,7 @@ func startWorker(userInput string) <-chan error { bus.Publish(partybus.Event{ Type: event.CatalogerFinished, - Value: presenter.GetPresenter(appConfig.PresenterOpt, scope.Metadata, catalog, distro), + Value: presenter.GetPresenter(appConfig.PresenterOpt, src.Metadata, catalog, distro), }) }() return errs diff --git a/syft/cataloger/catalog.go b/syft/cataloger/catalog.go index 0724be8b6..262fbdbb3 100644 --- a/syft/cataloger/catalog.go +++ b/syft/cataloger/catalog.go @@ -54,8 +54,6 @@ func Catalog(resolver source.Resolver, catalogers ...Cataloger) (*pkg.Catalog, e log.Debugf("cataloger '%s' discovered '%d' packages", theCataloger.Name(), catalogedPackages) packagesDiscovered.N += int64(catalogedPackages) - // helper function to add synthesized information... - for _, p := range packages { catalog.Add(p) } diff --git a/syft/lib.go b/syft/lib.go index d57c61f27..931c3747b 100644 --- a/syft/lib.go +++ b/syft/lib.go @@ -34,22 +34,22 @@ import ( // Catalog the given image from a particular perspective (e.g. squashed source, all-layers source). Returns the discovered // set of packages, the identified Linux distribution, and the source object used to wrap the data source. -func Catalog(userInput string, scoptOpt source.Scope) (*pkg.Catalog, *source.Source, *distro.Distro, error) { +func Catalog(userInput string, scope source.Scope) (source.Source, *pkg.Catalog, distro.Distro, error) { log.Info("cataloging image") - s, cleanup, err := source.New(userInput, scoptOpt) + s, cleanup, err := source.New(userInput, scope) defer cleanup() if err != nil { - return nil, nil, nil, err + return source.Source{}, nil, distro.Distro{}, err } d := IdentifyDistro(s) catalog, err := CatalogFromScope(s) if err != nil { - return nil, nil, nil, err + return source.Source{}, nil, distro.Distro{}, err } - return catalog, &s, &d, nil + return s, catalog, d, nil } // IdentifyDistro attempts to discover what the underlying Linux distribution may be from the available flat files @@ -82,12 +82,12 @@ func CatalogFromScope(s source.Source) (*pkg.Catalog, error) { return cataloger.Catalog(s.Resolver, catalogers...) } -// CatalogFromJSON takes an existing syft report and generates catalog primitives. -func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, source.Metadata, error) { +// CatalogFromJSON takes an existing syft report and generates native syft objects. +func CatalogFromJSON(reader io.Reader) (source.Metadata, *pkg.Catalog, distro.Distro, error) { var doc jsonPresenter.Document decoder := json.NewDecoder(reader) if err := decoder.Decode(&doc); err != nil { - return nil, nil, source.Metadata{}, err + return source.Metadata{}, nil, distro.Distro{}, err } var pkgs = make([]pkg.Package, len(doc.Artifacts)) @@ -106,10 +106,10 @@ func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, source.Met theDistro, err := distro.NewDistro(distroType, doc.Distro.Version, doc.Distro.IDLike) if err != nil { - return nil, nil, source.Metadata{}, err + return source.Metadata{}, nil, distro.Distro{}, err } - return catalog, &theDistro, doc.Source.ToSourceMetadata(), nil + return doc.Source.ToSourceMetadata(), catalog, theDistro, nil } // SetLogger sets the logger object used for all syft logging calls. diff --git a/syft/presenter/presenter.go b/syft/presenter/presenter.go index 4f1b86224..6c32b60d0 100644 --- a/syft/presenter/presenter.go +++ b/syft/presenter/presenter.go @@ -25,16 +25,16 @@ type Presenter interface { } // GetPresenter returns a presenter for images or directories -func GetPresenter(option Option, srcMetadata source.Metadata, catalog *pkg.Catalog, d *distro.Distro) Presenter { +func GetPresenter(option Option, srcMetadata source.Metadata, catalog *pkg.Catalog, d distro.Distro) Presenter { switch option { case JSONPresenter: - return json.NewPresenter(catalog, srcMetadata, *d) + return json.NewPresenter(catalog, srcMetadata, d) case TextPresenter: return text.NewPresenter(catalog, srcMetadata) case TablePresenter: return table.NewPresenter(catalog) case CycloneDxPresenter: - return cyclonedx.NewPresenter(catalog, srcMetadata, *d) + return cyclonedx.NewPresenter(catalog, srcMetadata, d) default: return nil } diff --git a/test/integration/distro_test.go b/test/integration/distro_test.go index a0ad3fd64..e6a9ce297 100644 --- a/test/integration/distro_test.go +++ b/test/integration/distro_test.go @@ -20,20 +20,14 @@ func TestDistroImage(t *testing.T) { if err != nil { t.Fatalf("failed to catalog image: %+v", err) } - if actualDistro == nil { - t.Fatalf("could not find distro") - } expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "") if err != nil { t.Fatalf("could not create distro: %+v", err) } - diffs := deep.Equal(*actualDistro, expected) - if len(diffs) != 0 { - for _, d := range diffs { - t.Errorf("found distro difference: %+v", d) - } + for _, d := range deep.Equal(actualDistro, expected) { + t.Errorf("found distro difference: %+v", d) } } diff --git a/test/integration/document_import_test.go b/test/integration/document_import_test.go index 056e92f66..86c07a68f 100644 --- a/test/integration/document_import_test.go +++ b/test/integration/document_import_test.go @@ -31,18 +31,18 @@ func TestCatalogFromJSON(t *testing.T) { tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture) defer cleanup() - expectedCatalog, expectedSource, expectedDistro, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) + expectedSource, expectedCatalog, expectedDistro, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) if err != nil { t.Fatalf("failed to catalog image: %+v", err) } var buf bytes.Buffer - jsonPres := json.NewPresenter(expectedCatalog, expectedSource.Metadata, *expectedDistro) + jsonPres := json.NewPresenter(expectedCatalog, expectedSource.Metadata, expectedDistro) if err = jsonPres.Present(&buf); err != nil { t.Fatalf("failed to write to presenter: %+v", err) } - actualCatalog, actualDistro, sourceMetadata, err := syft.CatalogFromJSON(&buf) + sourceMetadata, actualCatalog, actualDistro, err := syft.CatalogFromJSON(&buf) if err != nil { t.Fatalf("failed to import document: %+v", err) } diff --git a/test/integration/json_schema_test.go b/test/integration/json_schema_test.go index b84bf26f8..07b1a32cb 100644 --- a/test/integration/json_schema_test.go +++ b/test/integration/json_schema_test.go @@ -53,7 +53,7 @@ func validateAgainstV1Schema(t *testing.T, json string) { } } -func testJsonSchema(t *testing.T, catalog *pkg.Catalog, theScope *source.Source, prefix string) { +func testJsonSchema(t *testing.T, catalog *pkg.Catalog, theScope source.Source, prefix string) { // make the json output example dir if it does not exist absJsonSchemaExamplesPath := path.Join(repoRoot(t), jsonSchemaExamplesPath) if _, err := os.Stat(absJsonSchemaExamplesPath); os.IsNotExist(err) { @@ -67,7 +67,7 @@ func testJsonSchema(t *testing.T, catalog *pkg.Catalog, theScope *source.Source, t.Fatalf("bad distro: %+v", err) } - p := presenter.GetPresenter(presenter.JSONPresenter, theScope.Metadata, catalog, &d) + p := presenter.GetPresenter(presenter.JSONPresenter, theScope.Metadata, catalog, d) if p == nil { t.Fatal("unable to get presenter") } @@ -101,7 +101,7 @@ func TestJsonSchemaImg(t *testing.T) { tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName) defer cleanup() - catalog, theScope, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) + src, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) if err != nil { t.Fatalf("failed to catalog image: %+v", err) } @@ -112,13 +112,13 @@ func TestJsonSchemaImg(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - testJsonSchema(t, catalog, theScope, "img") + testJsonSchema(t, catalog, src, "img") }) } } func TestJsonSchemaDirs(t *testing.T) { - catalog, theScope, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.AllLayersScope) + src, catalog, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.AllLayersScope) if err != nil { t.Errorf("unable to create source from dir: %+v", err) } @@ -129,7 +129,7 @@ func TestJsonSchemaDirs(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - testJsonSchema(t, catalog, theScope, "dir") + testJsonSchema(t, catalog, src, "dir") }) } } diff --git a/test/integration/pkg_coverage_test.go b/test/integration/pkg_coverage_test.go index 78e821829..cc25f391a 100644 --- a/test/integration/pkg_coverage_test.go +++ b/test/integration/pkg_coverage_test.go @@ -18,7 +18,7 @@ func TestPkgCoverageImage(t *testing.T) { tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName) defer cleanup() - catalog, _, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) + _, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) if err != nil { t.Fatalf("failed to catalog image: %+v", err) } @@ -100,7 +100,7 @@ func TestPkgCoverageImage(t *testing.T) { } func TestPkgCoverageDirectory(t *testing.T) { - catalog, _, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.AllLayersScope) + _, catalog, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.AllLayersScope) if err != nil { t.Errorf("unable to create source from dir: %+v", err)