-
Notifications
You must be signed in to change notification settings - Fork 593
/
cyclonedxBom.go
58 lines (50 loc) · 1.2 KB
/
cyclonedxBom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package piperutils
import (
"encoding/xml"
"io"
"os"
"github.com/SAP/jenkins-library/pkg/log"
)
// To serialize the cyclonedx BOM file
type Bom struct {
Metadata Metadata `xml:"metadata"`
}
type Metadata struct {
Component BomComponent `xml:"component"`
Properties []BomProperty `xml:"properties>property"`
}
type BomProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type BomComponent struct {
Purl string `xml:"purl"`
}
func GetBom(absoluteBomPath string) (Bom, error) {
xmlFile, err := os.Open(absoluteBomPath)
if err != nil {
log.Entry().Debugf("failed to open bom file %s", absoluteBomPath)
return Bom{}, err
}
defer xmlFile.Close()
byteValue, err := io.ReadAll(xmlFile)
if err != nil {
log.Entry().Debugf("failed to read bom file %s", absoluteBomPath)
return Bom{}, err
}
var bom Bom
err = xml.Unmarshal(byteValue, &bom)
if err != nil {
log.Entry().Debugf("failed to unmarshal bom file %s", absoluteBomPath)
return Bom{}, err
}
return bom, nil
}
func GetPurl(bomFilePath string) string {
bom, err := GetBom(bomFilePath)
if err != nil {
log.Entry().Warnf("unable to get bom metadata: %v", err)
return ""
}
return bom.Metadata.Component.Purl
}