Skip to content

Commit

Permalink
sbom: Generate function
Browse files Browse the repository at this point in the history
The SBOM object now outputs snoms using a generate function
that uses its generators to output in the specified formats

Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Mar 9, 2022
1 parent 3aff686 commit a853c97
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/sbom/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type Options struct {
// Working directory,inherited from buid context
WorkDir string

// OutputDir is the directory where the sboms will be written
OutputDir string

// FileName is the base name for the sboms, the proper extension will get appended
FileName string

// Formats dictates which SBOM formats we will output
Formats []string

Expand Down
47 changes: 46 additions & 1 deletion pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ var DefaultOptions = options.Options{
Name: "Alpine Linux",
Version: "Unknown",
},
Formats: []string{"cyclonedx"},
FileName: "sbom",
Formats: []string{"cyclonedx"},
}

type SBOM struct {
Expand All @@ -59,6 +60,13 @@ func New() *SBOM {
}
}

// NewWithWorkDir returns a new sbom object with a working dir preset
func NewWithWorkDir(path string) *SBOM {
s := New()
s.Options.WorkDir = path
return s
}

func (s *SBOM) ReadReleaseData() error {
if err := s.impl.readReleaseData(
&s.Options, filepath.Join(s.Options.WorkDir, osReleasePath),
Expand All @@ -80,10 +88,20 @@ func (s *SBOM) ReadPackageIndex() ([]*repository.Package, error) {
return pks, nil
}

// Generate creates the sboms according to the options set
func (s *SBOM) Generate() ([]string, error) {
files, err := s.impl.generate(&s.Options, s.Generators)
if err != nil {
return nil, fmt.Errorf("generating sboms: %w", err)
}
return files, nil
}

//counterfeiter:generate . sbomImplementation
type sbomImplementation interface {
readReleaseData(*options.Options, string) error
readPackageIndex(*options.Options, string) ([]*repository.Package, error)
generate(*options.Options, map[string]generator.Generator) ([]string, error)
}

type defaultSBOMImplementation struct{}
Expand Down Expand Up @@ -121,3 +139,30 @@ func (di *defaultSBOMImplementation) readPackageIndex(
}
return packages, nil
}

// generate creates the documents according to the specified options
func (di *defaultSBOMImplementation) generate(
opts *options.Options, generators map[string]generator.Generator,
) ([]string, error) {
// Check the generators before running
for _, format := range opts.Formats {
if _, ok := generators[format]; !ok {
return nil, fmt.Errorf(
"unable to generate sboms: no generator available for format %s", format,
)
}
}

files := []string{}

for _, format := range opts.Formats {
path := filepath.Join(
opts.OutputDir, opts.FileName+"."+generators[format].Ext(),
)
if err := generators[format].Generate(opts, path); err != nil {
return nil, fmt.Errorf("generating %s sbom: %w", format, err)
}
files = append(files, path)
}
return files, nil
}

0 comments on commit a853c97

Please sign in to comment.