Debian Codegraph @

sarif/sarif.go

raw symbols SCIP
1 package sarif
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "os"
8 )
9
10 // Version is the version of Sarif to use
11 type Version string
12
13 // Version210 represents Version210 of Sarif
14 const Version210 Version = "2.1.0"
15
16 var versions = map[Version]string{
17 Version210: "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
18 }
19
20 // Report is the encapsulating type representing a Sarif Report
21 type Report struct {
22 PropertyBag
23 Version string `json:"version"`
24 Schema string `json:"$schema"`
25 Runs []*Run `json:"runs"`
26 }
27
28 // New Creates a new Report or returns an error
29 func New(version Version) (*Report, error) {
30 schema, err := getVersionSchema(version)
31 if err != nil {
32 return nil, err
33 }
34 return &Report{
35 Version: string(version),
36 Schema: schema,
37 Runs: []*Run{},
38 }, nil
39 }
40
41 // Open loads a Report from a file
42 func Open(filename string) (*Report, error) {
43 if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
44 return nil, fmt.Errorf("the provided file path doesn't have a file")
45 }
46
47 content, err := os.ReadFile(filename)
48 if err != nil {
49 return nil, fmt.Errorf("the provided filepath could not be opened. %w", err)
50 }
51 return FromBytes(content)
52 }
53
54 // FromString loads a Report from string content
55 func FromString(content string) (*Report, error) {
56 return FromBytes([]byte(content))
57 }
58
59 // FromBytes loads a Report from a byte array
60 func FromBytes(content []byte) (*Report, error) {
61 var report Report
62 if err := json.Unmarshal(content, &report); err != nil {
63 return nil, err
64 }
65 return &report, nil
66 }
67
68 // AddRun allows adding run information to the current report
69 func (sarif *Report) AddRun(run *Run) {
70 sarif.Runs = append(sarif.Runs, run)
71 }
72
73 func getVersionSchema(version Version) (string, error) {
74 for ver, schema := range versions {
75 if ver == version {
76 return schema, nil
77 }
78 }
79 return "", fmt.Errorf("version [%s] is not supported", version)
80 }
81
82 // WriteFile will write the report to a file using a pretty formatter
83 func (sarif *Report) WriteFile(filename string) error {
84 file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModeAppend)
85 if err != nil {
86 return err
87 }
88 defer func() { _ = file.Close() }()
89 return sarif.PrettyWrite(file)
90 }
91
92 // Write writes the JSON as a string with no formatting
93 func (sarif *Report) Write(w io.Writer) error {
94 for _, run := range sarif.Runs {
95 run.DedupeArtifacts()
96 }
97 marshal, err := json.Marshal(sarif)
98 if err != nil {
99 return err
100 }
101 _, err = w.Write(marshal)
102 return err
103 }
104
105 // PrettyWrite writes the JSON output with indentation
106 func (sarif *Report) PrettyWrite(w io.Writer) error {
107 marshal, err := json.MarshalIndent(sarif, "", " ")
108 if err != nil {
109 return err
110 }
111 _, err = w.Write(marshal)
112 return err
113 }