github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/cmd/syft/cli/options/output_file.go (about)

     1  package options
     2  
     3  import (
     4  	"github.com/anchore/clio"
     5  	"github.com/anchore/fangs"
     6  	"github.com/anchore/syft/syft/sbom"
     7  )
     8  
     9  var _ interface {
    10  	clio.FlagAdder
    11  	clio.PostLoader
    12  } = (*OutputFile)(nil)
    13  
    14  // Deprecated: OutputFile supports the --file to write the SBOM output to
    15  type OutputFile struct {
    16  	Enabled bool   `yaml:"-" json:"-" mapstructure:"-"`
    17  	File    string `yaml:"file" json:"file" mapstructure:"file"`
    18  }
    19  
    20  func (o *OutputFile) AddFlags(flags clio.FlagSet) {
    21  	if o.Enabled {
    22  		flags.StringVarP(&o.File, "file", "",
    23  			"file to write the default report output to (default is STDOUT)")
    24  
    25  		if pfp, ok := flags.(fangs.PFlagSetProvider); ok {
    26  			flagSet := pfp.PFlagSet()
    27  			flagSet.Lookup("file").Deprecated = "use: output"
    28  		}
    29  	}
    30  }
    31  
    32  func (o *OutputFile) PostLoad() error {
    33  	if !o.Enabled {
    34  		return nil
    35  	}
    36  	if o.File != "" {
    37  		file, err := expandFilePath(o.File)
    38  		if err != nil {
    39  			return err
    40  		}
    41  		o.File = file
    42  	}
    43  	return nil
    44  }
    45  
    46  func (o *OutputFile) SBOMWriter(f sbom.FormatEncoder) (sbom.Writer, error) {
    47  	if !o.Enabled {
    48  		return nil, nil
    49  	}
    50  	writer, err := newSBOMMultiWriter(newSBOMWriterDescription(f, o.File))
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return writer, nil
    56  }