github.com/anchore/syft@v1.38.2/syft/get_source_config.go (about)

     1  package syft
     2  
     3  import (
     4  	"crypto"
     5  	"fmt"
     6  
     7  	"github.com/anchore/go-collections"
     8  	"github.com/anchore/stereoscope/pkg/image"
     9  	"github.com/anchore/syft/syft/source"
    10  	"github.com/anchore/syft/syft/source/sourceproviders"
    11  )
    12  
    13  type GetSourceConfig struct {
    14  	// SourceProviderConfig may optionally be provided to be used when constructing the default set of source providers, unused if All specified
    15  	SourceProviderConfig *sourceproviders.Config
    16  
    17  	// Sources is an explicit list of source names to use, in order, to attempt to locate a source
    18  	Sources []string
    19  
    20  	// DefaultImagePullSource will cause a particular image pull source to be used as the first pull source, followed by other pull sources
    21  	DefaultImagePullSource string
    22  }
    23  
    24  func (c *GetSourceConfig) WithAlias(alias source.Alias) *GetSourceConfig {
    25  	c.SourceProviderConfig = c.SourceProviderConfig.WithAlias(alias)
    26  	return c
    27  }
    28  
    29  func (c *GetSourceConfig) WithRegistryOptions(registryOptions *image.RegistryOptions) *GetSourceConfig {
    30  	c.SourceProviderConfig = c.SourceProviderConfig.WithRegistryOptions(registryOptions)
    31  	return c
    32  }
    33  
    34  func (c *GetSourceConfig) WithPlatform(platform *image.Platform) *GetSourceConfig {
    35  	c.SourceProviderConfig = c.SourceProviderConfig.WithPlatform(platform)
    36  	return c
    37  }
    38  
    39  func (c *GetSourceConfig) WithExcludeConfig(excludeConfig source.ExcludeConfig) *GetSourceConfig {
    40  	c.SourceProviderConfig = c.SourceProviderConfig.WithExcludeConfig(excludeConfig)
    41  	return c
    42  }
    43  
    44  func (c *GetSourceConfig) WithDigestAlgorithms(algorithms ...crypto.Hash) *GetSourceConfig {
    45  	c.SourceProviderConfig = c.SourceProviderConfig.WithDigestAlgorithms(algorithms...)
    46  	return c
    47  }
    48  
    49  func (c *GetSourceConfig) WithBasePath(basePath string) *GetSourceConfig {
    50  	c.SourceProviderConfig = c.SourceProviderConfig.WithBasePath(basePath)
    51  	return c
    52  }
    53  
    54  func (c *GetSourceConfig) WithSources(sources ...string) *GetSourceConfig {
    55  	c.Sources = sources
    56  	return c
    57  }
    58  
    59  func (c *GetSourceConfig) WithDefaultImagePullSource(defaultImagePullSource string) *GetSourceConfig {
    60  	c.DefaultImagePullSource = defaultImagePullSource
    61  	return c
    62  }
    63  
    64  func (c *GetSourceConfig) getProviders(userInput string) ([]source.Provider, error) {
    65  	providers := collections.TaggedValueSet[source.Provider]{}.Join(sourceproviders.All(userInput, c.SourceProviderConfig)...)
    66  
    67  	// if the "default image pull source" is set, we move this as the first pull source
    68  	if c.DefaultImagePullSource != "" {
    69  		base := providers.Remove(sourceproviders.PullTag)
    70  		pull := providers.Select(sourceproviders.PullTag)
    71  		def := pull.Select(c.DefaultImagePullSource)
    72  		if len(def) == 0 {
    73  			return nil, fmt.Errorf("invalid DefaultImagePullSource: %s; available values are: %v", c.DefaultImagePullSource, pull.Tags())
    74  		}
    75  		providers = base.Join(def...).Join(pull...)
    76  	}
    77  
    78  	// narrow the sources to those explicitly requested generally by a user
    79  	if len(c.Sources) > 0 {
    80  		// select the explicitly provided sources, in order
    81  		providers = providers.Select(c.Sources...)
    82  	}
    83  
    84  	return providers.Values(), nil
    85  }
    86  
    87  func DefaultGetSourceConfig() *GetSourceConfig {
    88  	return &GetSourceConfig{
    89  		SourceProviderConfig: sourceproviders.DefaultConfig(),
    90  	}
    91  }