feat(cache): set cache directory correctly

resolves #1017
This commit is contained in:
Jan De Dobbeleer 2021-10-03 07:27:18 +02:00 committed by Jan De Dobbeleer
parent 1bf67c22c5
commit a118e178b5
5 changed files with 53 additions and 9 deletions

View file

@ -85,6 +85,7 @@ type environmentInfo interface {
isWsl() bool isWsl() bool
stackCount() int stackCount() int
getTerminalWidth() (int, error) getTerminalWidth() (int, error)
getCachePath() string
cache() cache cache() cache
close() close()
} }
@ -155,13 +156,13 @@ func (env *environment) init(args *args) {
env.cmdCache = &commandCache{ env.cmdCache = &commandCache{
commands: newConcurrentMap(), commands: newConcurrentMap(),
} }
env.fileCache = &fileCache{}
env.fileCache.init(env.homeDir())
tracer := &tracer{ tracer := &tracer{
debug: *args.Debug, debug: *args.Debug,
} }
tracer.init(env.homeDir()) tracer.init(env.homeDir())
env.tracer = tracer env.tracer = tracer
env.fileCache = &fileCache{}
env.fileCache.init(env.getCachePath())
} }
func (env *environment) getenv(key string) string { func (env *environment) getenv(key string) string {
@ -501,3 +502,19 @@ func cleanHostName(hostName string) string {
} }
return hostName 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
}

View file

@ -7,7 +7,7 @@ import (
) )
const ( const (
cachePath = "/.omp.cache" fileName = "/omp.cache"
) )
type cacheObject struct { type cacheObject struct {
@ -18,13 +18,13 @@ type cacheObject struct {
type fileCache struct { type fileCache struct {
cache *concurrentMap cache *concurrentMap
home string cachePath string
} }
func (fc *fileCache) init(home string) { func (fc *fileCache) init(cachePath string) {
fc.cache = newConcurrentMap() fc.cache = newConcurrentMap()
fc.home = home fc.cachePath = cachePath
content, err := ioutil.ReadFile(fc.home + cachePath) content, err := ioutil.ReadFile(fc.cachePath + fileName)
if err != nil { if err != nil {
return return
} }
@ -41,7 +41,7 @@ func (fc *fileCache) init(home string) {
func (fc *fileCache) close() { func (fc *fileCache) close() {
if dump, err := json.MarshalIndent(fc.cache.list(), "", " "); err == nil { if dump, err := json.MarshalIndent(fc.cache.list(), "", " "); err == nil {
_ = ioutil.WriteFile(fc.home+cachePath, dump, 0644) _ = ioutil.WriteFile(fc.cachePath+fileName, dump, 0644)
} }
} }

View file

@ -43,3 +43,16 @@ func (env *environment) getPlatform() string {
p, _, _, _ := host.PlatformInformation() p, _, _, _ := host.PlatformInformation()
return p 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()
}

View file

@ -86,3 +86,12 @@ func (env *environment) getTerminalWidth() (int, error) {
func (env *environment) getPlatform() string { func (env *environment) getPlatform() string {
return windowsPlatform 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()
}

View file

@ -153,6 +153,11 @@ func (env *MockedEnvironment) getTerminalWidth() (int, error) {
return args.Int(0), args.Error(1) 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 { func (env *MockedEnvironment) cache() cache {
args := env.Called(nil) args := env.Called(nil)
return args.Get(0).(cache) return args.Get(0).(cache)