internal: define ID directory
Define the ID directory in one place, in the "internal" package.
Also, actually change it to "ID".
Change-Id: I5a871b380d6f36f4a89ce0560dba7c3754090575
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/357033
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/client/client.go b/client/client.go
index fabac91..6666d71 100644
--- a/client/client.go
+++ b/client/client.go
@@ -43,6 +43,7 @@
"strings"
"time"
+ "golang.org/x/vulndb/internal"
"golang.org/x/vulndb/osv"
)
@@ -57,8 +58,6 @@
GetByID(string) (*osv.Entry, error)
}
-const idDir = "byID"
-
type source interface {
Client
Index() (osv.DBIndex, error)
@@ -83,7 +82,7 @@
}
func (ls *localSource) GetByID(id string) (*osv.Entry, error) {
- content, err := ioutil.ReadFile(filepath.Join(ls.dir, idDir, id+".json"))
+ content, err := ioutil.ReadFile(filepath.Join(ls.dir, internal.IDDirectory, id+".json"))
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
@@ -226,7 +225,7 @@
func (hs *httpSource) GetByID(id string) (*osv.Entry, error) {
// TODO(jba): cache?
- content, err := hs.readBody(fmt.Sprintf("%s/%s/%s.json", hs.url, idDir, id))
+ content, err := hs.readBody(fmt.Sprintf("%s/%s/%s.json", hs.url, internal.IDDirectory, id))
if err != nil || content == nil {
return nil, err
}
diff --git a/client/client_test.go b/client/client_test.go
index 924d73a..d09e3ec 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -20,6 +20,7 @@
"time"
"github.com/google/go-cmp/cmp"
+ "golang.org/x/vulndb/internal"
"golang.org/x/vulndb/osv"
)
@@ -129,7 +130,7 @@
if err := createDirAndFile(path.Join(dbName, ""), "index.json", index); err != nil {
return "", err
}
- if err := createDirAndFile(path.Join(dbName, idDir), "ID.json", testVuln); err != nil {
+ if err := createDirAndFile(path.Join(dbName, internal.IDDirectory), "ID.json", testVuln); err != nil {
return "", err
}
return dbName, nil
@@ -267,7 +268,7 @@
t.Skip("skipping test: no network on js")
}
- http.HandleFunc("/byID/ID.json", dataHandler(testVuln))
+ http.HandleFunc(fmt.Sprintf("/%s/ID.json", internal.IDDirectory), dataHandler(testVuln))
l, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {
t.Fatalf("failed to listen on 127.0.0.1: %s", err)
diff --git a/cmd/dbdiff/main.go b/cmd/dbdiff/main.go
index db3d868..46c6b86 100644
--- a/cmd/dbdiff/main.go
+++ b/cmd/dbdiff/main.go
@@ -13,6 +13,7 @@
"strings"
"github.com/google/go-cmp/cmp"
+ "golang.org/x/vulndb/internal"
"golang.org/x/vulndb/osv"
)
@@ -42,7 +43,7 @@
if err := json.Unmarshal(content, &index); err != nil {
return fmt.Errorf("unable to parse %q: %s", fpath, err)
}
- } else if path == filepath.Join(dbPath, "ID") {
+ } else if path == filepath.Join(dbPath, internal.IDDirectory) {
var entry osv.Entry
if err := json.Unmarshal(content, &entry); err != nil {
return fmt.Errorf("unable to parse %q: %s", fpath, err)
diff --git a/internal/internal.go b/internal/internal.go
index d8055b5..fd10ebd 100644
--- a/internal/internal.go
+++ b/internal/internal.go
@@ -11,6 +11,10 @@
"strings"
)
+// IDDirectory is the name of the directory that contains entries
+// listed by their IDs.
+const IDDirectory = "ID"
+
// Readfilelines reads and returns the lines from a file.
// Whitespace on each line is trimmed.
// Blank lines and lines beginning with '#' are ignored.