Skip to content

Commit

Permalink
feat(cache): set cache directory correctly
Browse files Browse the repository at this point in the history
resolves #1017
  • Loading branch information
JanDeDobbeleer committed Oct 3, 2021
1 parent 1bf67c2 commit a118e17
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
21 changes: 19 additions & 2 deletions src/environment.go
Expand Up @@ -85,6 +85,7 @@ type environmentInfo interface {
isWsl() bool
stackCount() int
getTerminalWidth() (int, error)
getCachePath() string
cache() cache
close()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions src/environment_cache.go
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
cachePath = "/.omp.cache"
fileName = "/omp.cache"
)

type cacheObject struct {
Expand All @@ -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
}
Expand All @@ -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)
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/environment_unix.go
Expand Up @@ -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()
}
9 changes: 9 additions & 0 deletions src/environment_windows.go
Expand Up @@ -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()
}
5 changes: 5 additions & 0 deletions src/segment_path_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit a118e17

Please sign in to comment.