Skip to content

Commit 1e99c68

Browse files
committed
Merge pull request #178 from lcowell/xdg_config
Allow $XDG_CONFIG_HOME to configure config location
2 parents f9848a5 + a13f9ef commit 1e99c68

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ func (c *Config) resolvePath(argPath string) (string, error) {
167167
if err != nil {
168168
return "", err
169169
}
170-
return strings.Replace(path, "~", h, 1), nil
170+
path = strings.Replace(path, "~", h, 1)
171+
172+
fi, _ := os.Stat(path)
173+
if fi != nil && fi.IsDir() {
174+
path = filepath.Join(path, File)
175+
}
176+
177+
return path, nil
171178
}
172179

173180
func (c *Config) setDefaults() error {

config/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ func TestLoad(t *testing.T) {
9292
}
9393
}
9494

95+
func TestReadDirectory(t *testing.T) {
96+
// if the provided path is a directory, append the default filename
97+
tmpDir, err := ioutil.TempDir("", "")
98+
assert.NoError(t, err)
99+
100+
myConfig, err := New(tmpDir)
101+
assert.NoError(t, err)
102+
103+
expected := filepath.Join(tmpDir, File)
104+
actual := myConfig.File
105+
assert.Equal(t, expected, actual)
106+
107+
// if it can't determine if the provided path is a directory, don't modify
108+
// the path
109+
myConfig, err = New("badpath")
110+
assert.NoError(t, err)
111+
assert.Equal(t, "badpath", myConfig.File)
112+
}
113+
95114
func TestReadingWritingConfig(t *testing.T) {
96115
tmpDir, err := ioutil.TempDir("", "")
97116
filename := fmt.Sprintf("%s/%s", tmpDir, File)

exercism/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
cli.StringFlag{
4747
Name: "config, c",
4848
Usage: "path to config file",
49-
EnvVar: "EXERCISM_CONFIG_FILE",
49+
EnvVar: "XDG_CONFIG_HOME,EXERCISM_CONFIG_FILE",
5050
},
5151
cli.BoolFlag{
5252
Name: "verbose, v",

0 commit comments

Comments
 (0)