lib: implement a NewScope to produce scopes from inputs

Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
Alfredo Deza 2020-07-14 16:20:06 -04:00
parent b2c8685437
commit d982ad6345

View File

@ -1,6 +1,8 @@
package imgbom
import (
"fmt"
"github.com/anchore/imgbom/imgbom/cataloger"
"github.com/anchore/imgbom/imgbom/distro"
"github.com/anchore/imgbom/imgbom/logger"
@ -8,6 +10,7 @@ import (
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/imgbom/internal/bus"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope"
"github.com/anchore/stereoscope/pkg/image"
"github.com/wagoodman/go-partybus"
)
@ -16,6 +19,42 @@ func IdentifyDistro(s scope.Scope) *distro.Distro {
return distro.Identify(s)
}
// NewScope produces a Scope based on userInput like dir:// or image:tag
func NewScope(userInput string, o scope.Option) (scope.Scope, func(), error) {
protocol := NewProtocol(userInput)
log.Debugf("protocol: %+v", protocol)
switch protocol.Type {
case DirProtocol:
// populate the scope object for dir
s, err := GetScopeFromDir(protocol.Value, o)
if err != nil {
return scope.Scope{}, func() {}, fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err)
}
return s, func() {}, nil
case ImageProtocol:
log.Infof("Fetching image '%s'", userInput)
img, err := stereoscope.GetImage(userInput)
cleanup := func() {
stereoscope.Cleanup()
}
if err != nil || img == nil {
return scope.Scope{}, cleanup, fmt.Errorf("could not fetch image '%s': %w", userInput, err)
}
s, err := GetScopeFromImage(img, o)
if err != nil {
return scope.Scope{}, cleanup, fmt.Errorf("could not populate scope with image: %w", err)
}
return s, cleanup, nil
default:
return scope.Scope{}, func() {}, fmt.Errorf("unable to process input for scanning: '%s'", userInput)
}
}
func GetScopeFromDir(d string, o scope.Option) (scope.Scope, error) {
return scope.NewScopeFromDir(d, o)
}