-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
94 lines (78 loc) · 1.92 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"os"
v1alpha1 "github.com/project-copacetic/copacetic/pkg/types/v1alpha1"
)
type FakeParser struct{}
// parseFakeReport parses a fake report from a file
func parseFakeReport(file string) (*FakeReport, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
var fake FakeReport
if err = json.Unmarshal(data, &fake); err != nil {
return nil, err
}
return &fake, nil
}
func newFakeParser() *FakeParser {
return &FakeParser{}
}
func (k *FakeParser) parse(file string) (*v1alpha1.UpdateManifest, error) {
// Parse the fake report
report, err := parseFakeReport(file)
if err != nil {
return nil, err
}
// Create the standardized report
updates := v1alpha1.UpdateManifest{
APIVersion: v1alpha1.APIVersion,
Metadata: v1alpha1.Metadata{
OS: v1alpha1.OS{
Type: report.OSType,
Version: report.OSVersion,
},
Config: v1alpha1.Config{
Arch: report.Arch,
},
},
}
// Convert the fake report to the standardized report
for i := range report.Packages {
pkgs := &report.Packages[i]
if pkgs.FixedVersion != "" {
updates.Updates = append(updates.Updates, v1alpha1.UpdatePackage{
Name: pkgs.Name,
InstalledVersion: pkgs.InstalledVersion,
FixedVersion: pkgs.FixedVersion,
VulnerabilityID: pkgs.VulnerabilityID,
})
}
}
return &updates, nil
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <image report>\n", os.Args[0])
os.Exit(1)
}
// Initialize the parser
fakeParser := newFakeParser()
// Get the image report from command line
imageReport := os.Args[1]
report, err := fakeParser.parse(imageReport)
if err != nil {
fmt.Printf("error parsing report: %v\n", err)
os.Exit(1)
}
// Serialize the standardized report and print it to stdout
reportBytes, err := json.Marshal(report)
if err != nil {
fmt.Printf("Error serializing report: %v\n", err)
os.Exit(1)
}
os.Stdout.Write(reportBytes)
}