Skip to content

Commit

Permalink
cache: use os.UserCacheDir
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Apr 9, 2022
1 parent 0d17d08 commit 7c6c7ed
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -41,15 +40,12 @@ func Load(file string, timeout time.Duration) (rawValues []common.RawValue, err
return
}

// TempDir creates a temporary folder for current user and returns the path
func TempDir(name string) (dir string, err error) {
var u *user.User
if u, err = user.Current(); err == nil {
dir = fmt.Sprintf("%v/carapace", os.TempDir())
if err = os.MkdirAll(dir, 0777); err == nil {
dir = fmt.Sprintf("%v/%v/%v/%v", dir, u.Username, uid.Executable(), name)
err = os.MkdirAll(dir, 0700)
}
// CacheDir creates a cache folder for current user and returns the path
func CacheDir(name string) (dir string, err error) {
var userCacheDir string
if userCacheDir, err = os.UserCacheDir(); err == nil {
dir = fmt.Sprintf("%v/carapace/%v/%v", userCacheDir, uid.Executable(), name)
err = os.MkdirAll(dir, 0700)
}
return
}
Expand All @@ -66,7 +62,7 @@ func File(callerFile string, callerLine int, keys ...cache.Key) (file string, er
}
ids = append(ids, id)
}
if dir, err := TempDir(uid); err == nil {
if dir, err := CacheDir(uid); err == nil {
file = dir + "/" + uidKeys(ids...)
}
return
Expand Down

0 comments on commit 7c6c7ed

Please sign in to comment.