diff --git a/pkg/sbom/sbom.go b/pkg/sbom/sbom.go index f454e2d3e..2bd3f900d 100644 --- a/pkg/sbom/sbom.go +++ b/pkg/sbom/sbom.go @@ -90,6 +90,11 @@ func (s *SBOM) ReadPackageIndex() ([]*repository.Package, error) { // Generate creates the sboms according to the options set func (s *SBOM) Generate() ([]string, error) { + if err := s.impl.checkGenerators( + &s.Options, s.Generators, + ); err != nil { + return nil, err + } files, err := s.impl.generate(&s.Options, s.Generators) if err != nil { return nil, fmt.Errorf("generating sboms: %w", err) @@ -102,6 +107,7 @@ type sbomImplementation interface { readReleaseData(*options.Options, string) error readPackageIndex(*options.Options, string) ([]*repository.Package, error) generate(*options.Options, map[string]generator.Generator) ([]string, error) + checkGenerators(*options.Options, map[string]generator.Generator) error } type defaultSBOMImplementation struct{} @@ -166,3 +172,18 @@ func (di *defaultSBOMImplementation) generate( } return files, nil } + +// checkGenerators verifies we have generators available for the +// formats specified in the options +func (di *defaultSBOMImplementation) checkGenerators( + opts *options.Options, generators map[string]generator.Generator, +) error { + for _, format := range opts.Formats { + if _, ok := generators[format]; !ok { + return fmt.Errorf( + "unable to generate sboms: no generator available for format %s", format, + ) + } + } + return nil +}