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

     1  package options
     2  
     3  import (
     4  	"github.com/anchore/clio"
     5  	"github.com/anchore/syft/syft/format/template"
     6  	"github.com/anchore/syft/syft/sbom"
     7  )
     8  
     9  var _ clio.FlagAdder = (*FormatTemplate)(nil)
    10  
    11  type FormatTemplate struct {
    12  	Enabled bool   `yaml:"-" json:"-" mapstructure:"-"`
    13  	Path    string `yaml:"path" json:"path" mapstructure:"path"` // -t template file to use for output
    14  }
    15  
    16  func DefaultFormatTemplate() FormatTemplate {
    17  	return FormatTemplate{
    18  		Enabled: true,
    19  	}
    20  }
    21  
    22  func (o *FormatTemplate) AddFlags(flags clio.FlagSet) {
    23  	if o.Enabled {
    24  		flags.StringVarP(&o.Path, "template", "t",
    25  			"specify the path to a Go template file")
    26  	}
    27  }
    28  
    29  func (o FormatTemplate) formatEncoders() ([]sbom.FormatEncoder, error) {
    30  	if !o.Enabled {
    31  		return nil, nil
    32  	}
    33  	enc, err := template.NewFormatEncoder(template.EncoderConfig{
    34  		TemplatePath: o.Path,
    35  	})
    36  	return []sbom.FormatEncoder{enc}, err
    37  }