cmd/govulncheck/internal/govulncheck: new package

Create a new internal package to hold some of govulncheck's code.
This will make it easier for gopls's implementation of vulnerability
checking to use the code. They will still have to copy it, but
the copying should be trivial.

This CL moves the vuln DB cache into the new package.
It is the first of several.

Change-Id: I56add35ee99b1e4cb7bdb882646a21661f798c37
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/406580
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/govulncheck/cache.go b/cmd/govulncheck/internal/govulncheck/cache.go
similarity index 82%
rename from cmd/govulncheck/cache.go
rename to cmd/govulncheck/internal/govulncheck/cache.go
index 26dd00f..0948837 100644
--- a/cmd/govulncheck/cache.go
+++ b/cmd/govulncheck/internal/govulncheck/cache.go
@@ -2,10 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build go1.18
-// +build go1.18
-
-package main
+// Package govulncheck supports the govulncheck command.
+package govulncheck
 
 import (
 	"encoding/json"
@@ -38,19 +36,22 @@
 // $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/{import path}/vulns.json
 //   []*osv.Entry
 
-// fsCache is a thread-safe file-system cache implementing osv.Cache
+// FSCache is a thread-safe file-system cache implementing osv.Cache
 //
 // TODO: use something like cmd/go/internal/lockedfile for thread safety?
-type fsCache struct {
+type FSCache struct {
 	mu      sync.Mutex
 	rootDir string
 }
 
+// Assert that *FSCache implements client.Cache.
+var _ client.Cache = (*FSCache)(nil)
+
 // use cfg.GOMODCACHE available in cmd/go/internal?
 var defaultCacheRoot = filepath.Join(build.Default.GOPATH, "/pkg/mod/cache/download/vulndb")
 
-func defaultCache() *fsCache {
-	return &fsCache{rootDir: defaultCacheRoot}
+func DefaultCache() *FSCache {
+	return &FSCache{rootDir: defaultCacheRoot}
 }
 
 type cachedIndex struct {
@@ -58,7 +59,7 @@
 	Index     client.DBIndex
 }
 
-func (c *fsCache) ReadIndex(dbName string) (client.DBIndex, time.Time, error) {
+func (c *FSCache) ReadIndex(dbName string) (client.DBIndex, time.Time, error) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
@@ -76,7 +77,7 @@
 	return index.Index, index.Retrieved, nil
 }
 
-func (c *fsCache) WriteIndex(dbName string, index client.DBIndex, retrieved time.Time) error {
+func (c *FSCache) WriteIndex(dbName string, index client.DBIndex, retrieved time.Time) error {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
@@ -97,7 +98,7 @@
 	return nil
 }
 
-func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
+func (c *FSCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
@@ -115,7 +116,7 @@
 	return entries, nil
 }
 
-func (c *fsCache) WriteEntries(dbName string, p string, entries []*osv.Entry) error {
+func (c *FSCache) WriteEntries(dbName string, p string, entries []*osv.Entry) error {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
diff --git a/cmd/govulncheck/cache_test.go b/cmd/govulncheck/internal/govulncheck/cache_test.go
similarity index 96%
rename from cmd/govulncheck/cache_test.go
rename to cmd/govulncheck/internal/govulncheck/cache_test.go
index 3e5609d..0917ae8 100644
--- a/cmd/govulncheck/cache_test.go
+++ b/cmd/govulncheck/internal/govulncheck/cache_test.go
@@ -2,10 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build go1.18
-// +build go1.18
-
-package main
+package govulncheck
 
 import (
 	"fmt"
@@ -23,7 +20,7 @@
 func TestCache(t *testing.T) {
 	tmpDir := t.TempDir()
 
-	cache := &fsCache{rootDir: tmpDir}
+	cache := &FSCache{rootDir: tmpDir}
 	dbName := "vulndb.golang.org"
 
 	_, _, err := cache.ReadIndex(dbName)
@@ -85,7 +82,7 @@
 func TestConcurrency(t *testing.T) {
 	tmpDir := t.TempDir()
 
-	cache := &fsCache{rootDir: tmpDir}
+	cache := &FSCache{rootDir: tmpDir}
 	dbName := "vulndb.golang.org"
 
 	g := new(errgroup.Group)
diff --git a/cmd/govulncheck/main.go b/cmd/govulncheck/main.go
index 02b7e79..66ec2d7 100644
--- a/cmd/govulncheck/main.go
+++ b/cmd/govulncheck/main.go
@@ -32,6 +32,7 @@
 	"golang.org/x/tools/go/buildutil"
 	"golang.org/x/tools/go/packages"
 	"golang.org/x/vuln/client"
+	"golang.org/x/vuln/cmd/govulncheck/internal/govulncheck"
 	"golang.org/x/vuln/osv"
 	"golang.org/x/vuln/vulncheck"
 )
@@ -86,7 +87,9 @@
 	if GOVULNDB := os.Getenv("GOVULNDB"); GOVULNDB != "" {
 		dbs = strings.Split(GOVULNDB, ",")
 	}
-	dbClient, err := client.NewClient(dbs, client.Options{HTTPCache: defaultCache()})
+	dbClient, err := client.NewClient(dbs, client.Options{
+		HTTPCache: govulncheck.DefaultCache(),
+	})
 	if err != nil {
 		die("govulncheck: %s", err)
 	}