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

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/scylladb/go-set/strset"
     8  
     9  	intFile "github.com/anchore/syft/internal/file"
    10  	"github.com/anchore/syft/syft/file"
    11  )
    12  
    13  type fileConfig struct {
    14  	Metadata   fileMetadata   `yaml:"metadata" json:"metadata" mapstructure:"metadata"`
    15  	Content    fileContent    `yaml:"content" json:"content" mapstructure:"content"`
    16  	Executable fileExecutable `yaml:"executable" json:"executable" mapstructure:"executable"`
    17  }
    18  
    19  type fileMetadata struct {
    20  	Selection file.Selection `yaml:"selection" json:"selection" mapstructure:"selection"`
    21  	Digests   []string       `yaml:"digests" json:"digests" mapstructure:"digests"`
    22  }
    23  
    24  type fileContent struct {
    25  	SkipFilesAboveSize int64    `yaml:"skip-files-above-size" json:"skip-files-above-size" mapstructure:"skip-files-above-size"`
    26  	Globs              []string `yaml:"globs" json:"globs" mapstructure:"globs"`
    27  }
    28  
    29  type fileExecutable struct {
    30  	Globs []string `yaml:"globs" json:"globs" mapstructure:"globs"`
    31  }
    32  
    33  func defaultFileConfig() fileConfig {
    34  	return fileConfig{
    35  		Metadata: fileMetadata{
    36  			Selection: file.FilesOwnedByPackageSelection,
    37  			Digests:   []string{"sha1", "sha256"},
    38  		},
    39  		Content: fileContent{
    40  			SkipFilesAboveSize: 250 * intFile.KB,
    41  		},
    42  		Executable: fileExecutable{
    43  			Globs: nil,
    44  		},
    45  	}
    46  }
    47  
    48  func (c *fileConfig) PostLoad() error {
    49  	digests := strset.New(c.Metadata.Digests...).List()
    50  	sort.Strings(digests)
    51  	c.Metadata.Digests = digests
    52  
    53  	switch c.Metadata.Selection {
    54  	case file.NoFilesSelection, file.FilesOwnedByPackageSelection, file.AllFilesSelection:
    55  		return nil
    56  	}
    57  	return fmt.Errorf("invalid file metadata selection: %q", c.Metadata.Selection)
    58  }