github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/examples/source_detection/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  
     8  	"github.com/anchore/go-collections"
     9  	"github.com/anchore/stereoscope"
    10  	"github.com/anchore/syft/syft"
    11  	"github.com/anchore/syft/syft/source"
    12  	"github.com/anchore/syft/syft/source/sourceproviders"
    13  )
    14  
    15  /*
    16   This example demonstrates how to create a source object from a generic string input.
    17  
    18   Example inputs:
    19      alpine:3.19                              pull an image from the docker daemon, podman, or the registry (based on what's available)
    20      ./my.tar                                 interpret a local archive as an OCI archive, docker save archive, or raw file from disk to catalog
    21      docker:yourrepo/yourimage:tag            explicitly use the Docker daemon
    22      podman:yourrepo/yourimage:tag            explicitly use the Podman daemon
    23      registry:yourrepo/yourimage:tag          pull image directly from a registry (no container runtime required)
    24      docker-archive:path/to/yourimage.tar     use a tarball from disk for archives created from "docker save"
    25      oci-archive:path/to/yourimage.tar        use a tarball from disk for OCI archives (from Skopeo or otherwise)
    26      oci-dir:path/to/yourimage                read directly from a path on disk for OCI layout directories (from Skopeo or otherwise)
    27      singularity:path/to/yourimage.sif        read directly from a Singularity Image Format (SIF) container on disk
    28      dir:path/to/yourproject                  read directly from a path on disk (any directory)
    29      file:path/to/yourproject/file            read directly from a path on disk (any single file)
    30  
    31  */
    32  
    33  const defaultImage = "alpine:3.19"
    34  
    35  func main() {
    36  	userInput := imageReference()
    37  
    38  	// parse the scheme against the known set of schemes
    39  	schemeSource, newUserInput := stereoscope.ExtractSchemeSource(userInput, allSourceTags()...)
    40  
    41  	// set up the GetSourceConfig
    42  	getSourceCfg := syft.DefaultGetSourceConfig()
    43  	if schemeSource != "" {
    44  		getSourceCfg = getSourceCfg.WithSources(schemeSource)
    45  		userInput = newUserInput
    46  	}
    47  	src, err := syft.GetSource(context.Background(), userInput, getSourceCfg)
    48  
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	// Show a basic description of the source to the screen
    54  	enc := json.NewEncoder(os.Stdout)
    55  	enc.SetIndent("", "  ")
    56  	if err := enc.Encode(src.Describe()); err != nil {
    57  		panic(err)
    58  	}
    59  }
    60  
    61  func imageReference() string {
    62  	// read an image string reference from the command line or use a default
    63  	if len(os.Args) > 1 {
    64  		return os.Args[1]
    65  	}
    66  	return defaultImage
    67  }
    68  
    69  func allSourceTags() []string {
    70  	return collections.TaggedValueSet[source.Provider]{}.Join(sourceproviders.All("", nil)...).Tags()
    71  }