mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 10:36:45 +01:00
minimize pointer usage & order return types consistently
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
parent
0ad8c53ec2
commit
569a598df7
@ -112,7 +112,7 @@ func logAppConfig() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Could not display application config: %+v", err)
|
log.Debugf("Could not display application config: %+v", err)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Application config:\n%+v", color.Magenta.Sprint(appCfgStr))
|
log.Debugf("Application config:\n%+v", color.Magenta.Sprint(string(appCfgStr)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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 {
|
if err != nil {
|
||||||
errs <- fmt.Errorf("failed to catalog input: %+v", err)
|
errs <- fmt.Errorf("failed to catalog input: %+v", err)
|
||||||
return
|
return
|
||||||
@ -100,7 +100,7 @@ func startWorker(userInput string) <-chan error {
|
|||||||
|
|
||||||
bus.Publish(partybus.Event{
|
bus.Publish(partybus.Event{
|
||||||
Type: event.CatalogerFinished,
|
Type: event.CatalogerFinished,
|
||||||
Value: presenter.GetPresenter(appConfig.PresenterOpt, scope.Metadata, catalog, distro),
|
Value: presenter.GetPresenter(appConfig.PresenterOpt, src.Metadata, catalog, distro),
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
return errs
|
return errs
|
||||||
|
|||||||
@ -54,8 +54,6 @@ func Catalog(resolver source.Resolver, catalogers ...Cataloger) (*pkg.Catalog, e
|
|||||||
log.Debugf("cataloger '%s' discovered '%d' packages", theCataloger.Name(), catalogedPackages)
|
log.Debugf("cataloger '%s' discovered '%d' packages", theCataloger.Name(), catalogedPackages)
|
||||||
packagesDiscovered.N += int64(catalogedPackages)
|
packagesDiscovered.N += int64(catalogedPackages)
|
||||||
|
|
||||||
// helper function to add synthesized information...
|
|
||||||
|
|
||||||
for _, p := range packages {
|
for _, p := range packages {
|
||||||
catalog.Add(p)
|
catalog.Add(p)
|
||||||
}
|
}
|
||||||
|
|||||||
20
syft/lib.go
20
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
|
// 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.
|
// 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")
|
log.Info("cataloging image")
|
||||||
s, cleanup, err := source.New(userInput, scoptOpt)
|
s, cleanup, err := source.New(userInput, scope)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return source.Source{}, nil, distro.Distro{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
d := IdentifyDistro(s)
|
d := IdentifyDistro(s)
|
||||||
|
|
||||||
catalog, err := CatalogFromScope(s)
|
catalog, err := CatalogFromScope(s)
|
||||||
if err != nil {
|
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
|
// 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...)
|
return cataloger.Catalog(s.Resolver, catalogers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CatalogFromJSON takes an existing syft report and generates catalog primitives.
|
// CatalogFromJSON takes an existing syft report and generates native syft objects.
|
||||||
func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, source.Metadata, error) {
|
func CatalogFromJSON(reader io.Reader) (source.Metadata, *pkg.Catalog, distro.Distro, error) {
|
||||||
var doc jsonPresenter.Document
|
var doc jsonPresenter.Document
|
||||||
decoder := json.NewDecoder(reader)
|
decoder := json.NewDecoder(reader)
|
||||||
if err := decoder.Decode(&doc); err != nil {
|
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))
|
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)
|
theDistro, err := distro.NewDistro(distroType, doc.Distro.Version, doc.Distro.IDLike)
|
||||||
if err != nil {
|
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.
|
// SetLogger sets the logger object used for all syft logging calls.
|
||||||
|
|||||||
@ -25,16 +25,16 @@ type Presenter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPresenter returns a presenter for images or directories
|
// 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 {
|
switch option {
|
||||||
case JSONPresenter:
|
case JSONPresenter:
|
||||||
return json.NewPresenter(catalog, srcMetadata, *d)
|
return json.NewPresenter(catalog, srcMetadata, d)
|
||||||
case TextPresenter:
|
case TextPresenter:
|
||||||
return text.NewPresenter(catalog, srcMetadata)
|
return text.NewPresenter(catalog, srcMetadata)
|
||||||
case TablePresenter:
|
case TablePresenter:
|
||||||
return table.NewPresenter(catalog)
|
return table.NewPresenter(catalog)
|
||||||
case CycloneDxPresenter:
|
case CycloneDxPresenter:
|
||||||
return cyclonedx.NewPresenter(catalog, srcMetadata, *d)
|
return cyclonedx.NewPresenter(catalog, srcMetadata, d)
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,20 +20,14 @@ func TestDistroImage(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to catalog image: %+v", err)
|
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", "")
|
expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not create distro: %+v", err)
|
t.Fatalf("could not create distro: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
diffs := deep.Equal(*actualDistro, expected)
|
for _, d := range deep.Equal(actualDistro, expected) {
|
||||||
if len(diffs) != 0 {
|
t.Errorf("found distro difference: %+v", d)
|
||||||
for _, d := range diffs {
|
|
||||||
t.Errorf("found distro difference: %+v", d)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,18 +31,18 @@ func TestCatalogFromJSON(t *testing.T) {
|
|||||||
tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture)
|
tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture)
|
||||||
defer cleanup()
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("failed to catalog image: %+v", err)
|
t.Fatalf("failed to catalog image: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
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 {
|
if err = jsonPres.Present(&buf); err != nil {
|
||||||
t.Fatalf("failed to write to presenter: %+v", err)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("failed to import document: %+v", err)
|
t.Fatalf("failed to import document: %+v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
// make the json output example dir if it does not exist
|
||||||
absJsonSchemaExamplesPath := path.Join(repoRoot(t), jsonSchemaExamplesPath)
|
absJsonSchemaExamplesPath := path.Join(repoRoot(t), jsonSchemaExamplesPath)
|
||||||
if _, err := os.Stat(absJsonSchemaExamplesPath); os.IsNotExist(err) {
|
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)
|
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 {
|
if p == nil {
|
||||||
t.Fatal("unable to get presenter")
|
t.Fatal("unable to get presenter")
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ func TestJsonSchemaImg(t *testing.T) {
|
|||||||
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
|
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
|
||||||
defer cleanup()
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("failed to catalog image: %+v", err)
|
t.Fatalf("failed to catalog image: %+v", err)
|
||||||
}
|
}
|
||||||
@ -112,13 +112,13 @@ func TestJsonSchemaImg(t *testing.T) {
|
|||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
testJsonSchema(t, catalog, theScope, "img")
|
testJsonSchema(t, catalog, src, "img")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJsonSchemaDirs(t *testing.T) {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("unable to create source from dir: %+v", err)
|
t.Errorf("unable to create source from dir: %+v", err)
|
||||||
}
|
}
|
||||||
@ -129,7 +129,7 @@ func TestJsonSchemaDirs(t *testing.T) {
|
|||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
testJsonSchema(t, catalog, theScope, "dir")
|
testJsonSchema(t, catalog, src, "dir")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ func TestPkgCoverageImage(t *testing.T) {
|
|||||||
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
|
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
catalog, _, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope)
|
_, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to catalog image: %+v", err)
|
t.Fatalf("failed to catalog image: %+v", err)
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ func TestPkgCoverageImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPkgCoverageDirectory(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 {
|
if err != nil {
|
||||||
t.Errorf("unable to create source from dir: %+v", err)
|
t.Errorf("unable to create source from dir: %+v", err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user