github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/cli/options/output.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/exp/slices"
     7  
     8  	"github.com/anchore/clio"
     9  	"github.com/anchore/syft/syft/formats"
    10  	"github.com/anchore/syft/syft/formats/table"
    11  	"github.com/anchore/syft/syft/formats/template"
    12  	"github.com/anchore/syft/syft/sbom"
    13  )
    14  
    15  // MultiOutput has the standard output options syft accepts: multiple -o, --file, --template
    16  type MultiOutput struct {
    17  	Outputs            []string `yaml:"output" json:"output" mapstructure:"output"` // -o, the format to use for output
    18  	OutputFile         `yaml:",inline" json:"" mapstructure:",squash"`
    19  	OutputTemplatePath string `yaml:"output-template-path" json:"output-template-path" mapstructure:"output-template-path"` // -t template file to use for output
    20  }
    21  
    22  var _ interface {
    23  	clio.FlagAdder
    24  } = (*MultiOutput)(nil)
    25  
    26  func DefaultOutput() MultiOutput {
    27  	return MultiOutput{
    28  		Outputs: []string{string(table.ID)},
    29  	}
    30  }
    31  
    32  func (o *MultiOutput) AddFlags(flags clio.FlagSet) {
    33  	flags.StringArrayVarP(&o.Outputs, "output", "o",
    34  		fmt.Sprintf("report output format, options=%v", formats.AllIDs()))
    35  
    36  	flags.StringVarP(&o.OutputTemplatePath, "template", "t",
    37  		"specify the path to a Go template file")
    38  }
    39  
    40  func (o *MultiOutput) SBOMWriter() (sbom.Writer, error) {
    41  	return makeSBOMWriter(o.Outputs, o.File, o.OutputTemplatePath)
    42  }
    43  
    44  // SingleOutput allows only 1 output to be specified, with a user able to set what options are allowed by setting AllowableOptions
    45  type SingleOutput struct {
    46  	AllowableOptions   []string `yaml:"-" json:"-" mapstructure:"-"`
    47  	Output             string   `yaml:"output" json:"output" mapstructure:"output"`
    48  	OutputTemplatePath string   `yaml:"output-template-path" json:"output-template-path" mapstructure:"output-template-path"` // -t template file to use for output
    49  }
    50  
    51  var _ clio.FlagAdder = (*SingleOutput)(nil)
    52  
    53  func (o *SingleOutput) AddFlags(flags clio.FlagSet) {
    54  	flags.StringVarP(&o.Output, "output", "o",
    55  		fmt.Sprintf("report output format, options=%v", o.AllowableOptions))
    56  
    57  	if slices.Contains(o.AllowableOptions, template.ID.String()) {
    58  		flags.StringVarP(&o.OutputTemplatePath, "template", "t",
    59  			"specify the path to a Go template file")
    60  	}
    61  }
    62  
    63  func (o *SingleOutput) SBOMWriter(file string) (sbom.Writer, error) {
    64  	return makeSBOMWriter([]string{o.Output}, file, o.OutputTemplatePath)
    65  }
    66  
    67  // OutputFile is only the --file argument
    68  type OutputFile struct {
    69  	File string `yaml:"file" json:"file" mapstructure:"file"` // --file, the file to write report output to
    70  }
    71  
    72  var _ interface {
    73  	clio.FlagAdder
    74  	clio.PostLoader
    75  } = (*OutputFile)(nil)
    76  
    77  func (o *OutputFile) AddFlags(flags clio.FlagSet) {
    78  	flags.StringVarP(&o.File, "file", "",
    79  		"file to write the default report output to (default is STDOUT)")
    80  }
    81  
    82  func (o *OutputFile) PostLoad() error {
    83  	if o.File != "" {
    84  		file, err := expandFilePath(o.File)
    85  		if err != nil {
    86  			return err
    87  		}
    88  		o.File = file
    89  	}
    90  	return nil
    91  }
    92  
    93  func (o *OutputFile) SBOMWriter(f sbom.Format) (sbom.Writer, error) {
    94  	return makeSBOMWriterForFormat(f, o.File)
    95  }