Skip to content

Commit

Permalink
Changes cache path
Browse files Browse the repository at this point in the history
Changed cache from XDG_CONFIG_HOME to XDG_CACHE_HOME

Fixes #66
  • Loading branch information
leucos committed Jan 5, 2021
1 parent 4eeccc4 commit 6a3600f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
31 changes: 19 additions & 12 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ func (a *App) Update(definitions, all bool, nocache bool, which ...string) error
return err
}

a.saveCache()
err = a.saveCache()

return nil
return err
}

func (a *App) updateGithub() error {
Expand Down Expand Up @@ -1035,7 +1035,7 @@ func (a *App) getDistributionsFromLock() ([]string, []string) {
}

func (a *App) loadCache() {
conf, err := getConfigDir()
conf, err := getCacheDir()
if err != nil {
return
}
Expand All @@ -1058,28 +1058,35 @@ func (a *App) loadCache() {
}
}

func (a *App) saveCache() {
conf, err := getConfigDir()
func (a *App) saveCache() error {
cache, err := getCacheDir()
if err != nil {
return
return nil
}

conf = filepath.Join(conf, "/cache.json")
err = os.MkdirAll(cache, 0750)
if err != nil {
return fmt.Errorf("unable to create cache directory '%s': %w", cache, err)
}

cache = filepath.Join(cache, "/cache.json")

js, err := json.Marshal(&a.cache)
if err != nil {
a.logger.Error().Err(err).Msgf("unable to marshal cache %q", conf)
return
a.logger.Error().Err(err).Msgf("unable to marshal cache %q", cache)
return nil
}

fd, err := os.OpenFile(conf, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0640)
fd, err := os.OpenFile(cache, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0640)
if err != nil {
a.logger.Error().Err(err).Msgf("unable to write cache %s: please check file permissions", conf)
return
a.logger.Error().Err(err).Msgf("unable to write cache %s: please check file permissions", cache)
return nil
}
defer fd.Close()

fd.Write(js)

return nil
}

func (a *App) createInstallers() {
Expand Down
15 changes: 15 additions & 0 deletions internal/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func getConfigDir() (string, error) {
return dir, nil
}

func getCacheDir() (string, error) {
var err error

dir := os.Getenv("XDG_CACHE_HOME")
if dir == "" {
dir, err = homedir.Dir()
if err != nil {
return "", err
}
dir += "/.cache/binenv"
}

return dir, nil
}

func getDistributionsFilePath() (string, error) {
conf, err := getConfigDir()
if err != nil {
Expand Down

0 comments on commit 6a3600f

Please sign in to comment.