From 33080791583e6c3c8707a7eb964e14cb97f2f249 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 22 Mar 2022 16:05:45 -0400 Subject: [PATCH] export event monitor structs but not behavior Signed-off-by: Alex Goodman --- internal/anchore/import.go | 8 +++--- syft/event/monitor/file_digester_monitor.go | 28 ------------------- .../monitor/package_cataloger_monitor.go | 18 ------------ .../monitor/secrets_cataloger_monitor.go | 23 --------------- syft/file/digest_cataloger.go | 27 ++++++++++++++++-- syft/file/secrets_cataloger.go | 26 ++++++++++++++++- syft/pkg/cataloger/packages/catalog.go | 21 +++++++++++++- syft/source/directory_resolver.go | 3 +- 8 files changed, 75 insertions(+), 79 deletions(-) delete mode 100644 syft/event/monitor/file_digester_monitor.go diff --git a/internal/anchore/import.go b/internal/anchore/import.go index 14f630373..3b1e4c1a2 100644 --- a/internal/anchore/import.go +++ b/internal/anchore/import.go @@ -4,16 +4,16 @@ import ( "context" "errors" "fmt" + "github.com/anchore/syft/internal/bus" + "github.com/anchore/syft/syft/event" + "github.com/wagoodman/go-partybus" + "github.com/wagoodman/go-progress" "time" "github.com/anchore/client-go/pkg/external" "github.com/anchore/stereoscope/pkg/image" - "github.com/anchore/syft/internal/bus" - "github.com/anchore/syft/syft/event" "github.com/anchore/syft/syft/sbom" "github.com/antihax/optional" - "github.com/wagoodman/go-partybus" - "github.com/wagoodman/go-progress" ) type ImportConfig struct { diff --git a/syft/event/monitor/file_digester_monitor.go b/syft/event/monitor/file_digester_monitor.go deleted file mode 100644 index c46b44e5f..000000000 --- a/syft/event/monitor/file_digester_monitor.go +++ /dev/null @@ -1,28 +0,0 @@ -package monitor - -import ( - "github.com/anchore/syft/internal/bus" - "github.com/anchore/syft/syft/event" - "github.com/wagoodman/go-partybus" - "github.com/wagoodman/go-progress" -) - -func FileDigesterMonitor(locations int64) (*progress.Stage, *progress.Manual) { - stage := &progress.Stage{} - prog := &progress.Manual{ - Total: locations, - } - - bus.Publish(partybus.Event{ - Type: event.FileDigestsCatalogerStarted, - Value: struct { - progress.Stager - progress.Progressable - }{ - Stager: progress.Stager(stage), - Progressable: prog, - }, - }) - - return stage, prog -} diff --git a/syft/event/monitor/package_cataloger_monitor.go b/syft/event/monitor/package_cataloger_monitor.go index 82b7e0032..6bf7e5b89 100644 --- a/syft/event/monitor/package_cataloger_monitor.go +++ b/syft/event/monitor/package_cataloger_monitor.go @@ -1,9 +1,6 @@ package monitor import ( - "github.com/anchore/syft/internal/bus" - "github.com/anchore/syft/syft/event" - "github.com/wagoodman/go-partybus" "github.com/wagoodman/go-progress" ) @@ -12,18 +9,3 @@ type PackageCatalogerMonitor struct { FilesProcessed progress.Monitorable // the number of files selected and contents analyzed from all registered catalogers PackagesDiscovered progress.Monitorable // the number of packages discovered from all registered catalogers } - -// NewPackageCatalogerMonitor creates a new PackageCatalogerMonitor object and publishes the object on the bus as a PackageCatalogerStarted event. -func NewPackageCatalogerMonitor() (*progress.Manual, *progress.Manual) { - filesProcessed := progress.Manual{} - packagesDiscovered := progress.Manual{} - - bus.Publish(partybus.Event{ - Type: event.PackageCatalogerStarted, - Value: PackageCatalogerMonitor{ - FilesProcessed: progress.Monitorable(&filesProcessed), - PackagesDiscovered: progress.Monitorable(&packagesDiscovered), - }, - }) - return &filesProcessed, &packagesDiscovered -} diff --git a/syft/event/monitor/secrets_cataloger_monitor.go b/syft/event/monitor/secrets_cataloger_monitor.go index a6208499d..6e5a04e25 100644 --- a/syft/event/monitor/secrets_cataloger_monitor.go +++ b/syft/event/monitor/secrets_cataloger_monitor.go @@ -1,9 +1,6 @@ package monitor import ( - "github.com/anchore/syft/internal/bus" - "github.com/anchore/syft/syft/event" - "github.com/wagoodman/go-partybus" "github.com/wagoodman/go-progress" ) @@ -12,23 +9,3 @@ type SecretsCatalogerMonitor struct { SecretsDiscovered progress.Monitorable progress.Progressable } - -func NewSecretsCatalogerMonitor(locations int64) (*progress.Stage, *progress.Manual, *progress.Manual) { - stage := &progress.Stage{} - secretsDiscovered := &progress.Manual{} - prog := &progress.Manual{ - Total: locations, - } - - bus.Publish(partybus.Event{ - Type: event.SecretsCatalogerStarted, - Source: secretsDiscovered, - Value: SecretsCatalogerMonitor{ - Stager: progress.Stager(stage), - SecretsDiscovered: secretsDiscovered, - Progressable: prog, - }, - }) - - return stage, prog, secretsDiscovered -} diff --git a/syft/file/digest_cataloger.go b/syft/file/digest_cataloger.go index 8133b4a2e..8d6c7c52c 100644 --- a/syft/file/digest_cataloger.go +++ b/syft/file/digest_cataloger.go @@ -5,7 +5,10 @@ import ( "errors" "fmt" "github.com/anchore/syft/internal" - "github.com/anchore/syft/syft/event/monitor" + "github.com/anchore/syft/internal/bus" + "github.com/anchore/syft/syft/event" + "github.com/wagoodman/go-partybus" + "github.com/wagoodman/go-progress" "hash" "io" @@ -29,7 +32,7 @@ func NewDigestsCataloger(hashes []crypto.Hash) (*DigestsCataloger, error) { func (i *DigestsCataloger) Catalog(resolver source.FileResolver) (map[source.Coordinates][]Digest, error) { results := make(map[source.Coordinates][]Digest) locations := allRegularFiles(resolver) - stage, prog := monitor.FileDigesterMonitor(int64(len(locations))) + stage, prog := digestsCatalogingProgress(int64(len(locations))) for _, location := range locations { stage.Current = location.RealPath result, err := i.catalogLocation(resolver, location) @@ -101,3 +104,23 @@ func (i *DigestsCataloger) catalogLocation(resolver source.FileResolver, locatio return result, nil } + +func digestsCatalogingProgress(locations int64) (*progress.Stage, *progress.Manual) { + stage := &progress.Stage{} + prog := &progress.Manual{ + Total: locations, + } + + bus.Publish(partybus.Event{ + Type: event.FileDigestsCatalogerStarted, + Value: struct { + progress.Stager + progress.Progressable + }{ + Stager: progress.Stager(stage), + Progressable: prog, + }, + }) + + return stage, prog +} diff --git a/syft/file/secrets_cataloger.go b/syft/file/secrets_cataloger.go index 00389eb8b..a1c5147e3 100644 --- a/syft/file/secrets_cataloger.go +++ b/syft/file/secrets_cataloger.go @@ -3,8 +3,12 @@ package file import ( "bytes" "fmt" + "github.com/anchore/syft/internal/bus" "github.com/anchore/syft/internal/file" + "github.com/anchore/syft/syft/event" "github.com/anchore/syft/syft/event/monitor" + "github.com/wagoodman/go-partybus" + "github.com/wagoodman/go-progress" "io" "io/ioutil" "regexp" @@ -56,7 +60,7 @@ func NewSecretsCataloger(config SecretsCatalogerConfig) (*SecretsCataloger, erro func (i *SecretsCataloger) Catalog(resolver source.FileResolver) (map[source.Coordinates][]SearchResult, error) { results := make(map[source.Coordinates][]SearchResult) locations := allRegularFiles(resolver) - stage, prog, secretsDiscovered := monitor.NewSecretsCatalogerMonitor(int64(len(locations))) + stage, prog, secretsDiscovered := newSecretsCatalogerMonitor(int64(len(locations))) for _, location := range locations { stage.Current = location.RealPath result, err := i.catalogLocation(resolver, location) @@ -143,3 +147,23 @@ func extractValue(resolver source.FileResolver, location source.Location, start, return buf.String(), nil } + +func newSecretsCatalogerMonitor(locations int64) (*progress.Stage, *progress.Manual, *progress.Manual) { + stage := &progress.Stage{} + secretsDiscovered := &progress.Manual{} + prog := &progress.Manual{ + Total: locations, + } + + bus.Publish(partybus.Event{ + Type: event.SecretsCatalogerStarted, + Source: secretsDiscovered, + Value: monitor.SecretsCatalogerMonitor{ + Stager: progress.Stager(stage), + SecretsDiscovered: secretsDiscovered, + Progressable: prog, + }, + }) + + return stage, prog, secretsDiscovered +} diff --git a/syft/pkg/cataloger/packages/catalog.go b/syft/pkg/cataloger/packages/catalog.go index 11e383c7a..ca7413edd 100644 --- a/syft/pkg/cataloger/packages/catalog.go +++ b/syft/pkg/cataloger/packages/catalog.go @@ -2,14 +2,18 @@ package packages import ( "fmt" + "github.com/anchore/syft/internal/bus" "github.com/anchore/syft/internal/log" "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" + "github.com/anchore/syft/syft/event" "github.com/anchore/syft/syft/event/monitor" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/source" "github.com/hashicorp/go-multierror" + "github.com/wagoodman/go-partybus" + "github.com/wagoodman/go-progress" ) // Catalog a given source (container image or filesystem) with the given catalogers, returning all discovered packages. @@ -20,7 +24,7 @@ func Catalog(resolver source.FileResolver, release *linux.Release, catalogers .. catalog := pkg.NewCatalog() var allRelationships []artifact.Relationship - filesProcessed, packagesDiscovered := monitor.NewPackageCatalogerMonitor() + filesProcessed, packagesDiscovered := newPackageCatalogerMonitor() // perform analysis, accumulating errors for each failed analysis var errs error @@ -104,3 +108,18 @@ func packageFileOwnershipRelationships(p pkg.Package, resolver source.FilePathRe return relationships, nil } + +// newPackageCatalogerMonitor creates a new PackageCatalogerMonitor object and publishes the object on the bus as a PackageCatalogerStarted event. +func newPackageCatalogerMonitor() (*progress.Manual, *progress.Manual) { + filesProcessed := progress.Manual{} + packagesDiscovered := progress.Manual{} + + bus.Publish(partybus.Event{ + Type: event.PackageCatalogerStarted, + Value: monitor.PackageCatalogerMonitor{ + FilesProcessed: progress.Monitorable(&filesProcessed), + PackagesDiscovered: progress.Monitorable(&packagesDiscovered), + }, + }) + return &filesProcessed, &packagesDiscovered +} diff --git a/syft/source/directory_resolver.go b/syft/source/directory_resolver.go index b735f744e..64ac1ef74 100644 --- a/syft/source/directory_resolver.go +++ b/syft/source/directory_resolver.go @@ -11,10 +11,9 @@ import ( "runtime" "strings" - "github.com/anchore/syft/internal" - "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/stereoscope/pkg/filetree" + "github.com/anchore/syft/internal" "github.com/anchore/syft/internal/bus" "github.com/anchore/syft/internal/log" "github.com/anchore/syft/syft/event"