github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/template/format.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/nextlinux/gosbom/gosbom/formats/gosbomjson"
     8  	"github.com/nextlinux/gosbom/gosbom/sbom"
     9  )
    10  
    11  const ID sbom.FormatID = "template"
    12  
    13  func Format() sbom.Format {
    14  	return OutputFormat{}
    15  }
    16  
    17  // implementation of sbom.Format interface
    18  // to make use of format options
    19  type OutputFormat struct {
    20  	templateFilePath string
    21  }
    22  
    23  func (f OutputFormat) ID() sbom.FormatID {
    24  	return ID
    25  }
    26  
    27  func (f OutputFormat) IDs() []sbom.FormatID {
    28  	return []sbom.FormatID{ID}
    29  }
    30  
    31  func (f OutputFormat) Version() string {
    32  	return sbom.AnyVersion
    33  }
    34  
    35  func (f OutputFormat) String() string {
    36  	return fmt.Sprintf("template: " + f.templateFilePath)
    37  }
    38  
    39  func (f OutputFormat) Decode(_ io.Reader) (*sbom.SBOM, error) {
    40  	return nil, sbom.ErrDecodingNotSupported
    41  }
    42  
    43  func (f OutputFormat) Encode(output io.Writer, s sbom.SBOM) error {
    44  	tmpl, err := makeTemplateExecutor(f.templateFilePath)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	doc := gosbomjson.ToFormatModel(s)
    50  	return tmpl.Execute(output, doc)
    51  }
    52  
    53  func (f OutputFormat) Validate(_ io.Reader) error {
    54  	return sbom.ErrValidationNotSupported
    55  }
    56  
    57  // SetTemplatePath sets path for template file
    58  func (f *OutputFormat) SetTemplatePath(filePath string) {
    59  	f.templateFilePath = filePath
    60  }
    61  
    62  var _ sbom.Format = (*OutputFormat)(nil)