Skip to content

Commit

Permalink
feat(cache): make cache location logic cross platform
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Aug 29, 2024
1 parent 90612a4 commit 476bfd1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
58 changes: 42 additions & 16 deletions src/runtime/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,48 @@ func (term *Terminal) SystemInfo() (*SystemInfo, error) {
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 {
if c == '/' {
return true
Expand Down Expand Up @@ -876,19 +918,3 @@ 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 := 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
}
21 changes: 0 additions & 21 deletions src/runtime/terminal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,6 @@ func (term *Terminal) Platform() string {
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) {
return nil, &NotImplemented{}
}
Expand Down
11 changes: 0 additions & 11 deletions src/runtime/terminal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ func (term *Terminal) Platform() string {
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
//
// "HKLM\Software\Microsoft\Windows NT\CurrentVersion\EditionID"
Expand Down

0 comments on commit 476bfd1

Please sign in to comment.