add and use event bus for UI progress

This commit is contained in:
Alex Goodman
2020-06-25 10:39:11 -04:00
parent 2e9e969c5f
commit 926b5f2a50
24 changed files with 725 additions and 96 deletions
+30
View File
@@ -4,11 +4,15 @@ import (
"github.com/anchore/imgbom/imgbom/cataloger/bundler"
"github.com/anchore/imgbom/imgbom/cataloger/dpkg"
"github.com/anchore/imgbom/imgbom/cataloger/python"
"github.com/anchore/imgbom/imgbom/event"
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/imgbom/internal/bus"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
"github.com/hashicorp/go-multierror"
"github.com/wagoodman/go-partybus"
"github.com/wagoodman/go-progress"
)
var controllerInstance controller
@@ -48,14 +52,36 @@ func (c *controller) add(a Cataloger) {
c.catalogers = append(c.catalogers, a)
}
type Monitor struct {
FilesProcessed progress.Monitorable
PackagesDiscovered progress.Monitorable
}
func (c *controller) trackCataloger() (*progress.Manual, *progress.Manual) {
filesProcessed := progress.Manual{}
packagesDiscovered := progress.Manual{}
bus.Publish(partybus.Event{
Type: event.CatalogerStarted,
Value: Monitor{
FilesProcessed: progress.Monitorable(&filesProcessed),
PackagesDiscovered: progress.Monitorable(&packagesDiscovered),
},
})
return &filesProcessed, &packagesDiscovered
}
func (c *controller) catalog(s scope.Scope) (*pkg.Catalog, error) {
catalog := pkg.NewCatalog()
fileSelection := make([]file.Reference, 0)
filesProcessed, packagesDiscovered := c.trackCataloger()
// ask catalogers for files to extract from the image tar
for _, a := range c.catalogers {
fileSelection = append(fileSelection, a.SelectFiles(&s)...)
log.Debugf("cataloger '%s' selected '%d' files", a.Name(), len(fileSelection))
filesProcessed.N += int64(len(fileSelection))
}
// fetch contents for requested selection by catalogers
@@ -75,6 +101,7 @@ func (c *controller) catalog(s scope.Scope) (*pkg.Catalog, error) {
}
log.Debugf("cataloger '%s' discovered '%d' packages", a.Name(), len(packages))
packagesDiscovered.N += int64(len(packages))
for _, p := range packages {
catalog.Add(p)
@@ -85,5 +112,8 @@ func (c *controller) catalog(s scope.Scope) (*pkg.Catalog, error) {
return nil, errs
}
filesProcessed.SetCompleted()
packagesDiscovered.SetCompleted()
return catalog, nil
}