Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from fabpot/2.0
Browse files Browse the repository at this point in the history
Fix cache, remove double load call
  • Loading branch information
fabpot authored Apr 9, 2022
2 parents 70eb7c0 + 8e6e644 commit 285cb05
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 1.3.0 (2022-04-09)
# 2.0.0 (2022-04-09)

* Add --cache-dir
* Add --disable-exit-code
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 7 additions & 7 deletions security/advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -47,27 +47,27 @@ 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
}

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)
Expand Down

0 comments on commit 285cb05

Please sign in to comment.