github.com/anchore/syft@v1.38.2/cmd/syft/internal/options/output_file.go (about) 1 package options 2 3 import ( 4 "fmt" 5 6 "github.com/anchore/clio" 7 "github.com/anchore/fangs" 8 "github.com/anchore/go-homedir" 9 "github.com/anchore/syft/syft/sbom" 10 ) 11 12 var _ interface { 13 clio.FlagAdder 14 clio.PostLoader 15 } = (*OutputFile)(nil) 16 17 // Deprecated: OutputFile supports the --file to write the SBOM output to 18 type OutputFile struct { 19 Enabled bool `yaml:"-" json:"-" mapstructure:"-"` 20 LegacyFile string `yaml:"-" json:"-" mapstructure:"legacyFile"` 21 } 22 23 func (o *OutputFile) AddFlags(flags clio.FlagSet) { 24 if o.Enabled { 25 flags.StringVarP(&o.LegacyFile, "file", "", 26 "file to write the default report output to (default is STDOUT)") 27 28 if pfp, ok := flags.(fangs.PFlagSetProvider); ok { 29 flagSet := pfp.PFlagSet() 30 flagSet.Lookup("file").Deprecated = "use: --output FORMAT=PATH" 31 } 32 } 33 } 34 35 func (o *OutputFile) PostLoad() error { 36 if !o.Enabled { 37 return nil 38 } 39 if o.LegacyFile != "" { 40 file, err := expandFilePath(o.LegacyFile) 41 if err != nil { 42 return err 43 } 44 o.LegacyFile = file 45 } 46 return nil 47 } 48 49 func (o *OutputFile) SBOMWriter(f sbom.FormatEncoder) (sbom.Writer, error) { 50 if !o.Enabled { 51 return nil, nil 52 } 53 writer, err := newSBOMMultiWriter(newSBOMWriterDescription(f, o.LegacyFile)) 54 if err != nil { 55 return nil, err 56 } 57 58 return writer, nil 59 } 60 61 func expandFilePath(file string) (string, error) { 62 if file != "" { 63 expandedPath, err := homedir.Expand(file) 64 if err != nil { 65 return "", fmt.Errorf("unable to expand file path=%q: %w", file, err) 66 } 67 file = expandedPath 68 } 69 return file, nil 70 }