Skip to content

Instantly share code, notes, and snippets.

@raisinbl
Last active November 20, 2023 03:46
Show Gist options
  • Save raisinbl/f51f91053676f07390eb3cb182e46933 to your computer and use it in GitHub Desktop.
Save raisinbl/f51f91053676f07390eb3cb182e46933 to your computer and use it in GitHub Desktop.
minimal using Syft as library ver 0.94
package main
import (
// "github.com/anchore/syft/syft/pkg/cataloger/python"
"fmt"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/format/cyclonedxjson"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)
func main(){
userInput := "test-fixture/python/requirements.txt"
detection, err := source.Detect(userInput, source.DefaultDetectConfig())
theSource, err := detection.NewSource(source.DefaultDetectionSourceConfig())
// TODO: this would be better with functional options (after/during API refactor)
c := cataloger.DefaultConfig()
c.Search.Scope = source.AllLayersScope
pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, c)
if err != nil {
panic(err)
}
detectedSbom := sbom.SBOM{
Artifacts: sbom.Artifacts{
Packages: pkgCatalog,
LinuxDistribution: actualDistro,
},
Relationships: relationships,
Source: theSource.Describe(),
}
cjsonEncoder,err := cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())
bytes, err := format.Encode(detectedSbom, cjsonEncoder)
if nil != err {
panic(err)
}
fmt.Println(string(bytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment