Skip to content

Commit df73fa2

Browse files
committed
Merge pull request #215 from lcowell/master
use $XDG_CONFIG_HOME if available
2 parents f58dd42 + cbf2c08 commit df73fa2

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

paths/paths.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ const (
1919

2020
var (
2121
// Home by default will contact the location of your home directory
22-
Home string
22+
Home string
23+
24+
// XdgConfigHome will contain $XDG_CONFIG_HOME if it exists
25+
XdgConfigHome string
2326
errHomeNotFound = errors.New("unable to locate home directory")
2427
)
2528

2629
func init() {
27-
// on startup set default values for Home and Config
30+
// on startup set default values
2831
Recalculate()
2932
}
3033

@@ -35,7 +38,11 @@ func init() {
3538
// * the config file appended if we know the target is a directory
3639
func Config(path string) string {
3740
if path == "" {
38-
return filepath.Join(Home, File)
41+
if XdgConfigHome == "" {
42+
return filepath.Join(Home, File)
43+
}
44+
45+
return filepath.Join(XdgConfigHome, File)
3946
}
4047

4148
expandedPath := expandPath(path)
@@ -66,6 +73,7 @@ func Recalculate() {
6673
}
6774
Home = home
6875
}
76+
XdgConfigHome = os.Getenv("XDG_CONFIG_HOME")
6977
}
7078

7179
// IsDir tells us if the path is valid and is a directory

paths/paths_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ func TestConfig(t *testing.T) {
7474
assert.Equal(t, tc.expectedPath, actual, tc.desc)
7575
}
7676
}
77+
78+
func TestXdgConfig(t *testing.T) {
79+
XdgConfigHome = "/home/user/.xdg_config"
80+
81+
assert.Equal(t, filepath.Join(XdgConfigHome, File), Config(""))
82+
83+
}

0 commit comments

Comments
 (0)