diff options
-rw-r--r-- | config/allconfig/allconfig.go | 5 | ||||
-rw-r--r-- | helpers/path.go | 17 | ||||
-rw-r--r-- | main_test.go | 19 | ||||
-rw-r--r-- | testscripts/commands/config__cachedir.txt | 18 |
4 files changed, 54 insertions, 5 deletions
diff --git a/config/allconfig/allconfig.go b/config/allconfig/allconfig.go index 72f2883f2..ec95f5eba 100644 --- a/config/allconfig/allconfig.go +++ b/config/allconfig/allconfig.go @@ -729,6 +729,7 @@ func fromLoadConfigResult(fs afero.Fs, logger loggers.Logger, res config.LoadCon cfg := res.Cfg all := &Config{} + err := decodeConfigFromParams(fs, bcfg, cfg, all, nil) if err != nil { return nil, err @@ -871,6 +872,10 @@ func fromLoadConfigResult(fs afero.Fs, logger loggers.Logger, res config.LoadCon bcfg.PublishDir = all.PublishDir res.BaseConfig = bcfg + all.CommonDirs.CacheDir = bcfg.CacheDir + for _, l := range langConfigs { + l.CommonDirs.CacheDir = bcfg.CacheDir + } cm := &Configs{ Base: all, diff --git a/helpers/path.go b/helpers/path.go index 4c2799ac2..57877bfed 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -394,6 +394,7 @@ func OpenFileForWriting(fs afero.Fs, filename string) (afero.File, error) { // The dir will be created if it does not exist. func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) { cacheDir = cacheDirDefault(cacheDir) + if cacheDir != "" { exists, err := DirExists(cacheDir, fs) if err != nil { @@ -408,12 +409,22 @@ func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) { return cacheDir, nil } + const hugoCacheBase = "hugo_cache" + + userCacheDir, err := os.UserCacheDir() + if err == nil { + cacheDir := filepath.Join(userCacheDir, hugoCacheBase) + if err := fs.Mkdir(cacheDir, 0777); err == nil || os.IsExist(err) { + return cacheDir, nil + } + } + // Fall back to a cache in /tmp. userName := os.Getenv("USER") if userName != "" { - return GetTempDir("hugo_cache_"+userName, fs), nil + return GetTempDir(hugoCacheBase+"_"+userName, fs), nil } else { - return GetTempDir("hugo_cache", fs), nil + return GetTempDir(hugoCacheBase, fs), nil } } @@ -433,7 +444,7 @@ func cacheDirDefault(cacheDir string) string { return "/opt/build/cache/hugo_cache/" } - // This will fall back to an hugo_cache folder in the tmp dir, which should work fine for most CI + // This will fall back to an hugo_cache folder in either os.UserCacheDir or the tmp dir, which should work fine for most CI // providers. See this for a working CircleCI setup: // https://github.com/bep/hugo-sass-test/blob/6c3960a8f4b90e8938228688bc49bdcdd6b2d99e/.circleci/config.yml // If not, they can set the HUGO_CACHEDIR environment variable or cacheDir config key. diff --git a/main_test.go b/main_test.go index 6274cd9bb..c5a59828f 100644 --- a/main_test.go +++ b/main_test.go @@ -375,8 +375,23 @@ func testSetupFunc() func(env *testscript.Env) error { return func(env *testscript.Env) error { var keyVals []string keyVals = append(keyVals, "HUGO_TESTRUN", "true") - hugoCachedDir := filepath.Join(env.WorkDir, "hugocache") - keyVals = append(keyVals, "HUGO_CACHEDIR", hugoCachedDir) + keyVals = append(keyVals, "HUGO_CACHEDIR", filepath.Join(env.WorkDir, "hugocache")) + xdghome := filepath.Join(env.WorkDir, "xdgcachehome") + keyVals = append(keyVals, "XDG_CACHE_HOME", xdghome) + home := filepath.Join(env.WorkDir, "home") + keyVals = append(keyVals, "HOME", home) + + if runtime.GOOS == "darwin" { + if err := os.MkdirAll(filepath.Join(home, "Library", "Caches"), 0777); err != nil { + return err + } + } + + if runtime.GOOS == "linux" { + if err := os.MkdirAll(xdghome, 0777); err != nil { + return err + } + } keyVals = append(keyVals, "SOURCE", sourceDir) diff --git a/testscripts/commands/config__cachedir.txt b/testscripts/commands/config__cachedir.txt new file mode 100644 index 000000000..aecb40b6c --- /dev/null +++ b/testscripts/commands/config__cachedir.txt @@ -0,0 +1,18 @@ + +[windows] skip + +env HUGO_CACHEDIR= +hugo config + +[darwin] stdout 'home/Library/Caches/hugo_cache' +[linux] stdout 'xdgcachehome/hugo_cache' + +# Repeat it to make sure it handles an existing hugo_cache dir. +hugo config + +[darwin] stdout 'home/Library/Caches/hugo_cache' +[linux] stdout 'xdgcachehome/hugo_cache' + +-- hugo.toml -- +baseURL="https://example.com/" +title="My New Hugo Site" |