From 2711979617c588e54f81b52e543e3c279bd97d32 Mon Sep 17 00:00:00 2001
From: Alessio Caiazza <acaiazza@gitlab.com>
Date: Wed, 26 Sep 2018 17:57:44 +0200
Subject: [PATCH] reduce cyclomatic complexity

---
 commands/helpers/cache_extractor.go | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/commands/helpers/cache_extractor.go b/commands/helpers/cache_extractor.go
index 0ad01d4f0..ad7fcf0f8 100644
--- a/commands/helpers/cache_extractor.go
+++ b/commands/helpers/cache_extractor.go
@@ -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 {
-- 
GitLab