mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 02:26:42 +01:00
add cataloger selection example (#2646)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
parent
3598cb4f8f
commit
434b6ad506
78
examples/select_catalogers/main.go
Normal file
78
examples/select_catalogers/main.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/anchore/syft/syft"
|
||||||
|
"github.com/anchore/syft/syft/cataloging/pkgcataloging"
|
||||||
|
"github.com/anchore/syft/syft/sbom"
|
||||||
|
"github.com/anchore/syft/syft/source"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultImage = "alpine:3.19"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// automagically get a source.Source for arbitrary string input
|
||||||
|
src := getSource(imageReference())
|
||||||
|
|
||||||
|
// catalog the given source and return a SBOM
|
||||||
|
// let's explicitly use catalogers that are:
|
||||||
|
// - for installed software
|
||||||
|
// - used in the directory scan
|
||||||
|
sbom := getSBOM(src, pkgcataloging.InstalledTag, pkgcataloging.DirectoryTag)
|
||||||
|
|
||||||
|
// Show a basic catalogers and input configuration used
|
||||||
|
enc := json.NewEncoder(os.Stdout)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
if err := enc.Encode(sbom.Descriptor.Configuration); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func imageReference() string {
|
||||||
|
// read an image string reference from the command line or use a default
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
return os.Args[1]
|
||||||
|
}
|
||||||
|
return defaultImage
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSource(input string) source.Source {
|
||||||
|
detection, err := source.Detect(input,
|
||||||
|
source.DetectConfig{
|
||||||
|
DefaultImageSource: "docker",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := detection.NewSource(source.DefaultDetectionSourceConfig())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return src
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSBOM(src source.Source, defaultTags ...string) sbom.SBOM {
|
||||||
|
cfg := syft.DefaultCreateSBOMConfig().
|
||||||
|
WithCatalogerSelection(
|
||||||
|
// here you can sub-select, add, remove catalogers from the default selection...
|
||||||
|
// or replace the default selection entirely!
|
||||||
|
pkgcataloging.NewSelectionRequest().
|
||||||
|
WithDefaults(defaultTags...),
|
||||||
|
)
|
||||||
|
|
||||||
|
s, err := syft.CreateSBOM(context.Background(), src, cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return *s
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user