github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/examples/source_from_image/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "encoding/json" 6 "os" 7 8 "github.com/anchore/stereoscope" 9 "github.com/anchore/stereoscope/pkg/image/oci" 10 "github.com/anchore/syft/syft/source/stereoscopesource" 11 ) 12 13 /* 14 This shows how to create a source from an image reference. This is useful when you are programmatically always 15 expecting to catalog a container image and always from the same source (e.g. docker daemon, podman, registry, etc). 16 */ 17 18 const defaultImage = "alpine:3.19" 19 20 func main() { 21 // using oci.Registry causes the lookup to always use the registry, there are several other "Source" options here 22 img, err := stereoscope.GetImageFromSource(context.Background(), imageReference(), oci.Registry, stereoscope.WithPlatform("linux/amd64")) 23 if err != nil { 24 panic(err) 25 } 26 27 src := stereoscopesource.New(img, stereoscopesource.ImageConfig{ 28 Reference: imageReference(), 29 }) 30 31 // Show a basic description of the source to the screen 32 enc := json.NewEncoder(os.Stdout) 33 enc.SetIndent("", " ") 34 if err := enc.Encode(src.Describe()); err != nil { 35 panic(err) 36 } 37 } 38 39 func imageReference() string { 40 // read an image string reference from the command line or use a default 41 if len(os.Args) > 1 { 42 return os.Args[1] 43 } 44 return defaultImage 45 }