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

     1  package options
     2  
     3  import (
     4  	"github.com/hashicorp/go-multierror"
     5  
     6  	"github.com/anchore/syft/syft/format/spdxjson"
     7  	"github.com/anchore/syft/syft/sbom"
     8  )
     9  
    10  type FormatSPDXJSON struct {
    11  	Pretty *bool `yaml:"pretty" json:"pretty" mapstructure:"pretty"`
    12  }
    13  
    14  func DefaultFormatSPDXJSON() FormatSPDXJSON {
    15  	return FormatSPDXJSON{}
    16  }
    17  
    18  func (o FormatSPDXJSON) formatEncoders() ([]sbom.FormatEncoder, error) {
    19  	var (
    20  		encs []sbom.FormatEncoder
    21  		errs error
    22  	)
    23  	for _, v := range spdxjson.SupportedVersions() {
    24  		enc, err := spdxjson.NewFormatEncoderWithConfig(o.buildConfig(v))
    25  		if err != nil {
    26  			errs = multierror.Append(errs, err)
    27  		} else {
    28  			encs = append(encs, enc)
    29  		}
    30  	}
    31  	return encs, errs
    32  }
    33  
    34  func (o FormatSPDXJSON) buildConfig(v string) spdxjson.EncoderConfig {
    35  	var pretty bool
    36  	if o.Pretty != nil {
    37  		pretty = *o.Pretty
    38  	}
    39  	return spdxjson.EncoderConfig{
    40  		Version: v,
    41  		Pretty:  pretty,
    42  	}
    43  }