github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/options/source.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/scylladb/go-set/strset"
     9  
    10  	"github.com/anchore/syft/syft/source/sourceproviders"
    11  )
    12  
    13  type sourceConfig struct {
    14  	Name     string      `json:"name" yaml:"name" mapstructure:"name"`
    15  	Version  string      `json:"version" yaml:"version" mapstructure:"version"`
    16  	BasePath string      `yaml:"base-path" json:"base-path" mapstructure:"base-path"` // specify base path for all file paths
    17  	File     fileSource  `json:"file" yaml:"file" mapstructure:"file"`
    18  	Image    imageSource `json:"image" yaml:"image" mapstructure:"image"`
    19  }
    20  
    21  type fileSource struct {
    22  	Digests []string `json:"digests" yaml:"digests" mapstructure:"digests"`
    23  }
    24  
    25  type imageSource struct {
    26  	DefaultPullSource string `json:"default-pull-source" yaml:"default-pull-source" mapstructure:"default-pull-source"`
    27  }
    28  
    29  func defaultSourceConfig() sourceConfig {
    30  	var digests []string
    31  	for _, alg := range sourceproviders.DefaultConfig().DigestAlgorithms {
    32  		digests = append(digests, alg.String())
    33  	}
    34  	return sourceConfig{
    35  		File: fileSource{
    36  			Digests: digests,
    37  		},
    38  	}
    39  }
    40  
    41  func (c *fileSource) PostLoad() error {
    42  	digests := strset.New(c.Digests...).List()
    43  	sort.Strings(digests)
    44  	c.Digests = digests
    45  	return nil
    46  }
    47  
    48  func (c imageSource) PostLoad() error {
    49  	return checkDefaultSourceValues(c.DefaultPullSource)
    50  }
    51  
    52  var validDefaultSourceValues = []string{"registry", "docker", "podman", ""}
    53  
    54  func checkDefaultSourceValues(source string) error {
    55  	validValues := strset.New(validDefaultSourceValues...)
    56  	if !validValues.Has(source) {
    57  		validValuesString := strings.Join(validDefaultSourceValues, ", ")
    58  		return fmt.Errorf("%s is not a valid default source; please use one of the following: %s''", source, validValuesString)
    59  	}
    60  
    61  	return nil
    62  }