Skip to content

Commit

Permalink
Spin checkGenerators from the impl generate fn
Browse files Browse the repository at this point in the history
Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Mar 9, 2022
1 parent 1f92704 commit 62b37aa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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
}

0 comments on commit 62b37aa

Please sign in to comment.