tpl/data: Shorten some internal func names

This commit is contained in:
Cameron Moore 2017-05-01 14:04:21 -05:00 committed by Bjørn Erik Pedersen
parent f604076de1
commit 5f095c27ea
2 changed files with 42 additions and 40 deletions

View file

@ -68,20 +68,21 @@ func (l *remoteLock) URLUnlock(url string) {
} }
} }
// getCacheFileID returns the cache ID for a string // getCacheFileID returns the cache ID for a string.
func getCacheFileID(cfg config.Provider, id string) string { func getCacheFileID(cfg config.Provider, id string) string {
return cfg.GetString("cacheDir") + url.QueryEscape(id) return cfg.GetString("cacheDir") + url.QueryEscape(id)
} }
// resGetCache returns the content for an ID from the file cache or an error // getCache returns the content for an ID from the file cache or an error.
// if the file is not found returns nil,nil // If the ID is not found, return nil,nil.
func resGetCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool) ([]byte, error) { func getCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool) ([]byte, error) {
resCacheMu.RLock()
defer resCacheMu.RUnlock()
if ignoreCache { if ignoreCache {
return nil, nil return nil, nil
} }
resCacheMu.RLock()
defer resCacheMu.RUnlock()
fID := getCacheFileID(cfg, id) fID := getCacheFileID(cfg, id)
isExists, err := helpers.Exists(fID, fs) isExists, err := helpers.Exists(fID, fs)
if err != nil { if err != nil {
@ -92,40 +93,41 @@ func resGetCache(id string, fs afero.Fs, cfg config.Provider, ignoreCache bool)
} }
return afero.ReadFile(fs, fID) return afero.ReadFile(fs, fID)
} }
// resWriteCache writes bytes to an ID into the file cache // writeCache writes bytes associated with an ID into the file cache.
func resWriteCache(id string, c []byte, fs afero.Fs, cfg config.Provider, ignoreCache bool) error { func writeCache(id string, c []byte, fs afero.Fs, cfg config.Provider, ignoreCache bool) error {
resCacheMu.Lock()
defer resCacheMu.Unlock()
if ignoreCache { if ignoreCache {
return nil return nil
} }
resCacheMu.Lock()
defer resCacheMu.Unlock()
fID := getCacheFileID(cfg, id) fID := getCacheFileID(cfg, id)
f, err := fs.Create(fID) f, err := fs.Create(fID)
if err != nil { if err != nil {
return errors.New("Error: " + err.Error() + ". Failed to create file: " + fID) return errors.New("Error: " + err.Error() + ". Failed to create file: " + fID)
} }
defer f.Close() defer f.Close()
n, err := f.Write(c) n, err := f.Write(c)
if n == 0 {
return errors.New("No bytes written to file: " + fID)
}
if err != nil { if err != nil {
return errors.New("Error: " + err.Error() + ". Failed to write to file: " + fID) 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 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)) return fs.Remove(getCacheFileID(cfg, id))
} }
// resGetRemote loads the content of a remote file. This method is thread safe. // getRemote 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) { func getRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client) ([]byte, error) {
c, err := resGetCache(url, fs, cfg, cfg.GetBool("ignoreCache")) c, err := getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
if c != nil && err == nil { if c != nil && err == nil {
return c, nil return c, nil
} }
@ -137,8 +139,8 @@ func resGetRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client)
remoteURLLock.URLLock(url) remoteURLLock.URLLock(url)
defer func() { remoteURLLock.URLUnlock(url) }() defer func() { remoteURLLock.URLUnlock(url) }()
// avoid multiple locks due to calling resGetCache twice // avoid multiple locks due to calling getCache twice
c, err = resGetCache(url, fs, cfg, cfg.GetBool("ignoreCache")) c, err = getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
if c != nil && err == nil { if c != nil && err == nil {
return c, nil return c, nil
} }
@ -156,7 +158,7 @@ func resGetRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = resWriteCache(url, c, fs, cfg, cfg.GetBool("ignoreCache")) err = writeCache(url, c, fs, cfg, cfg.GetBool("ignoreCache"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -164,8 +166,8 @@ func resGetRemote(url string, fs afero.Fs, cfg config.Provider, hc *http.Client)
return c, nil return c, nil
} }
// resGetLocal loads the content of a local file // getLocal loads the content of a local file
func resGetLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) { func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
filename := filepath.Join(cfg.GetString("workingDir"), url) filename := filepath.Join(cfg.GetString("workingDir"), url)
if e, err := helpers.Exists(filename, fs); !e { if e, err := helpers.Exists(filename, fs); !e {
return nil, err return nil, err
@ -175,15 +177,15 @@ func resGetLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
} }
// resGetResource loads the content of a local or remote file // getResource loads the content of a local or remote file
func (ns *Namespace) resGetResource(url string) ([]byte, error) { func (ns *Namespace) getResource(url string) ([]byte, error) {
if url == "" { if url == "" {
return nil, nil return nil, nil
} }
if strings.Contains(url, "://") { 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. // 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 @@ func (ns *Namespace) GetJSON(urlParts ...string) interface{} {
url := strings.Join(urlParts, "") url := strings.Join(urlParts, "")
for i := 0; i <= resRetries; i++ { for i := 0; i <= resRetries; i++ {
c, err := ns.resGetResource(url) c, err := ns.getResource(url)
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to get json resource %s with error message %s", url, err) jww.ERROR.Printf("Failed to get json resource %s with error message %s", url, err)
return nil return nil
@ -205,7 +207,7 @@ func (ns *Namespace) GetJSON(urlParts ...string) interface{} {
jww.ERROR.Printf("Cannot read json from resource %s with error message %s", url, err) 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) jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
time.Sleep(resSleep) time.Sleep(resSleep)
resDeleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg) deleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
continue continue
} }
break break
@ -238,11 +240,11 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) [][]string {
var clearCacheSleep = func(i int, u string) { var clearCacheSleep = func(i int, u string) {
jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep) jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
time.Sleep(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++ { for i := 0; i <= resRetries; i++ {
c, err := ns.resGetResource(url) c, err := ns.getResource(url)
if err == nil && !bytes.Contains(c, []byte(sep)) { if err == nil && !bytes.Contains(c, []byte(sep)) {
err = errors.New("Cannot find separator " + sep + " in CSV.") err = errors.New("Cannot find separator " + sep + " in CSV.")

View file

@ -54,7 +54,7 @@ func TestScpCache(t *testing.T) {
for _, test := range tests { for _, test := range tests {
cfg := viper.New() cfg := viper.New()
c, err := resGetCache(test.path, fs, cfg, test.ignore) c, err := getCache(test.path, fs, cfg, test.ignore)
if err != nil { if err != nil {
t.Errorf("Error getting cache: %s", err) t.Errorf("Error getting cache: %s", err)
} }
@ -62,12 +62,12 @@ func TestScpCache(t *testing.T) {
t.Errorf("There is content where there should not be anything: %s", string(c)) 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 { if err != nil {
t.Errorf("Error writing cache: %s", err) 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 { if err != nil {
t.Errorf("Error getting cache after writing: %s", err) t.Errorf("Error getting cache after writing: %s", err)
} }
@ -107,7 +107,7 @@ func TestScpGetLocal(t *testing.T) {
t.Error(err) t.Error(err)
} }
c, err := resGetLocal(test.path, fs.Source, v) c, err := getLocal(test.path, fs.Source, v)
if err != nil { if err != nil {
t.Errorf("Error getting resource content: %s", err) t.Errorf("Error getting resource content: %s", err)
} }
@ -154,14 +154,14 @@ func TestScpGetRemote(t *testing.T) {
cfg := viper.New() cfg := viper.New()
c, err := resGetRemote(test.path, fs, cfg, cl) c, err := getRemote(test.path, fs, cfg, cl)
if err != nil { if err != nil {
t.Errorf("Error getting resource content: %s", err) t.Errorf("Error getting resource content: %s", err)
} }
if !bytes.Equal(c, test.content) { if !bytes.Equal(c, test.content) {
t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c)) 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 { if cErr != nil {
t.Error(cErr) t.Error(cErr)
} }
@ -199,7 +199,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
go func(gor int) { go func(gor int) {
defer wg.Done() defer wg.Done()
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
c, err := resGetRemote(url, fs, cfg, cl) c, err := getRemote(url, fs, cfg, cl)
if err != nil { if err != nil {
t.Errorf("Error getting resource content: %s", err) t.Errorf("Error getting resource content: %s", err)
} }