diff --git a/cointop/common/pathutil/pathutil.go b/cointop/common/pathutil/pathutil.go index 58bb4e3..6761b28 100644 --- a/cointop/common/pathutil/pathutil.go +++ b/cointop/common/pathutil/pathutil.go @@ -3,22 +3,15 @@ package pathutil import ( "os" "path/filepath" - "runtime" "strings" ) // UserPreferredHomeDir returns the preferred home directory for the user func UserPreferredHomeDir() (string, bool) { - var home string var isConfigDir bool - if runtime.GOOS == "windows" { - home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") - isConfigDir = false - } else if runtime.GOOS == "linux" { - home = os.Getenv("XDG_CONFIG_HOME") - isConfigDir = true - } + home, _ := os.UserConfigDir() + isConfigDir = true if home == "" { home, _ = os.UserHomeDir() diff --git a/cointop/common/pathutil/pathutil_test.go b/cointop/common/pathutil/pathutil_test.go index 8f13463..954ac0b 100644 --- a/cointop/common/pathutil/pathutil_test.go +++ b/cointop/common/pathutil/pathutil_test.go @@ -8,13 +8,22 @@ import ( // TestNormalizePath checks that NormalizePath returns the correct directory func TestNormalizePath(t *testing.T) { + home, _ := os.UserHomeDir() + configDir, _ := os.UserConfigDir() cases := []struct { in, want string }{ - {"~/.config/cointop/config.toml", filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(configDir, "/cointop/config.toml")}, + {"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")}, } - for _, c := range cases { + for i, c := range cases { got := NormalizePath(c.in) + if i > 1 { + home = "" + configDir = "" + } if got != c.want { t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want) }