From 4257721a8b72a215e17cbcf1344b1f50d69abc87 Mon Sep 17 00:00:00 2001 From: Julien Kauffmann Date: Fri, 2 Dec 2016 14:11:59 -0500 Subject: [PATCH] Fixed environment variable handling on Windows (fixes #282) --- util.go | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/util.go b/util.go index 3ebada9..d4bc11b 100644 --- a/util.go +++ b/util.go @@ -94,16 +94,33 @@ func insensitiviseMap(m map[string]interface{}) { } } +func getEnv(key string) string { + if key == "HOME" && runtime.GOOS == "windows" { + home := os.Getenv(key) + + if home != "" { + return home + } + + home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + + if home != "" { + return home + } + + return os.Getenv("USERPROFILE") + } + + return os.Getenv(key) +} + func absPathify(inPath string) string { jww.INFO.Println("Trying to resolve absolute path to", inPath) - if strings.HasPrefix(inPath, "$HOME") { - inPath = userHomeDir() + inPath[5:] - } + inPath = filepath.FromSlash(inPath) - if strings.HasPrefix(inPath, "$") { - end := strings.Index(inPath, string(os.PathSeparator)) - inPath = os.Getenv(inPath[1:end]) + inPath[end:] + if strings.ContainsRune(inPath, '$') { + inPath = os.Expand(inPath, getEnv) } if filepath.IsAbs(inPath) { @@ -141,17 +158,6 @@ func stringInSlice(a string, list []string) bool { return false } -func userHomeDir() string { - if runtime.GOOS == "windows" { - home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") - if home == "" { - home = os.Getenv("USERPROFILE") - } - return home - } - return os.Getenv("HOME") -} - func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error { buf := new(bytes.Buffer) buf.ReadFrom(in)