Skip to content
Closed
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
52 changes: 28 additions & 24 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,17 @@ func mapFromString(str string) (*yamlmap.Map, error) {

// ConfigDir returns the path to the configuration directory.
//
// Config path precedence: GH_CONFIG_DIR, XDG_CONFIG_HOME, AppData (windows only), HOME.
// Config path precedence: GH_CONFIG_DIR, then the platform's standard user
// config directory.
func ConfigDir() string {
var path string
if a := os.Getenv(ghConfigDir); a != "" {
path = a
} else if b := os.Getenv(xdgConfigHome); b != "" {
path = filepath.Join(b, "gh")
} else if c := os.Getenv(appData); runtime.GOOS == "windows" && c != "" {
path = filepath.Join(c, "GitHub CLI")
} else {
d, _ := os.UserHomeDir()
path = filepath.Join(d, ".config", "gh")
return a
}
return path
base, err := os.UserConfigDir()
if err != nil {
return filepath.Join(".config", "gh")
}
return filepath.Join(base, configSubdir())
}

// StateDir returns the path to the state directory.
Expand Down Expand Up @@ -296,21 +293,28 @@ func DataDir() string {

// CacheDir returns the path to the cache directory.
//
// Cache path precedence: XDG_CACHE_HOME, LocalAppData (windows only), HOME, legacy gh-cli-cache.
// Cache path precedence: the platform's standard user cache directory, then
// the legacy temporary directory when the standard directory is unavailable.
func CacheDir() string {
if a := os.Getenv(xdgCacheHome); a != "" {
return filepath.Join(a, "gh")
} else if b := os.Getenv(localAppData); runtime.GOOS == "windows" && b != "" {
return filepath.Join(b, "GitHub CLI")
} else if c, err := os.UserHomeDir(); err == nil {
return filepath.Join(c, ".cache", "gh")
} else {
// Note that this has a minor security issue because /tmp is world-writeable.
// As such, it is possible for other users on a shared system to overwrite cached data.
// The practical risk of this is low, but it's worth calling out as a risk.
// I've included this here for backwards compatibility but we should consider removing it.
return filepath.Join(os.TempDir(), "gh-cli-cache")
if base, err := os.UserCacheDir(); err == nil {
return filepath.Join(base, cacheSubdir())
}
// Note that this has a minor security issue because /tmp is world-writeable.
// As such, it is possible for other users on a shared system to overwrite cached data.
// The practical risk of this is low, but it's worth calling out as a risk.
// I've included this here for backwards compatibility but we should consider removing it.
return filepath.Join(os.TempDir(), "gh-cli-cache")
}

func configSubdir() string {
if runtime.GOOS == "windows" {
return "GitHub CLI"
}
return "gh"
}

func cacheSubdir() string {
return configSubdir()
}

func readFile(filename string) ([]byte, error) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ func TestConfigDir(t *testing.T) {
t.Setenv(k, v)
}
}
assert.Equal(t, tt.output, ConfigDir())
expected := tt.output
if tt.name == "HOME/USERPROFILE specified" {
base, err := os.UserConfigDir()
require.NoError(t, err)
expected = filepath.Join(base, configSubdir())
}
assert.Equal(t, expected, ConfigDir())
})
}
}
Expand Down Expand Up @@ -284,7 +290,13 @@ func TestCacheDir(t *testing.T) {
t.Setenv(k, v)
}
}
assert.Equal(t, tt.output, CacheDir())
expected := tt.output
if tt.name == "tries to use the home dir cache directory" {
base, err := os.UserCacheDir()
require.NoError(t, err)
expected = filepath.Join(base, cacheSubdir())
}
assert.Equal(t, expected, CacheDir())
})
}

Expand Down