change CacheDir to CachedDir

This commit is contained in:
sugarme 2022-03-09 21:36:43 +11:00
parent 44afdb50ec
commit d5634fc397
2 changed files with 11 additions and 11 deletions

View File

@ -111,12 +111,12 @@ var ModelUrls map[string]string = map[string]string{
//
// CachedPath does several things consequently:
// 1. Resolves input string to a fullpath cached filename candidate.
// 2. Check it at `CachePath`, if exists, then return the candidate. If not
// 3. Retrieves and Caches data to `CachePath` and returns path to cached data
// 2. Check it at `CachedDir`, if exists, then return the candidate. If not
// 3. Retrieves and Caches data to `CachedDir` and returns path to cached data
func CachedPath(filenameOrUrl string) (resolvedPath string, err error) {
filename := path.Base(filenameOrUrl)
// Resolves to "candidate" filename at `CacheDir`
cachedFileCandidate := fmt.Sprintf("%s/%s", CacheDir, filename)
// Resolves to "candidate" filename at `CachedDir`
cachedFileCandidate := fmt.Sprintf("%s/%s", CachedDir, filename)
// 1. Cached candidate file exists
if _, err := os.Stat(cachedFileCandidate); err == nil {
@ -288,9 +288,9 @@ func copyFile(src, dst string) error {
return err
}
// CleanCache removes all files cached at `CacheDir`
// CleanCache removes all files cached at `CachedDir`
func CleanCache() error {
err := os.RemoveAll(CacheDir)
err := os.RemoveAll(CachedDir)
if err != nil {
err = fmt.Errorf("CleanCache() failed: %w", err)
return err

10
init.go
View File

@ -7,13 +7,13 @@ import (
)
var (
CacheDir string = "NOT_SETTING"
CachedDir string = "NOT_SETTING"
gotchEnvKey string = "GOTCH_CACHE"
)
func init() {
homeDir := os.Getenv("HOME")
CacheDir = fmt.Sprintf("%s/.cache/gotch", homeDir) // default dir: "{$HOME}/.cache/gotch"
CachedDir = fmt.Sprintf("%s/.cache/gotch", homeDir) // default dir: "{$HOME}/.cache/gotch"
initEnv()
// log.Printf("INFO: CacheDir=%q\n", CacheDir)
@ -22,11 +22,11 @@ func init() {
func initEnv() {
val := os.Getenv(gotchEnvKey)
if val != "" {
CacheDir = val
CachedDir = val
}
if _, err := os.Stat(CacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(CacheDir, 0755); err != nil {
if _, err := os.Stat(CachedDir); os.IsNotExist(err) {
if err := os.MkdirAll(CachedDir, 0755); err != nil {
log.Fatal(err)
}
}