cmd: do not default for images, handle it specifically

Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
Alfredo Deza 2020-07-13 11:16:14 -04:00
parent 2f626bf9fd
commit a6e1866cdb
2 changed files with 38 additions and 63 deletions

View File

@ -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)
log.Errorf("could not produce catalog: %w", 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)
}
}
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

View File

@ -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,13 +31,13 @@ func Identify(s scope.Scope) *Distro {
if len(refs) == 0 {
continue
}
ref := refs[0]
for _, ref := range refs {
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 {
log.Infof("no content present for ref: %s", ref)
continue
}
@ -60,6 +59,7 @@ func Identify(s scope.Scope) *Distro {
return distro
}
}
// TODO: is it useful to know partially detected distros? where the ID is known but not the version (and viceversa?)
return nil
}