github.com/anchore/syft@v1.38.2/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  		"1.6",
    23  	}
    24  
    25  	if id != JSONFormatID {
    26  		// JSON format not supported for version < 1.2
    27  		versions = append([]string{"1.0", "1.1"}, versions...)
    28  	}
    29  
    30  	return versions
    31  }
    32  
    33  func SpecVersionFromString(v string) (cyclonedx.SpecVersion, error) {
    34  	switch v {
    35  	case "1.0":
    36  		return cyclonedx.SpecVersion1_0, nil
    37  	case "1.1":
    38  		return cyclonedx.SpecVersion1_1, nil
    39  	case "1.2":
    40  		return cyclonedx.SpecVersion1_2, nil
    41  	case "1.3":
    42  		return cyclonedx.SpecVersion1_3, nil
    43  	case "1.4":
    44  		return cyclonedx.SpecVersion1_4, nil
    45  	case "1.5":
    46  		return cyclonedx.SpecVersion1_5, nil
    47  	case "1.6":
    48  		return cyclonedx.SpecVersion1_6, nil
    49  	}
    50  	return -1, fmt.Errorf("unsupported CycloneDX version %q", v)
    51  }
    52  
    53  func VersionFromSpecVersion(spec cyclonedx.SpecVersion) string {
    54  	switch spec {
    55  	case cyclonedx.SpecVersion1_0:
    56  		return "1.0"
    57  	case cyclonedx.SpecVersion1_1:
    58  		return "1.1"
    59  	case cyclonedx.SpecVersion1_2:
    60  		return "1.2"
    61  	case cyclonedx.SpecVersion1_3:
    62  		return "1.3"
    63  	case cyclonedx.SpecVersion1_4:
    64  		return "1.4"
    65  	case cyclonedx.SpecVersion1_5:
    66  		return "1.5"
    67  	case cyclonedx.SpecVersion1_6:
    68  		return "1.6"
    69  	}
    70  	return ""
    71  }