github.com/anchore/syft@v1.38.2/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  	"github.com/anchore/clio"
    10  	intFile "github.com/anchore/syft/internal/file"
    11  	"github.com/anchore/syft/syft/file"
    12  )
    13  
    14  type fileConfig struct {
    15  	Metadata   fileMetadata   `yaml:"metadata" json:"metadata" mapstructure:"metadata"`
    16  	Content    fileContent    `yaml:"content" json:"content" mapstructure:"content"`
    17  	Executable fileExecutable `yaml:"executable" json:"executable" mapstructure:"executable"`
    18  }
    19  
    20  type fileMetadata struct {
    21  	Selection file.Selection `yaml:"selection" json:"selection" mapstructure:"selection"`
    22  	Digests   []string       `yaml:"digests" json:"digests" mapstructure:"digests"`
    23  }
    24  
    25  type fileContent struct {
    26  	SkipFilesAboveSize int64    `yaml:"skip-files-above-size" json:"skip-files-above-size" mapstructure:"skip-files-above-size"`
    27  	Globs              []string `yaml:"globs" json:"globs" mapstructure:"globs"`
    28  }
    29  
    30  type fileExecutable struct {
    31  	Globs []string `yaml:"globs" json:"globs" mapstructure:"globs"`
    32  }
    33  
    34  func defaultFileConfig() fileConfig {
    35  	return fileConfig{
    36  		Metadata: fileMetadata{
    37  			Selection: file.FilesOwnedByPackageSelection,
    38  			Digests:   []string{"sha1", "sha256"},
    39  		},
    40  		Content: fileContent{
    41  			SkipFilesAboveSize: 250 * intFile.KB,
    42  		},
    43  		Executable: fileExecutable{
    44  			Globs: nil,
    45  		},
    46  	}
    47  }
    48  
    49  var _ interface {
    50  	clio.PostLoader
    51  	clio.FieldDescriber
    52  } = (*fileConfig)(nil)
    53  
    54  func (c *fileConfig) PostLoad() error {
    55  	digests := strset.New(c.Metadata.Digests...).List()
    56  	sort.Strings(digests)
    57  	c.Metadata.Digests = digests
    58  
    59  	switch c.Metadata.Selection {
    60  	case file.NoFilesSelection, file.FilesOwnedByPackageSelection, file.AllFilesSelection:
    61  		return nil
    62  	}
    63  	return fmt.Errorf("invalid file metadata selection: %q", c.Metadata.Selection)
    64  }
    65  
    66  func (c *fileConfig) DescribeFields(descriptions clio.FieldDescriptionSet) {
    67  	descriptions.Add(&c.Metadata.Selection, `select which files should be captured by the file-metadata cataloger and included in the SBOM. 
    68  Options include:
    69   - "all": capture all files from the search space
    70   - "owned-by-package": capture only files owned by packages
    71   - "none", "": do not capture any files`)
    72  	descriptions.Add(&c.Metadata.Digests, `the file digest algorithms to use when cataloging files (options: "md5", "sha1", "sha224", "sha256", "sha384", "sha512")`)
    73  
    74  	descriptions.Add(&c.Content.SkipFilesAboveSize, `skip searching a file entirely if it is above the given size (default = 1MB; unit = bytes)`)
    75  	descriptions.Add(&c.Content.Globs, `file globs for the cataloger to match on`)
    76  
    77  	descriptions.Add(&c.Executable.Globs, `file globs for the cataloger to match on`)
    78  }