From 8e6e644c136c267c9155e3615267458084c9b8ac Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 9 Apr 2022 16:04:45 +0200 Subject: [PATCH] Fix cache, remove double load call --- CHANGELOG.md | 2 +- main.go | 7 +------ security/advisories.go | 14 +++++++------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e0a419..90158a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.3.0 (2022-04-09) +# 2.0.0 (2022-04-09) * Add --cache-dir * Add --disable-exit-code diff --git a/main.go b/main.go index a23b28e..9c6751c 100644 --- a/main.go +++ b/main.go @@ -39,18 +39,13 @@ func main() { os.Exit(0) } - db, err := security.NewDB(*local, *advisoryArchiveURL) - db.CacheDir = *cacheDir + db, err := security.NewDB(*local, *advisoryArchiveURL, *cacheDir) if err != nil { fmt.Fprintf(os.Stderr, "unable to load the advisory DB: %s\n", err) os.Exit(127) } if *updateCacheOnly { - if err := db.Load(*advisoryArchiveURL); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(127) - } return } diff --git a/security/advisories.go b/security/advisories.go index affcb62..ab1d13b 100644 --- a/security/advisories.go +++ b/security/advisories.go @@ -20,7 +20,7 @@ const AdvisoryArchiveURL = "https://codeload.github.com/FriendsOfPHP/security-ad // AdvisoryDB stores all known security advisories type AdvisoryDB struct { Advisories []Advisory - CacheDir string + cacheDir string noHTTPCalls bool } @@ -47,19 +47,19 @@ type Cache struct { } // NewDB fetches the advisory DB from Github -func NewDB(noHTTPCalls bool, advisoryArchiveURL string) (*AdvisoryDB, error) { - db := &AdvisoryDB{noHTTPCalls: noHTTPCalls} - if err := db.Load(advisoryArchiveURL); err != nil { +func NewDB(noHTTPCalls bool, advisoryArchiveURL, cacheDir string) (*AdvisoryDB, error) { + db := &AdvisoryDB{noHTTPCalls: noHTTPCalls, cacheDir: cacheDir} + if err := db.load(advisoryArchiveURL); err != nil { return nil, fmt.Errorf("unable to fetch advisories: %s", err) } return db, nil } -// Load Loads fetches the database from Github and reads/loads current advisories +// load loads fetches the database from Github and reads/loads current advisories // from the repository. Cache handling is delegated to http.Transport and // **must** be handled appropriately. -func (db *AdvisoryDB) Load(advisoryArchiveURL string) error { +func (db *AdvisoryDB) load(advisoryArchiveURL string) error { if len(db.Advisories) > 0 { return nil } @@ -67,7 +67,7 @@ func (db *AdvisoryDB) Load(advisoryArchiveURL string) error { db.Advisories = []Advisory{} var cache *Cache - cachePath := filepath.Join(db.CacheDir, "php_sec_db.json") + cachePath := filepath.Join(db.cacheDir, "php_sec_db.json") if cacheContent, err := ioutil.ReadFile(cachePath); err == nil { // ignore errors json.Unmarshal(cacheContent, &cache)