Skip to content

Commit

Permalink
fix: config loading order (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3rn31 committed Aug 9, 2022
1 parent 13315ed commit 8df0e90
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
7 changes: 6 additions & 1 deletion configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,29 @@ func arePathsNotSet(cPaths []string) bool {
func defaultConfigPaths() []string {
result := make([]string, 0, 4)

result = append(result, ".")
// First global config
if runtime.GOOS != windowsOs {
result = append(result, "/etc/gremlins")
}

// Then $XDG_CONFIG_HOME
xchLocation, _ := homedir.Expand("~/.config")
if x := os.Getenv(xdgConfigHomeKey); x != "" {
xchLocation = x
}
xchLocation = filepath.Join(xchLocation, "gremlins", "gremlins")
result = append(result, xchLocation)

// Then $HOME
homeLocation, err := homedir.Expand("~/.gremlins")
if err != nil {
return result
}
result = append(result, homeLocation)

// Finally the current directory
result = append(result, ".")

return result
}

Expand Down
39 changes: 35 additions & 4 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package configuration

import (
"os"
"path/filepath"
"runtime"
"testing"
Expand Down Expand Up @@ -130,12 +131,27 @@ func TestConfigPaths(t *testing.T) {
home, _ := homedir.Dir()

t.Run("it lookups in default locations", func(t *testing.T) {
oldDir, _ := os.Getwd()
_ = os.Chdir("testdata/config1")
defer func(dir string) {
_ = os.Chdir(dir)
}(oldDir)

var want []string
want = append(want, ".")

// First global
if runtime.GOOS != "windows" {
want = append(want, "/etc/gremlins")
}
want = append(want, filepath.Join(home, ".config", "gremlins", "gremlins"), filepath.Join(home, ".gremlins"))

// Then $XDG_CONFIG_HOME and $HOME
want = append(want,
filepath.Join(home, ".config", "gremlins", "gremlins"),
filepath.Join(home, ".gremlins"),
)

// Last current folder
want = append(want, ".")

got := defaultConfigPaths()

Expand All @@ -145,14 +161,29 @@ func TestConfigPaths(t *testing.T) {
})

t.Run("when XDG_CONFIG_HOME is set, it lookups in that locations", func(t *testing.T) {
oldDir, _ := os.Getwd()
_ = os.Chdir("testdata/config1")
defer func(dir string) {
_ = os.Chdir(dir)
}(oldDir)

customPath := filepath.Join("my", "custom", "path")
t.Setenv("XDG_CONFIG_HOME", customPath)

var want []string
want = append(want, ".")

// First global
if runtime.GOOS != "windows" {
want = append(want, "/etc/gremlins")
}
want = append(want, filepath.Join(customPath, "gremlins", "gremlins"), filepath.Join(home, ".gremlins"))

// Then $XDG_CONFIG_HOME and $HOME
want = append(want,
filepath.Join(customPath, "gremlins", "gremlins"),
filepath.Join(home, ".gremlins"))

// Last the current directory
want = append(want, ".")

got := defaultConfigPaths()

Expand Down

0 comments on commit 8df0e90

Please sign in to comment.