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