minimize pointer usage & order return types consistently

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-11-17 12:26:27 -05:00
parent 0ad8c53ec2
commit 569a598df7
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
9 changed files with 29 additions and 37 deletions

View File

@ -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)))
}
}

View File

@ -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

View File

@ -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)
}

View File

@ -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.

View File

@ -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
}

View File

@ -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 {
for _, d := range deep.Equal(actualDistro, expected) {
t.Errorf("found distro difference: %+v", d)
}
}
}

View File

@ -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)
}

View File

@ -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")
})
}
}

View File

@ -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)