shithub: hugo

Download patch

ref: 5f095c27eabab75c1c948e4aa62ff4737a5607bd
parent: f604076de17e5c06ba25a07ee8c8a55d1bb6b936
author: Cameron Moore <moorereason@gmail.com>
date: Mon May 1 10:04:21 EDT 2017

tpl/data: Shorten some internal func names

--- a/tpl/data/resources.go
+++ b/tpl/data/resources.go
@@ -68,20 +68,21 @@
 	}
 }
 
-// getCacheFileID returns the cache ID for a string
+// getCacheFileID returns the cache ID for a string.
 func getCacheFileID(cfg config.Provider, id string) string {
 	return cfg.GetString("cacheDir") + url.QueryEscape(id)
 }
 
-// resGetCache returns the content for an ID from the file cache or an error
-// if the file is not found returns nil,nil
-func resGetCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool) ([]byte, error) {
-	resCacheMu.RLock()
-	defer resCacheMu.RUnlock()
-
+// getCache returns the content for an ID from the file cache or an error.
+// If the ID is not found, return nil,nil.
+func getCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool) ([]byte, error) {
 	if ignoreCache {
 		return nil, nil
 	}
+
+	resCacheMu.RLock()
+	defer resCacheMu.RUnlock()
+
 	fID := getCacheFileID(cfg, id)
 	isExists, err := helpers.Exists(fID, fs)
 	if err != nil {
@@ -92,17 +93,17 @@
 	}
 
 	return afero.ReadFile(fs, fID)
-
 }
 
-// resWriteCache writes bytes to an ID into the file cache
-func resWriteCache(id string, c []byte, fs afero.Fs, cfg config.Provider, ignoreCache bool) error {
-	resCacheMu.Lock()
-	defer resCacheMu.Unlock()
-
+// writeCache writes bytes associated with an ID into the file cache.
+func writeCache(id string, c []byte, fs afero.Fs, cfg config.Provider, ignoreCache bool) error {
 	if ignoreCache {
 		return nil
 	}
+
+	resCacheMu.Lock()
+	defer resCacheMu.Unlock()
+
 	fID := getCacheFileID(cfg, id)
 	f, err := fs.Create(fID)
 	if err != nil {
@@ -109,23 +110,24 @@
 		return errors.New("Error: " + err.Error() + ". Failed to create file: " + fID)
 	}
 	defer f.Close()
+
 	n, err := f.Write(c)
-	if n == 0 {
-		return errors.New("No bytes written to file: " + fID)
-	}
 	if err != nil {
 		return errors.New("Error: " + err.Error() + ". Failed to write to file: " + fID)
 	}
+	if n == 0 {
+		return errors.New("No bytes written to file: " + fID)
+	}
 	return nil
 }
 
-func resDeleteCache(id string, fs afero.Fs, cfg config.Provider) error {
+func deleteCache(id string, fs afero.Fs, cfg config.Provider) error {
 	return fs.Remove(getCacheFileID(cfg, id))
 }
 
-// resGetRemote loads the content of a remote file. This method is thread safe.
-func resGetRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client) ([]byte, error) {
-	c, err := resGetCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
+// getRemote loads the content of a remote file. This method is thread safe.
+func getRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client) ([]byte, error) {
+	c, err := getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
 	if c != nil && err == nil {
 		return c, nil
 	}
@@ -137,8 +139,8 @@
 	remoteURLLock.URLLock(url)
 	defer func() { remoteURLLock.URLUnlock(url) }()
 
-	// avoid multiple locks due to calling resGetCache twice
-	c, err = resGetCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
+	// avoid multiple locks due to calling getCache twice
+	c, err = getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
 	if c != nil && err == nil {
 		return c, nil
 	}
@@ -156,7 +158,7 @@
 	if err != nil {
 		return nil, err
 	}
-	err = resWriteCache(url, c, fs, cfg, cfg.GetBool("ignoreCache"))
+	err = writeCache(url, c, fs, cfg, cfg.GetBool("ignoreCache"))
 	if err != nil {
 		return nil, err
 	}
@@ -164,8 +166,8 @@
 	return c, nil
 }
 
-// resGetLocal loads the content of a local file
-func resGetLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
+// getLocal loads the content of a local file
+func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
 	filename := filepath.Join(cfg.GetString("workingDir"), url)
 	if e, err := helpers.Exists(filename, fs); !e {
 		return nil, err
@@ -175,15 +177,15 @@
 
 }
 
-// resGetResource loads the content of a local or remote file
-func (ns *Namespace) resGetResource(url string) ([]byte, error) {
+// getResource loads the content of a local or remote file
+func (ns *Namespace) getResource(url string) ([]byte, error) {
 	if url == "" {
 		return nil, nil
 	}
 	if strings.Contains(url, "://") {
-		return resGetRemote(url, ns.deps.Fs.Source, ns.deps.Cfg, http.DefaultClient)
+		return getRemote(url, ns.deps.Fs.Source, ns.deps.Cfg, http.DefaultClient)
 	}
-	return resGetLocal(url, ns.deps.Fs.Source, ns.deps.Cfg)
+	return getLocal(url, ns.deps.Fs.Source, ns.deps.Cfg)
 }
 
 // GetJSON expects one or n-parts of a URL to a resource which can either be a local or a remote one.
@@ -194,7 +196,7 @@
 	url := strings.Join(urlParts, "")
 
 	for i := 0; i <= resRetries; i++ {
-		c, err := ns.resGetResource(url)
+		c, err := ns.getResource(url)
 		if err != nil {
 			jww.ERROR.Printf("Failed to get json resource %s with error message %s", url, err)
 			return nil
@@ -205,7 +207,7 @@
 			jww.ERROR.Printf("Cannot read json from resource %s with error message %s", url, err)
 			jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
 			time.Sleep(resSleep)
-			resDeleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
+			deleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
 			continue
 		}
 		break
@@ -238,11 +240,11 @@
 	var clearCacheSleep = func(i int, u string) {
 		jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
 		time.Sleep(resSleep)
-		resDeleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
+		deleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
 	}
 
 	for i := 0; i <= resRetries; i++ {
-		c, err := ns.resGetResource(url)
+		c, err := ns.getResource(url)
 
 		if err == nil && !bytes.Contains(c, []byte(sep)) {
 			err = errors.New("Cannot find separator " + sep + " in CSV.")
--- a/tpl/data/resources_test.go
+++ b/tpl/data/resources_test.go
@@ -54,7 +54,7 @@
 
 	for _, test := range tests {
 		cfg := viper.New()
-		c, err := resGetCache(test.path, fs, cfg, test.ignore)
+		c, err := getCache(test.path, fs, cfg, test.ignore)
 		if err != nil {
 			t.Errorf("Error getting cache: %s", err)
 		}
@@ -62,12 +62,12 @@
 			t.Errorf("There is content where there should not be anything: %s", string(c))
 		}
 
-		err = resWriteCache(test.path, test.content, fs, cfg, test.ignore)
+		err = writeCache(test.path, test.content, fs, cfg, test.ignore)
 		if err != nil {
 			t.Errorf("Error writing cache: %s", err)
 		}
 
-		c, err = resGetCache(test.path, fs, cfg, test.ignore)
+		c, err = getCache(test.path, fs, cfg, test.ignore)
 		if err != nil {
 			t.Errorf("Error getting cache after writing: %s", err)
 		}
@@ -107,7 +107,7 @@
 			t.Error(err)
 		}
 
-		c, err := resGetLocal(test.path, fs.Source, v)
+		c, err := getLocal(test.path, fs.Source, v)
 		if err != nil {
 			t.Errorf("Error getting resource content: %s", err)
 		}
@@ -154,7 +154,7 @@
 
 		cfg := viper.New()
 
-		c, err := resGetRemote(test.path, fs, cfg, cl)
+		c, err := getRemote(test.path, fs, cfg, cl)
 		if err != nil {
 			t.Errorf("Error getting resource content: %s", err)
 		}
@@ -161,7 +161,7 @@
 		if !bytes.Equal(c, test.content) {
 			t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c))
 		}
-		cc, cErr := resGetCache(test.path, fs, cfg, test.ignore)
+		cc, cErr := getCache(test.path, fs, cfg, test.ignore)
 		if cErr != nil {
 			t.Error(cErr)
 		}
@@ -199,7 +199,7 @@
 			go func(gor int) {
 				defer wg.Done()
 				for j := 0; j < 10; j++ {
-					c, err := resGetRemote(url, fs, cfg, cl)
+					c, err := getRemote(url, fs, cfg, cl)
 					if err != nil {
 						t.Errorf("Error getting resource content: %s", err)
 					}