vlint: add LintReport
A LintReport function is added for linting reports in x/vulndb.
Change-Id: I31df926efe947cb1719542293bb95c17ac3e108d
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/360534
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
diff --git a/vlint/vlint.go b/vlint/vlint.go
new file mode 100644
index 0000000..03ee6c0
--- /dev/null
+++ b/vlint/vlint.go
@@ -0,0 +1,31 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package vlint contains functionality for linting reports in x/vulndb.
+package vlint
+
+import (
+ "fmt"
+ "io/ioutil"
+
+ "golang.org/x/vuln/internal/derrors"
+ "golang.org/x/vuln/internal/report"
+ "gopkg.in/yaml.v2"
+)
+
+// LintReport is used to lint the x/vulndb/reports/ directory. It is run by
+// TestLintReports and cmd/gendb to ensure that there are no errors in the YAML
+// reports.
+func LintReport(filename string) (_ []string, err error) {
+ defer derrors.Wrap(&err, "Lint(%q)", filename)
+ b, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return nil, fmt.Errorf("ioutil.ReadDir(%q): %v", filename, err)
+ }
+ var r report.Report
+ if err := yaml.UnmarshalStrict(b, &r); err != nil {
+ return nil, fmt.Errorf("yaml.UnmarshalStrict(b, &r): %v (%q)", err, filename)
+ }
+ return r.Lint(), nil
+}