|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | +) |
| 6 | + |
| 7 | +type BomSWDocument struct { |
| 8 | + Basic swBasicInfo `json:"documentBasicInfo"` |
| 9 | + Software swSoftwareCompositionInfo `json:"softwareCompositionInfo"` |
| 10 | +} |
| 11 | + |
| 12 | +type swBasicInfo struct { |
| 13 | + // 文档名称 |
| 14 | + DocumentName string `json:"documentName"` |
| 15 | + // 文档版本 |
| 16 | + DocumentVersion string `json:"documentVersion"` |
| 17 | + // 文档创建/更新时间 yyyy-MM-ddTHH:mm:ssTZD |
| 18 | + DocumentTime string `json:"timestamp"` |
| 19 | + // 文档格式 |
| 20 | + SbomFormat string `json:"sbomFormat"` |
| 21 | + // 生成工具 |
| 22 | + ToolInfo string `json:"toolInfo"` |
| 23 | + // bom作者 |
| 24 | + SbomAuthor string `json:"sbomAuthor"` |
| 25 | + // 文档作者注释 |
| 26 | + SbomAuthorComments string `json:"sbomAuthorComments"` |
| 27 | + // 文档注释 |
| 28 | + SbomComments string `json:"sbomComments"` |
| 29 | + // 文档类型 |
| 30 | + SbomType string `json:"sbomType"` |
| 31 | +} |
| 32 | + |
| 33 | +type swSoftwareCompositionInfo struct { |
| 34 | + // 组件列表 |
| 35 | + Components []BomSWComponent `json:"components"` |
| 36 | + // 依赖关系 |
| 37 | + Dependencies []swDependencies `json:"dependencies"` |
| 38 | +} |
| 39 | + |
| 40 | +type BomSWComponent struct { |
| 41 | + Author map[string]string `json:"componentAuthor"` |
| 42 | + Provider map[string]string `json:"componentProvider"` |
| 43 | + Name string `json:"componentName"` |
| 44 | + Version string `json:"componentVersion"` |
| 45 | + // map[hash算法]hash值 |
| 46 | + HashValue []swChecksumValue `json:"componentHashValue"` |
| 47 | + ID string `json:"componentId"` |
| 48 | + License []string `json:"license"` |
| 49 | + // 组件信息更新时间 yyyy-MM-ddTHH:mm:ssTZD |
| 50 | + Timestamp string `json:"componentTimestamp"` |
| 51 | +} |
| 52 | + |
| 53 | +type swChecksumValue struct { |
| 54 | + Algorithm string `json:"algorithm"` |
| 55 | + Value string `json:"hashValue"` |
| 56 | +} |
| 57 | + |
| 58 | +type swDependencies struct { |
| 59 | + Ref string `json:"ref"` |
| 60 | + DependsOn []struct { |
| 61 | + Ref string `json:"ref"` |
| 62 | + } `json:"dependsOn"` |
| 63 | +} |
| 64 | + |
| 65 | +func newDependencies(ref string, dependsOn []string) swDependencies { |
| 66 | + deps := swDependencies{Ref: ref} |
| 67 | + deps.DependsOn = make([]struct { |
| 68 | + Ref string `json:"ref"` |
| 69 | + }, len(dependsOn)) |
| 70 | + for i, d := range dependsOn { |
| 71 | + deps.DependsOn[i].Ref = d |
| 72 | + } |
| 73 | + return deps |
| 74 | +} |
| 75 | + |
| 76 | +func NewBomSWDocument(name, creator string) *BomSWDocument { |
| 77 | + version := "1.0.0" |
| 78 | + timestamp := time.Now().Format("2006-01-02T15:04:05MST") |
| 79 | + return &BomSWDocument{ |
| 80 | + Basic: swBasicInfo{ |
| 81 | + DocumentName: name, |
| 82 | + DocumentVersion: version, |
| 83 | + DocumentTime: timestamp, |
| 84 | + SbomFormat: "BOM-SW 1.0", |
| 85 | + ToolInfo: creator, |
| 86 | + SbomAuthor: "", |
| 87 | + SbomAuthorComments: "", |
| 88 | + SbomComments: "", |
| 89 | + SbomType: "analyzed", |
| 90 | + }, |
| 91 | + Software: swSoftwareCompositionInfo{ |
| 92 | + Dependencies: []swDependencies{}, |
| 93 | + }, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (doc *BomSWDocument) AppendComponents(fn func(*BomSWComponent)) { |
| 98 | + c := BomSWComponent{ |
| 99 | + Author: map[string]string{ |
| 100 | + "name": "NONE", |
| 101 | + }, |
| 102 | + Provider: map[string]string{ |
| 103 | + "shortName": "NONE", |
| 104 | + "fullName": "NONE", |
| 105 | + }, |
| 106 | + HashValue: []swChecksumValue{}, |
| 107 | + License: []string{}, |
| 108 | + } |
| 109 | + if fn != nil { |
| 110 | + fn(&c) |
| 111 | + } |
| 112 | + if c.Timestamp == "" { |
| 113 | + c.Timestamp = time.Now().Format("2006-01-02T15:04:05MST") |
| 114 | + } |
| 115 | + doc.Software.Components = append(doc.Software.Components, c) |
| 116 | +} |
| 117 | + |
| 118 | +func (doc *BomSWDocument) AppendDependencies(parentId string, childrenIds []string) { |
| 119 | + if doc.Software.Dependencies == nil { |
| 120 | + doc.Software.Dependencies = []swDependencies{} |
| 121 | + } |
| 122 | + if len(childrenIds) > 0 { |
| 123 | + doc.Software.Dependencies = append(doc.Software.Dependencies, newDependencies(parentId, childrenIds)) |
| 124 | + } |
| 125 | +} |
0 commit comments