jsonhandler.go (1337B)
1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package govulncheck 6 7 import ( 8 "encoding/json" 9 10 "io" 11 12 "golang.org/x/vuln/internal/osv" 13 ) 14 15 type jsonHandler struct { 16 enc *json.Encoder 17 } 18 19 // NewJSONHandler returns a handler that writes govulncheck output as json. 20 func NewJSONHandler(w io.Writer) Handler { 21 enc := json.NewEncoder(w) 22 enc.SetIndent("", " ") 23 return &jsonHandler{enc: enc} 24 } 25 26 // Config writes config block in JSON to the underlying writer. 27 func (h *jsonHandler) Config(config *Config) error { 28 return h.enc.Encode(Message{Config: config}) 29 } 30 31 // Progress writes a progress message in JSON to the underlying writer. 32 func (h *jsonHandler) Progress(progress *Progress) error { 33 return h.enc.Encode(Message{Progress: progress}) 34 } 35 36 // SBOM writes the SBOM block in JSON to the underlying writer. 37 func (h *jsonHandler) SBOM(sbom *SBOM) error { 38 return h.enc.Encode(Message{SBOM: sbom}) 39 } 40 41 // OSV writes an osv entry in JSON to the underlying writer. 42 func (h *jsonHandler) OSV(entry *osv.Entry) error { 43 return h.enc.Encode(Message{OSV: entry}) 44 } 45 46 // Finding writes a finding in JSON to the underlying writer. 47 func (h *jsonHandler) Finding(finding *Finding) error { 48 return h.enc.Encode(Message{Finding: finding}) 49 }