From a118e178b57edefb826330b98ee04875190a3f2c Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 3 Oct 2021 07:27:18 +0200 Subject: [PATCH] feat(cache): set cache directory correctly resolves #1017 --- src/environment.go | 21 +++++++++++++++++++-- src/environment_cache.go | 14 +++++++------- src/environment_unix.go | 13 +++++++++++++ src/environment_windows.go | 9 +++++++++ src/segment_path_test.go | 5 +++++ 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/environment.go b/src/environment.go index 735678d2..ce9b11c8 100644 --- a/src/environment.go +++ b/src/environment.go @@ -85,6 +85,7 @@ type environmentInfo interface { isWsl() bool stackCount() int getTerminalWidth() (int, error) + getCachePath() string cache() cache close() } @@ -155,13 +156,13 @@ func (env *environment) init(args *args) { env.cmdCache = &commandCache{ commands: newConcurrentMap(), } - env.fileCache = &fileCache{} - env.fileCache.init(env.homeDir()) tracer := &tracer{ debug: *args.Debug, } tracer.init(env.homeDir()) env.tracer = tracer + env.fileCache = &fileCache{} + env.fileCache.init(env.getCachePath()) } func (env *environment) getenv(key string) string { @@ -501,3 +502,19 @@ func cleanHostName(hostName string) string { } return hostName } + +func returnOrBuildCachePath(path string) string { + // validate root path + if _, err := os.Stat(path); err != nil { + return "" + } + // validate oh-my-posh folder, if non existent, create it + cachePath := path + "/oh-my-posh" + if _, err := os.Stat(cachePath); err == nil { + return cachePath + } + if err := os.Mkdir(cachePath, 0755); err != nil { + return "" + } + return cachePath +} diff --git a/src/environment_cache.go b/src/environment_cache.go index 94e9ffc8..ac50b228 100644 --- a/src/environment_cache.go +++ b/src/environment_cache.go @@ -7,7 +7,7 @@ import ( ) const ( - cachePath = "/.omp.cache" + fileName = "/omp.cache" ) type cacheObject struct { @@ -17,14 +17,14 @@ type cacheObject struct { } type fileCache struct { - cache *concurrentMap - home string + cache *concurrentMap + cachePath string } -func (fc *fileCache) init(home string) { +func (fc *fileCache) init(cachePath string) { fc.cache = newConcurrentMap() - fc.home = home - content, err := ioutil.ReadFile(fc.home + cachePath) + fc.cachePath = cachePath + content, err := ioutil.ReadFile(fc.cachePath + fileName) if err != nil { return } @@ -41,7 +41,7 @@ func (fc *fileCache) init(home string) { func (fc *fileCache) close() { if dump, err := json.MarshalIndent(fc.cache.list(), "", " "); err == nil { - _ = ioutil.WriteFile(fc.home+cachePath, dump, 0644) + _ = ioutil.WriteFile(fc.cachePath+fileName, dump, 0644) } } diff --git a/src/environment_unix.go b/src/environment_unix.go index bd159b84..ef9c6eaf 100644 --- a/src/environment_unix.go +++ b/src/environment_unix.go @@ -43,3 +43,16 @@ func (env *environment) getPlatform() string { p, _, _, _ := host.PlatformInformation() return p } + +func (env *environment) getCachePath() string { + defer env.tracer.trace(time.Now(), "getCachePath") + // get XDG_CACHE_HOME if present + if cachePath := returnOrBuildCachePath(env.getenv("XDG_CACHE_HOME")); len(cachePath) != 0 { + return cachePath + } + // HOME cache folder + if cachePath := returnOrBuildCachePath(env.homeDir() + "/.cache"); len(cachePath) != 0 { + return cachePath + } + return env.homeDir() +} diff --git a/src/environment_windows.go b/src/environment_windows.go index 1b77f552..9b03c961 100644 --- a/src/environment_windows.go +++ b/src/environment_windows.go @@ -86,3 +86,12 @@ func (env *environment) getTerminalWidth() (int, error) { func (env *environment) getPlatform() string { return windowsPlatform } + +func (env *environment) getCachePath() string { + defer env.tracer.trace(time.Now(), "getCachePath") + // get LOCALAPPDATA if present + if cachePath := returnOrBuildCachePath(env.getenv("LOCALAPPDATA")); len(cachePath) != 0 { + return cachePath + } + return env.homeDir() +} diff --git a/src/segment_path_test.go b/src/segment_path_test.go index 46f69b9a..764fe144 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -153,6 +153,11 @@ func (env *MockedEnvironment) getTerminalWidth() (int, error) { return args.Int(0), args.Error(1) } +func (env *MockedEnvironment) getCachePath() string { + args := env.Called(nil) + return args.String(0) +} + func (env *MockedEnvironment) cache() cache { args := env.Called(nil) return args.Get(0).(cache)