github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/cmd/syft/cli/options/packages.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  	"github.com/spf13/pflag"
     8  	"github.com/spf13/viper"
     9  
    10  	"github.com/kastenhq/syft/syft/formats"
    11  	"github.com/kastenhq/syft/syft/formats/table"
    12  	"github.com/kastenhq/syft/syft/pkg/cataloger"
    13  	"github.com/kastenhq/syft/syft/source"
    14  )
    15  
    16  type PackagesOptions struct {
    17  	Scope              string
    18  	Output             []string
    19  	OutputTemplatePath string
    20  	File               string
    21  	Platform           string
    22  	Exclude            []string
    23  	Catalogers         []string
    24  	SourceName         string
    25  	SourceVersion      string
    26  	BasePath           string
    27  }
    28  
    29  var _ Interface = (*PackagesOptions)(nil)
    30  
    31  func (o *PackagesOptions) AddFlags(cmd *cobra.Command, v *viper.Viper) error {
    32  	cmd.Flags().StringVarP(&o.Scope, "scope", "s", cataloger.DefaultSearchConfig().Scope.String(),
    33  		fmt.Sprintf("selection of layers to catalog, options=%v", source.AllScopes))
    34  
    35  	cmd.Flags().StringArrayVarP(&o.Output, "output", "o", []string{string(table.ID)},
    36  		fmt.Sprintf("report output format, options=%v", formats.AllIDs()))
    37  
    38  	cmd.Flags().StringVarP(&o.File, "file", "", "",
    39  		"file to write the default report output to (default is STDOUT)")
    40  
    41  	cmd.Flags().StringVarP(&o.OutputTemplatePath, "template", "t", "",
    42  		"specify the path to a Go template file")
    43  
    44  	cmd.Flags().StringVarP(&o.Platform, "platform", "", "",
    45  		"an optional platform specifier for container image sources (e.g. 'linux/arm64', 'linux/arm64/v8', 'arm64', 'linux')")
    46  
    47  	cmd.Flags().StringArrayVarP(&o.Exclude, "exclude", "", nil,
    48  		"exclude paths from being scanned using a glob expression")
    49  
    50  	cmd.Flags().StringArrayVarP(&o.Catalogers, "catalogers", "", nil,
    51  		"enable one or more package catalogers")
    52  
    53  	cmd.Flags().StringVarP(&o.SourceName, "name", "", "",
    54  		"set the name of the target being analyzed")
    55  	cmd.Flags().Lookup("name").Deprecated = "use: source-name"
    56  
    57  	cmd.Flags().StringVarP(&o.SourceName, "source-name", "", "",
    58  		"set the name of the target being analyzed")
    59  
    60  	cmd.Flags().StringVarP(&o.SourceVersion, "source-version", "", "",
    61  		"set the name of the target being analyzed")
    62  
    63  	cmd.Flags().StringVarP(&o.BasePath, "base-path", "", "",
    64  		"base directory for scanning, no links will be followed above this directory, and all paths will be reported relative to this directory")
    65  
    66  	return bindPackageConfigOptions(cmd.Flags(), v)
    67  }
    68  
    69  //nolint:revive
    70  func bindPackageConfigOptions(flags *pflag.FlagSet, v *viper.Viper) error {
    71  	// Formatting & Input options //////////////////////////////////////////////
    72  
    73  	if err := v.BindPFlag("package.cataloger.scope", flags.Lookup("scope")); err != nil {
    74  		return err
    75  	}
    76  
    77  	if err := v.BindPFlag("file", flags.Lookup("file")); err != nil {
    78  		return err
    79  	}
    80  
    81  	if err := v.BindPFlag("exclude", flags.Lookup("exclude")); err != nil {
    82  		return err
    83  	}
    84  
    85  	if err := v.BindPFlag("catalogers", flags.Lookup("catalogers")); err != nil {
    86  		return err
    87  	}
    88  
    89  	if err := v.BindPFlag("name", flags.Lookup("name")); err != nil {
    90  		return err
    91  	}
    92  
    93  	if err := v.BindPFlag("source.name", flags.Lookup("source-name")); err != nil {
    94  		return err
    95  	}
    96  
    97  	if err := v.BindPFlag("source.version", flags.Lookup("source-version")); err != nil {
    98  		return err
    99  	}
   100  
   101  	if err := v.BindPFlag("output", flags.Lookup("output")); err != nil {
   102  		return err
   103  	}
   104  
   105  	if err := v.BindPFlag("output-template-path", flags.Lookup("template")); err != nil {
   106  		return err
   107  	}
   108  
   109  	if err := v.BindPFlag("platform", flags.Lookup("platform")); err != nil {
   110  		return err
   111  	}
   112  
   113  	if err := v.BindPFlag("base-path", flags.Lookup("base-path")); err != nil {
   114  		return err
   115  	}
   116  
   117  	return nil
   118  }