Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cache directory as defined by XDG Base Directory Specification #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Package cache provides an interface for interfacing with the Glide local cache
//
// Glide has a local cache of metadata and repositories similar to the GOPATH.
// To store the cache Glide creates a .glide directory with a cache subdirectory.
// This is usually in the users home directory unless there is no accessible
// home directory in which case the .glide directory is in the root of the
// repository.
// To store the cache Glide creates a $XDG_CACHE_HOME/glide directory with a
// cache subdirectory. This is usually in the ~/.cache/glide directory.
//
// To get the cache location use the `cache.Location()` function. This will
// return the proper base location in your environment.
Expand Down Expand Up @@ -40,7 +38,6 @@ import (
"time"

"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
)

// Enabled sets if the cache is globally enabled. Defaults to true.
Expand All @@ -63,13 +60,13 @@ func Setup() {
}
msg.Debug("Setting up the cache directory")
pths := []string{
"cache",
filepath.Join("cache", "src"),
filepath.Join("cache", "info"),
"src",
"info",
}

cachedir := filepath.Join(xdgCacheDir(), "glide")
for _, l := range pths {
err := os.MkdirAll(filepath.Join(gpath.Home(), l), 0755)
err := os.MkdirAll(filepath.Join(cachedir, l), 0755)
if err != nil {
msg.Die("Cache directory unavailable: %s", err)
}
Expand All @@ -86,7 +83,7 @@ func SetupReset() {

// Location returns the location of the cache.
func Location() string {
p := filepath.Join(gpath.Home(), "cache")
p := filepath.Join(xdgCacheDir(), "glide")
Setup()

return p
Expand Down
35 changes: 35 additions & 0 deletions cache/xdg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Borrowed from golang.org/x/build/cmd/fetchlogs/xdg.go
//
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license.

package cache

import (
"os"
"os/user"
"path/filepath"
"runtime"
)

// xdgCacheDir returns the XDG Base Directory Specification cache
// directory.
func xdgCacheDir() string {
cache := os.Getenv("XDG_CACHE_HOME")
if cache == "" {
home := os.Getenv("HOME")
if home == "" {
u, err := user.Current()
if err != nil {
home = u.HomeDir
}
}
// Not XDG but standard for OS X.
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library/Caches")
}
cache = filepath.Join(home, ".cache")
}
return cache
}