From a6e1866cdb29af83a3f897ae6d8cd8c821fc60e7 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Mon, 13 Jul 2020 11:16:14 -0400 Subject: [PATCH] cmd: do not default for images, handle it specifically Signed-off-by: Alfredo Deza --- cmd/root.go | 49 +++++++++--------------------------- imgbom/distro/identify.go | 52 +++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 63 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b12c53b02..5cc1a0243 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,12 +7,10 @@ import ( "github.com/anchore/imgbom/imgbom" "github.com/anchore/imgbom/imgbom/event" "github.com/anchore/imgbom/imgbom/presenter" - "github.com/anchore/imgbom/imgbom/scope" "github.com/anchore/imgbom/internal" "github.com/anchore/imgbom/internal/bus" "github.com/anchore/imgbom/internal/log" "github.com/anchore/imgbom/internal/ui" - "github.com/anchore/stereoscope" "github.com/spf13/cobra" "github.com/wagoodman/go-partybus" ) @@ -50,54 +48,31 @@ func startWorker(userInput string) <-chan error { errs := make(chan error) go func() { defer close(errs) - protocol := imgbom.NewProtocol(userInput) - log.Debugf("protocol: %+v", protocol) - var s scope.Scope - var err error + s, cleanup, err := imgbom.NewScope(userInput, appConfig.ScopeOpt) + defer cleanup() - switch protocol.Type { - case imgbom.DirProtocol: - // populate the scope object for dir - s, err = imgbom.GetScopeFromDir(protocol.Value, appConfig.ScopeOpt) - if err != nil { - errs <- fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err) - } - - default: - log.Infof("Fetching image '%s'", userInput) - img, err := stereoscope.GetImage(userInput) - - if err != nil || img == nil { - errs <- fmt.Errorf("could not fetch image '%s': %w", userInput, err) - - // TODO: this needs to be handled better - bus.Publish(partybus.Event{ - Type: event.CatalogerFinished, - Value: nil, - }) - return - } - defer stereoscope.Cleanup() - - // populate the scope object for image - s, err = imgbom.GetScopeFromImage(img, appConfig.ScopeOpt) - if err != nil { - errs <- fmt.Errorf("could not populate scope with image: %w", err) - } + if err != nil { + log.Errorf("could not produce catalog: %w", err) } - log.Info("Identifying Distro") distro := imgbom.IdentifyDistro(s) + if distro == nil { log.Errorf("error identifying distro") } else { log.Infof(" Distro: %s", distro) } + log.Info("Creating the Catalog") + catalog, err := imgbom.Catalog(s) + + if err != nil { + log.Errorf("could not produce catalog: %w", err) + } bus.Publish(partybus.Event{ Type: event.CatalogerFinished, - Value: presenter.GetPresenter(appConfig.PresenterOpt, s), + Value: presenter.GetPresenter(appConfig.PresenterOpt, s, catalog), }) }() return errs diff --git a/imgbom/distro/identify.go b/imgbom/distro/identify.go index 5f489899a..07589962b 100644 --- a/imgbom/distro/identify.go +++ b/imgbom/distro/identify.go @@ -22,7 +22,6 @@ func Identify(s scope.Scope) *Distro { } for path, fn := range identityFiles { - // this is always a slice with a single ref, the API is odd because it was meant for images refs, err := s.FilesByPath(path) if err != nil { log.Errorf("unable to get path refs from %s: %s", path, err) @@ -32,33 +31,34 @@ func Identify(s scope.Scope) *Distro { if len(refs) == 0 { continue } - ref := refs[0] - contents, err := s.MultipleFileContentsByRef(ref) - log.Infof("contents are: %+v", contents) - content, ok := contents[ref] - // XXX is it possible to get a ref and no contents at all? - if !ok { - continue + for _, ref := range refs { + contents, err := s.MultipleFileContentsByRef(ref) + content, ok := contents[ref] + + if !ok { + log.Infof("no content present for ref: %s", ref) + continue + } + + if err != nil { + log.Debugf("unable to get contents from %s: %s", path, err) + continue + } + + if content == "" { + log.Debugf("no contents in file, skipping: %s", path) + continue + } + + distro := fn(content) + + if distro == nil { + continue + } + + return distro } - - if err != nil { - log.Debugf("unable to get contents from %s: %s", path, err) - continue - } - - if content == "" { - log.Debugf("no contents in file, skipping: %s", path) - continue - } - - distro := fn(content) - - if distro == nil { - continue - } - - return distro } // TODO: is it useful to know partially detected distros? where the ID is known but not the version (and viceversa?) return nil