github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/internal/cyclonedxutil/versions.go (about)

     1  package cyclonedxutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/CycloneDX/cyclonedx-go"
     7  
     8  	"github.com/anchore/syft/syft/sbom"
     9  )
    10  
    11  const (
    12  	XMLFormatID  sbom.FormatID = "cyclonedx-xml"
    13  	JSONFormatID sbom.FormatID = "cyclonedx-json"
    14  )
    15  
    16  func SupportedVersions(id sbom.FormatID) []string {
    17  	versions := []string{
    18  		"1.2",
    19  		"1.3",
    20  		"1.4",
    21  		"1.5",
    22  	}
    23  
    24  	if id != JSONFormatID {
    25  		// JSON format not supported for version < 1.2
    26  		versions = append([]string{"1.0", "1.1"}, versions...)
    27  	}
    28  
    29  	return versions
    30  }
    31  
    32  func SpecVersionFromString(v string) (cyclonedx.SpecVersion, error) {
    33  	switch v {
    34  	case "1.0":
    35  		return cyclonedx.SpecVersion1_0, nil
    36  	case "1.1":
    37  		return cyclonedx.SpecVersion1_1, nil
    38  	case "1.2":
    39  		return cyclonedx.SpecVersion1_2, nil
    40  	case "1.3":
    41  		return cyclonedx.SpecVersion1_3, nil
    42  	case "1.4":
    43  		return cyclonedx.SpecVersion1_4, nil
    44  	case "1.5":
    45  		return cyclonedx.SpecVersion1_5, nil
    46  	}
    47  	return -1, fmt.Errorf("unsupported CycloneDX version %q", v)
    48  }
    49  
    50  func VersionFromSpecVersion(spec cyclonedx.SpecVersion) string {
    51  	switch spec {
    52  	case cyclonedx.SpecVersion1_0:
    53  		return "1.0"
    54  	case cyclonedx.SpecVersion1_1:
    55  		return "1.1"
    56  	case cyclonedx.SpecVersion1_2:
    57  		return "1.2"
    58  	case cyclonedx.SpecVersion1_3:
    59  		return "1.3"
    60  	case cyclonedx.SpecVersion1_4:
    61  		return "1.4"
    62  	case cyclonedx.SpecVersion1_5:
    63  		return "1.5"
    64  	}
    65  	return ""
    66  }