internal/govulncheck: add structs to send more info from sandbox
Adds a new GovulncheckResponse struct, which contains a govulncheck
result as well as memory and time stats.
Change-Id: I3cfd38e26e9a0f9a2ee12ad50f2cb64317250ce7
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/474938
Run-TryBot: Maceo Thompson <maceothompson@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
diff --git a/internal/govulncheck/govulncheck.go b/internal/govulncheck/govulncheck.go
new file mode 100644
index 0000000..0c5297f
--- /dev/null
+++ b/internal/govulncheck/govulncheck.go
@@ -0,0 +1,60 @@
+// Copyright 2022 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 govulncheck
+
+import (
+ "encoding/json"
+ "errors"
+
+ "golang.org/x/vuln/exp/govulncheck"
+)
+
+// ScanStats represent monitoring information about a given
+// run of govulncheck or vulncheck
+type ScanStats struct {
+ // The amount of time a scan took to run, in seconds
+ ScanSeconds float64
+ // The peak (heap) memory used by govulncheck, in kb
+ ScanMemory uint64
+}
+
+// GovulncheckResponse passes both the raw govulncheck result as well as
+// statistics about memory usage and run time
+type GovulncheckResponse struct {
+ Res govulncheck.Result
+ Stats ScanStats
+}
+
+func UnmarshalGovulncheckSandboxResponse(output []byte) (*GovulncheckResponse, error) {
+ var e struct{ Error string }
+ if err := json.Unmarshal(output, &e); err != nil {
+ return nil, err
+ }
+ if e.Error != "" {
+ return nil, errors.New(e.Error)
+ }
+ var res GovulncheckResponse
+ if err := json.Unmarshal(output, &res); err != nil {
+ return nil, err
+ }
+ return &res, nil
+}
+
+func UnmarshalGovulncheckResult(output []byte) (*govulncheck.Result, error) {
+ var e struct {
+ Error string
+ }
+ if err := json.Unmarshal(output, &e); err != nil {
+ return nil, err
+ }
+ if e.Error != "" {
+ return nil, errors.New(e.Error)
+ }
+ var res govulncheck.Result
+ if err := json.Unmarshal(output, &res); err != nil {
+ return nil, err
+ }
+ return &res, nil
+}