2024-02-02 16:26:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-27 21:44:37 +00:00
|
|
|
"context"
|
2024-02-02 16:26:44 +00:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
2024-02-27 21:44:37 +00:00
|
|
|
"github.com/anchore/stereoscope"
|
|
|
|
"github.com/anchore/stereoscope/pkg/image/oci"
|
|
|
|
"github.com/anchore/syft/syft/source/stereoscopesource"
|
2024-02-02 16:26:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This shows how to create a source from an image reference. This is useful when you are programmatically always
|
|
|
|
expecting to catalog a container image and always from the same source (e.g. docker daemon, podman, registry, etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
const defaultImage = "alpine:3.19"
|
|
|
|
|
|
|
|
func main() {
|
2024-02-27 21:44:37 +00:00
|
|
|
// using oci.Registry causes the lookup to always use the registry, there are several other "Source" options here
|
|
|
|
img, err := stereoscope.GetImageFromSource(context.Background(), imageReference(), oci.Registry, stereoscope.WithPlatform("linux/amd64"))
|
2024-02-02 16:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:44:37 +00:00
|
|
|
src := stereoscopesource.New(img, stereoscopesource.ImageConfig{
|
|
|
|
Reference: imageReference(),
|
|
|
|
})
|
2024-02-02 16:26:44 +00:00
|
|
|
|
|
|
|
// Show a basic description of the source to the screen
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(src.Describe()); 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
|
|
|
|
}
|