From cb2d24bc1155d78284a6dccf15749a9e5e95db24 Mon Sep 17 00:00:00 2001 From: afh Date: Mon, 19 Apr 2021 07:38:49 +0200 Subject: [PATCH] Add support for :PREFERRED_CACHE_HOME: --- pkg/pathutil/pathutil.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/pathutil/pathutil.go b/pkg/pathutil/pathutil.go index 86b0832..7606705 100644 --- a/pkg/pathutil/pathutil.go +++ b/pkg/pathutil/pathutil.go @@ -22,6 +22,22 @@ func UserPreferredConfigDir() string { return config } +// UserPreferredCacheDir returns the preferred cache directory for the user +func UserPreferredCacheDir() string { + defaultCacheDir := "/tmp" + + cache, err := os.UserCacheDir() + if err != nil { + return defaultCacheDir + } + + if cache == "" { + return defaultCacheDir + } + + return cache +} + // UserPreferredHomeDir returns the preferred home directory for the user func UserPreferredHomeDir() string { home, err := os.UserHomeDir() @@ -36,6 +52,7 @@ func UserPreferredHomeDir() string { func NormalizePath(path string) string { userHome := UserPreferredHomeDir() userConfigHome := UserPreferredConfigDir() + userCacheHome := UserPreferredCacheDir() // expand tilde if strings.HasPrefix(path, "~/") { @@ -44,6 +61,7 @@ func NormalizePath(path string) string { path = strings.Replace(path, ":HOME:", userHome, -1) path = strings.Replace(path, ":PREFERRED_CONFIG_HOME:", userConfigHome, -1) + path = strings.Replace(path, ":PREFERRED_CACHE_HOME:", userCacheHome, -1) path = strings.Replace(path, "/", string(filepath.Separator), -1) return filepath.Clean(path)