X Tutup
Skip to content

Commit e6ad8a1

Browse files
committed
Add support for XDG_DATA_HOME
1 parent 548a91f commit e6ad8a1

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

internal/config/config_file.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
GH_CONFIG_DIR = "GH_CONFIG_DIR"
1818
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
1919
XDG_STATE_HOME = "XDG_STATE_HOME"
20+
XDG_DATA_HOME = "XDG_DATA_HOME"
2021
APP_DATA = "AppData"
2122
LOCAL_APP_DATA = "LocalAppData"
2223
)
@@ -70,6 +71,24 @@ func StateDir() string {
7071
return path
7172
}
7273

74+
// Data path precedence
75+
// 1. XDG_DATA_HOME
76+
// 2. LocalAppData (windows only)
77+
// 3. HOME
78+
func DataDir() string {
79+
var path string
80+
if a := os.Getenv(XDG_DATA_HOME); a != "" {
81+
path = filepath.Join(a, "gh")
82+
} else if b := os.Getenv(LOCAL_APP_DATA); runtime.GOOS == "windows" && b != "" {
83+
path = filepath.Join(b, "GitHub CLI")
84+
} else {
85+
c, _ := os.UserHomeDir()
86+
path = filepath.Join(c, ".local", "share", "gh")
87+
}
88+
89+
return path
90+
}
91+
7392
var errSamePath = errors.New("same path")
7493
var errNotExist = errors.New("not exist")
7594

internal/config/config_file_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,68 @@ func Test_autoMigrateStateDir_migration(t *testing.T) {
484484
assert.Equal(t, 1, len(files))
485485
assert.Equal(t, "state.yml", files[0].Name())
486486
}
487+
488+
func Test_DataDir(t *testing.T) {
489+
tempDir := t.TempDir()
490+
491+
tests := []struct {
492+
name string
493+
onlyWindows bool
494+
env map[string]string
495+
output string
496+
}{
497+
{
498+
name: "HOME/USERPROFILE specified",
499+
env: map[string]string{
500+
"XDG_DATA_HOME": "",
501+
"GH_CONFIG_DIR": "",
502+
"XDG_CONFIG_HOME": "",
503+
"LocalAppData": "",
504+
"USERPROFILE": tempDir,
505+
"HOME": tempDir,
506+
},
507+
output: filepath.Join(tempDir, ".local", "share", "gh"),
508+
},
509+
{
510+
name: "XDG_DATA_HOME specified",
511+
env: map[string]string{
512+
"XDG_DATA_HOME": tempDir,
513+
},
514+
output: filepath.Join(tempDir, "gh"),
515+
},
516+
{
517+
name: "LocalAppData specified",
518+
onlyWindows: true,
519+
env: map[string]string{
520+
"LocalAppData": tempDir,
521+
},
522+
output: filepath.Join(tempDir, "GitHub CLI"),
523+
},
524+
{
525+
name: "XDG_DATA_HOME and LocalAppData specified",
526+
onlyWindows: true,
527+
env: map[string]string{
528+
"XDG_DATA_HOME": tempDir,
529+
"LocalAppData": tempDir,
530+
},
531+
output: filepath.Join(tempDir, "gh"),
532+
},
533+
}
534+
535+
for _, tt := range tests {
536+
if tt.onlyWindows && runtime.GOOS != "windows" {
537+
continue
538+
}
539+
t.Run(tt.name, func(t *testing.T) {
540+
if tt.env != nil {
541+
for k, v := range tt.env {
542+
old := os.Getenv(k)
543+
os.Setenv(k, v)
544+
defer os.Setenv(k, old)
545+
}
546+
}
547+
548+
assert.Equal(t, tt.output, DataDir())
549+
})
550+
}
551+
}

0 commit comments

Comments
 (0)
X Tutup