feat(cache): make cache location logic cross platform
Some checks are pending
Code QL / code-ql (push) Waiting to run
Release / changelog (push) Waiting to run
Release / artifacts (push) Blocked by required conditions

This commit is contained in:
Jan De Dobbeleer 2024-08-29 09:57:46 +02:00 committed by Jan De Dobbeleer
parent 90612a42d9
commit 476bfd1fff
3 changed files with 42 additions and 48 deletions

View file

@ -798,6 +798,48 @@ func (term *Terminal) SystemInfo() (*SystemInfo, error) {
return s, nil return s, nil
} }
func (term *Terminal) CachePath() string {
defer term.Trace(time.Now())
returnOrBuildCachePath := func(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 := filepath.Join(path, "oh-my-posh")
if _, err := os.Stat(cachePath); err == nil {
return cachePath
}
if err := os.Mkdir(cachePath, 0o755); err != nil {
return ""
}
return cachePath
}
// WINDOWS cache folder, should not exist elsewhere
if cachePath := returnOrBuildCachePath(term.Getenv("LOCALAPPDATA")); len(cachePath) != 0 {
return cachePath
}
// allow the user to set the cache path using OMP_CACHE_DIR
if cachePath := returnOrBuildCachePath(term.Getenv("OMP_CACHE_DIR")); len(cachePath) != 0 {
return cachePath
}
// get XDG_CACHE_HOME if present
if cachePath := returnOrBuildCachePath(term.Getenv("XDG_CACHE_HOME")); len(cachePath) != 0 {
return cachePath
}
// HOME cache folder
if cachePath := returnOrBuildCachePath(term.Home() + "/.cache"); len(cachePath) != 0 {
return cachePath
}
return term.Home()
}
func IsPathSeparator(env Environment, c uint8) bool { func IsPathSeparator(env Environment, c uint8) bool {
if c == '/' { if c == '/' {
return true return true
@ -876,19 +918,3 @@ 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 := filepath.Join(path, "oh-my-posh")
if _, err := os.Stat(cachePath); err == nil {
return cachePath
}
if err := os.Mkdir(cachePath, 0o755); err != nil {
return ""
}
return cachePath
}

View file

@ -113,27 +113,6 @@ func (term *Terminal) Platform() string {
return platform return platform
} }
func (term *Terminal) CachePath() string {
defer term.Trace(time.Now())
// allow the user to set the cache path using OMP_CACHE_DIR
if cachePath := returnOrBuildCachePath(term.Getenv("OMP_CACHE_DIR")); len(cachePath) != 0 {
return cachePath
}
// get XDG_CACHE_HOME if present
if cachePath := returnOrBuildCachePath(term.Getenv("XDG_CACHE_HOME")); len(cachePath) != 0 {
return cachePath
}
// HOME cache folder
if cachePath := returnOrBuildCachePath(term.Home() + "/.cache"); len(cachePath) != 0 {
return cachePath
}
return term.Home()
}
func (term *Terminal) WindowsRegistryKeyValue(_ string) (*WindowsRegistryValue, error) { func (term *Terminal) WindowsRegistryKeyValue(_ string) (*WindowsRegistryValue, error) {
return nil, &NotImplemented{} return nil, &NotImplemented{}
} }

View file

@ -121,17 +121,6 @@ func (term *Terminal) Platform() string {
return WINDOWS return WINDOWS
} }
func (term *Terminal) CachePath() string {
defer term.Trace(time.Now())
// get LOCALAPPDATA if present
if cachePath := returnOrBuildCachePath(term.Getenv("LOCALAPPDATA")); len(cachePath) != 0 {
return cachePath
}
return term.Home()
}
// Takes a registry path to a key like // Takes a registry path to a key like
// //
// "HKLM\Software\Microsoft\Windows NT\CurrentVersion\EditionID" // "HKLM\Software\Microsoft\Windows NT\CurrentVersion\EditionID"