Merge branch 'master' of https://github.com/biehlerj/cointop into biehlerj-master

pull/61/head
Miguel Mota 4 years ago
commit 32b0d06448

@ -3,32 +3,33 @@ package pathutil
import (
"os"
"path/filepath"
"runtime"
"strings"
)
// UserPreferredHomeDir returns the preferred home directory for the user
func UserPreferredHomeDir() string {
var home string
func UserPreferredHomeDir() (string, bool) {
var isConfigDir bool
if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
} else if runtime.GOOS == "linux" {
home = os.Getenv("XDG_CONFIG_HOME")
}
home, _ := os.UserConfigDir()
isConfigDir = true
if home == "" {
home, _ = os.UserHomeDir()
isConfigDir = false
}
return home
return home, isConfigDir
}
// NormalizePath normalizes and extends the path string
func NormalizePath(path string) string {
// expand tilde
if strings.HasPrefix(path, "~/") {
path = filepath.Join(UserPreferredHomeDir(), path[2:])
home, isConfigDir := UserPreferredHomeDir()
if !isConfigDir {
path = filepath.Join(home, path[2:])
}
path = filepath.Join(home, path[10:])
}
path = strings.Replace(path, "/", string(filepath.Separator), -1)

@ -0,0 +1,25 @@
package pathutil
import (
"os"
"path/filepath"
"testing"
)
// 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(configDir, "/cointop/config.toml")},
{"~/.config/cointop/config.toml", filepath.Join(home, ".config/cointop/config.toml")},
}
for _, c := range cases {
got := NormalizePath(c.in)
if got != c.want {
t.Errorf("NormalizePath(%q) == %q, want %q", c.in, got, c.want)
}
}
}
Loading…
Cancel
Save