@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "bytes"
5+ "context"
56 "errors"
67 "fmt"
78 "strings"
@@ -19,7 +20,9 @@ import (
1920 "github.com/wagoodman/go-partybus"
2021
2122 "github.com/anchore/stereoscope"
23+ "github.com/anchore/stereoscope/pkg/file"
2224 "github.com/anchore/stereoscope/pkg/image"
25+ stereoscopeDocker "github.com/anchore/stereoscope/pkg/image/docker"
2326 "github.com/anchore/syft/syft"
2427 "github.com/anchore/syft/syft/event"
2528 "github.com/anchore/syft/syft/pkg/cataloger"
@@ -37,7 +40,7 @@ const (
3740 shortDescription = "View the packaged-based Software Bill Of Materials (SBOM) for an image"
3841)
3942
40- func cmd (_ command.Cli ) * cobra.Command {
43+ func cmd (dockerCli command.Cli ) * cobra.Command {
4144 c := & cobra.Command {
4245 Use : "sbom" ,
4346 Short : shortDescription ,
@@ -47,7 +50,7 @@ func cmd(_ command.Cli) *cobra.Command {
4750 SilenceUsage : true ,
4851 SilenceErrors : true ,
4952 Version : version .FromBuild ().Version ,
50- RunE : run ,
53+ RunE : newRunner ( dockerCli ). run ,
5154 ValidArgsFunction : dockerImageValidArgsFunction ,
5255 }
5356
@@ -174,7 +177,17 @@ func validateInputArgs(cmd *cobra.Command, args []string) error {
174177 return cobra .ExactArgs (1 )(cmd , args )
175178}
176179
177- func run (_ * cobra.Command , args []string ) error {
180+ type runner struct {
181+ client command.Cli
182+ }
183+
184+ func newRunner (client command.Cli ) runner {
185+ return runner {
186+ client : client ,
187+ }
188+ }
189+
190+ func (r runner ) run (_ * cobra.Command , args []string ) error {
178191 writer , err := makeWriter ([]string {appConfig .Format }, appConfig .Output )
179192 if err != nil {
180193 return err
@@ -186,16 +199,16 @@ func run(_ *cobra.Command, args []string) error {
186199 }
187200 }()
188201
189- si := source. Input {
190- UserInput : args [ 0 ],
191- Scheme : source . ImageScheme ,
192- ImageSource : image . DockerDaemonSource ,
193- Location : args [ 0 ],
194- Platform : appConfig . Platform ,
202+ var platform * image. Platform
203+ if appConfig . Platform != "" {
204+ platform , err = image . NewPlatform ( appConfig . Platform )
205+ if err != nil {
206+ return fmt . Errorf ( "invalid platform provided: %w" , err )
207+ }
195208 }
196209
197210 return eventLoop (
198- sbomExecWorker (si , writer ),
211+ sbomExecWorker (args [ 0 ], r . client , platform , writer ),
199212 setupSignals (),
200213 eventSubscription ,
201214 stereoscope .Cleanup ,
@@ -236,21 +249,42 @@ func generateSBOM(src *source.Source) (*sbom.SBOM, error) {
236249 return & s , nil
237250}
238251
239- func sbomExecWorker (si source. Input , writer sbom.Writer ) <- chan error {
252+ func sbomExecWorker (userInput string , dockerCli command. Cli , platform * image. Platform , writer sbom.Writer ) <- chan error {
240253 errs := make (chan error )
241254 go func () {
242255 defer close (errs )
243256
244- src , cleanup , err := source .New (si , nil , appConfig .Exclusions )
245- if cleanup != nil {
246- defer cleanup ()
257+ provider := stereoscopeDocker .NewProviderFromDaemon (
258+ userInput ,
259+ file .NewTempDirGenerator (internal .ApplicationName ),
260+ dockerCli .Client (),
261+ platform ,
262+ )
263+ img , err := provider .Provide (context .Background ())
264+ defer func () {
265+ if err := img .Cleanup (); err != nil {
266+ log .Warnf ("failed to clean up image: %+v" , err )
267+ }
268+ }()
269+ if err != nil {
270+ errs <- fmt .Errorf ("failed to fetch the image %q: %w" , userInput , err )
271+ return
247272 }
273+
274+ err = img .Read ()
275+ if err != nil {
276+ errs <- fmt .Errorf ("failed to read the image %q: %w" , userInput , err )
277+ return
278+ }
279+
280+ src , err := source .NewFromImage (img , userInput )
248281 if err != nil {
249- errs <- fmt .Errorf ("failed to construct source from user input %q: %w" , si . UserInput , err )
282+ errs <- fmt .Errorf ("failed to construct source from user input %q: %w" , userInput , err )
250283 return
251284 }
285+ src .Exclusions = appConfig .Exclusions
252286
253- s , err := generateSBOM (src )
287+ s , err := generateSBOM (& src )
254288 if err != nil {
255289 errs <- err
256290 return
0 commit comments