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
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
}

View file

@ -7,7 +7,7 @@ import (
)
const (
cachePath = "/.omp.cache"
fileName = "/omp.cache"
)
type cacheObject struct {
@ -18,13 +18,13 @@ type cacheObject struct {
type fileCache struct {
cache *concurrentMap
home string
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)
}
}

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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)