mirror of https://github.com/spf13/viper.git
Handle $HOME and other environment variables in paths
This commit is contained in:
parent
541c1f8c59
commit
7f5b583ff1
22
viper.go
22
viper.go
|
@ -14,6 +14,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -368,8 +369,29 @@ 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 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 = userHomeDir() + inPath[5:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(inPath, "$") {
|
||||||
|
end := strings.Index(inPath, string(os.PathSeparator))
|
||||||
|
inPath = os.Getenv(inPath[1:end]) + inPath[end:]
|
||||||
|
}
|
||||||
|
|
||||||
if filepath.IsAbs(inPath) {
|
if filepath.IsAbs(inPath) {
|
||||||
return filepath.Clean(inPath)
|
return filepath.Clean(inPath)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue