Fixed environment variable handling on Windows (fixes #282)

This commit is contained in:
Julien Kauffmann 2016-12-02 14:11:59 -05:00
parent 3968772673
commit 4257721a8b
1 changed files with 23 additions and 17 deletions

40
util.go
View File

@ -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 { func absPathify(inPath string) string {
jww.INFO.Println("Trying to resolve absolute path to", inPath) jww.INFO.Println("Trying to resolve absolute path to", inPath)
if strings.HasPrefix(inPath, "$HOME") { inPath = filepath.FromSlash(inPath)
inPath = userHomeDir() + inPath[5:]
}
if strings.HasPrefix(inPath, "$") { if strings.ContainsRune(inPath, '$') {
end := strings.Index(inPath, string(os.PathSeparator)) inPath = os.Expand(inPath, getEnv)
inPath = os.Getenv(inPath[1:end]) + inPath[end:]
} }
if filepath.IsAbs(inPath) { if filepath.IsAbs(inPath) {
@ -141,17 +158,6 @@ func stringInSlice(a string, list []string) bool {
return false 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 { func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(in) buf.ReadFrom(in)