| 1 | package sarifscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.16 io/" |
| 7 | "osscip-go gomod github.com/golang/go/src go1.16 os/" |
| 8 | ) |
| 9 | |
| 10 | // Version is the version of Sarif to use |
| 11 | type Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version# string |
| 12 | |
| 13 | // Version210 represents Version210 of Sarif |
| 14 | const Version210scip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version210. Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version# = "2.1.0" |
| 15 | |
| 16 | var versionsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/versions. = map[Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version#]string{ |
| 17 | Version210scip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/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 Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report# struct { |
| 22 | PropertyBagscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#PropertyBag. |
| 23 | Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Version. string `json:"version"` |
| 24 | Schemascip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Schema. string `json:"$schema"` |
| 25 | Runsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Runs. []*Runscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Run# `json:"runs"` |
| 26 | } |
| 27 | |
| 28 | // New Creates a new Report or returns an error |
| 29 | func Newscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/New().(version Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version#) (*Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#, error) { |
| 30 | schema, err := getVersionSchemascip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/getVersionSchema().(version) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | return &Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#{ |
| 35 | Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Version.: string(version), |
| 36 | Schemascip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Schema.: schema, |
| 37 | Runsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Runs.: []*Runscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Run#{}, |
| 38 | }, nil |
| 39 | } |
| 40 | |
| 41 | // Open loads a Report from a file |
| 42 | func Openscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Open().(filename string) (*Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#, error) { |
| 43 | if _, err := osscip-go gomod github.com/golang/go/src go1.16 os/.Statscip-go gomod github.com/golang/go/src go1.16 os/Stat().(filename); err != nil && osscip-go gomod github.com/golang/go/src go1.16 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.16 os/IsNotExist().(err) { |
| 44 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("the provided file path doesn't have a file") |
| 45 | } |
| 46 | |
| 47 | content, err := osscip-go gomod github.com/golang/go/src go1.16 os/.ReadFilescip-go gomod github.com/golang/go/src go1.16 os/ReadFile().(filename) |
| 48 | if err != nil { |
| 49 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("the provided filepath could not be opened. %w", err) |
| 50 | } |
| 51 | return FromBytesscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/FromBytes().(content) |
| 52 | } |
| 53 | |
| 54 | // FromString loads a Report from string content |
| 55 | func FromStringscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/FromString().(content string) (*Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#, error) { |
| 56 | return FromBytesscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/FromBytes().([]byte(content)) |
| 57 | } |
| 58 | |
| 59 | // FromBytes loads a Report from a byte array |
| 60 | func FromBytesscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/FromBytes().(content []byte) (*Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#, error) { |
| 61 | var report Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report# |
| 62 | if err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.16 `encoding/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 *Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#) AddRunscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#AddRun().(run *Runscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Run#) { |
| 70 | sarif.Runsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Runs. = append(sarif.Runsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Runs., run) |
| 71 | } |
| 72 | |
| 73 | func getVersionSchemascip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/getVersionSchema().(version Versionscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Version#) (string, error) { |
| 74 | for ver, schema := range versionsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/versions. { |
| 75 | if ver == version { |
| 76 | return schema, nil |
| 77 | } |
| 78 | } |
| 79 | return "", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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 *Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#) WriteFilescip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#WriteFile().(filename string) error { |
| 84 | file, err := osscip-go gomod github.com/golang/go/src go1.16 os/.OpenFilescip-go gomod github.com/golang/go/src go1.16 os/OpenFile().(filename, osscip-go gomod github.com/golang/go/src go1.16 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.16 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.16 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.16 os/O_WRONLY., osscip-go gomod github.com/golang/go/src go1.16 os/.ModeAppendscip-go gomod github.com/golang/go/src go1.16 os/ModeAppend.) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | defer func() { _ = file.Closescip-go gomod github.com/golang/go/src go1.16 os/File#Close().() }() |
| 89 | return sarif.PrettyWritescip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#PrettyWrite().(file) |
| 90 | } |
| 91 | |
| 92 | // Write writes the JSON as a string with no formatting |
| 93 | func (sarif *Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#) Writescip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Write().(w ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) error { |
| 94 | for _, run := range sarif.Runsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#Runs. { |
| 95 | run.DedupeArtifactsscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Run#DedupeArtifacts().() |
| 96 | } |
| 97 | marshal, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(sarif) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | _, err = w.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(marshal) |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | // PrettyWrite writes the JSON output with indentation |
| 106 | func (sarif *Reportscip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#) PrettyWritescip-go gomod github.com/owenrumney/go-sarif . `github.com/owenrumney/go-sarif/sarif`/Report#PrettyWrite().(w ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) error { |
| 107 | marshal, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/MarshalIndent().(sarif, "", " ") |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | _, err = w.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(marshal) |
| 112 | return err |
| 113 | } |