Commit 27119796 authored by Alessio Caiazza's avatar Alessio Caiazza

reduce cyclomatic complexity

parent 0c1b0999
......@@ -35,16 +35,15 @@ func (c *CacheExtractorCommand) getClient() *CacheClient {
return c.client
}
func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) {
fi, _ := os.Lstat(path)
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
return fi != nil && !date.After(fi.ModTime()), date
}
func (c *CacheExtractorCommand) download() (bool, error) {
os.MkdirAll(filepath.Dir(c.File), 0700)
file, err := ioutil.TempFile(filepath.Dir(c.File), "cache")
if err != nil {
return false, err
}
defer file.Close()
defer os.Remove(file.Name())
resp, err := c.getClient().Get(c.URL)
if err != nil {
return true, err
......@@ -59,13 +58,19 @@ func (c *CacheExtractorCommand) download() (bool, error) {
return retry, fmt.Errorf("Received: %s", resp.Status)
}
fi, _ := os.Lstat(c.File)
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
if fi != nil && !date.After(fi.ModTime()) {
upToDate, date := checkIfUpToDate(c.File, resp)
if upToDate {
logrus.Infoln(filepath.Base(c.File), "is up to date")
return false, nil
}
file, err := ioutil.TempFile(filepath.Dir(c.File), "cache")
if err != nil {
return false, err
}
defer os.Remove(file.Name())
defer file.Close()
logrus.Infoln("Downloading", filepath.Base(c.File), "from", url_helpers.CleanURL(c.URL))
_, err = io.Copy(file, resp.Body)
if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment