From d982ad634534ed3191839ce63c082a7569a54638 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Tue, 14 Jul 2020 16:20:06 -0400 Subject: [PATCH] lib: implement a NewScope to produce scopes from inputs Signed-off-by: Alfredo Deza --- imgbom/lib.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/imgbom/lib.go b/imgbom/lib.go index 717111324..ec183fa34 100644 --- a/imgbom/lib.go +++ b/imgbom/lib.go @@ -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) }