Move downloadFeed out to a seperate function

This commit is contained in:
Jean Michel MacKay 2018-09-10 14:42:22 -04:00
parent f34f94320a
commit 56b4f23ae2

View File

@ -165,7 +165,18 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
}
}
downloadAndSave := func() error {
err := downloadFeed(dataFeedName, fileName)
if err != nil {
return dataFeedReaders, dataFeedHashes, err
}
dataFeedReaders[dataFeedName] = fileName
}
}
return dataFeedReaders, dataFeedHashes, nil
}
func downloadFeed(dataFeedName, fileName string) error {
// Download data feed.
r, err := httputil.GetWithUserAgent(fmt.Sprintf(dataFeedURL, dataFeedName))
@ -176,40 +187,33 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
defer r.Body.Close()
if !httputil.Status2xx(r) {
log.WithField("StatusCode", r.StatusCode).Error("Failed to download NVD data feed")
log.WithFields(log.Fields{"StatusCode": r.StatusCode, "DataFeedName": dataFeedName}).Error("Failed to download NVD data feed")
return commonerr.ErrCouldNotDownload
}
// Un-gzip it.
gr, err := gzip.NewReader(r.Body)
if err != nil {
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not read NVD data feed")
log.WithError(err).WithFields(log.Fields{"StatusCode": r.StatusCode, "DataFeedName": dataFeedName}).Error("could not read NVD data feed")
return commonerr.ErrCouldNotDownload
}
// Store it to a file at the same time if possible.
if f, err := os.Create(fileName); err == nil {
_, err = io.Copy(f, gr)
f, err := os.Create(fileName)
if err != nil {
log.WithError(err).Warning("could not stream NVD data feed to filesystem")
log.WithError(err).WithField("Filename", fileName).Warning("could not store NVD data feed to filesystem")
return commonerr.ErrFilesystem
}
defer f.Close()
dataFeedReaders[dataFeedName] = fileName
} else {
log.WithError(err).Warning("could not store NVD data feed to filesystem")
}
return nil
}
err := downloadAndSave()
_, err = io.Copy(f, gr)
if err != nil {
return dataFeedReaders, dataFeedHashes, err
}
}
log.WithError(err).WithField("Filename", fileName).Warning("could not stream NVD data feed to filesystem")
return commonerr.ErrFilesystem
}
return dataFeedReaders, dataFeedHashes, nil
return nil
}
func getHashFromMetaURL(metaURL string) (string, error) {