mirror of
https://github.com/spf13/viper
synced 2024-12-23 20:17:03 +00:00
Fixed environment variable handling on Windows (fixes #282)
This commit is contained in:
parent
3968772673
commit
4257721a8b
1 changed files with 23 additions and 17 deletions
40
util.go
40
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)
|
||||
|
|
Loading…
Reference in a new issue