mirror of
https://github.com/spf13/viper
synced 2025-05-07 20:57:18 +00:00
Merge 8c544d5674
into 2c12c60302
This commit is contained in:
commit
159de3698a
2 changed files with 87 additions and 8 deletions
84
util.go
84
util.go
|
@ -88,16 +88,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.IndexRune(inPath, '$') >= 0 {
|
||||||
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) {
|
||||||
|
@ -135,6 +152,61 @@ func stringInSlice(a string, list []string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
|
switch strings.ToLower(configType) {
|
||||||
|
case "yaml", "yml":
|
||||||
|
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "json":
|
||||||
|
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "hcl":
|
||||||
|
obj, err := hcl.Parse(string(buf.Bytes()))
|
||||||
|
if err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
if err = hcl.DecodeObject(&c, obj); err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "toml":
|
||||||
|
tree, err := toml.LoadReader(buf)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
tmap := tree.ToMap()
|
||||||
|
for k, v := range tmap {
|
||||||
|
c[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
case "properties", "props", "prop":
|
||||||
|
var p *properties.Properties
|
||||||
|
var err error
|
||||||
|
if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
for _, key := range p.Keys() {
|
||||||
|
value, _ := p.Get(key)
|
||||||
|
// recursively build nested maps
|
||||||
|
path := strings.Split(key, ".")
|
||||||
|
lastKey := strings.ToLower(path[len(path)-1])
|
||||||
|
deepestMap := deepSearch(c, path[0:len(path)-1])
|
||||||
|
// set innermost value
|
||||||
|
deepestMap[lastKey] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
insensitiviseMap(c)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func userHomeDir() string {
|
func userHomeDir() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||||
|
|
|
@ -12,8 +12,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
path "path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -186,10 +187,16 @@ func initHcl() {
|
||||||
func initDirs(t *testing.T) (string, string, func()) {
|
func initDirs(t *testing.T) (string, string, func()) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testDirs = []string{`a a`, `b`, `c\c`, `D_`}
|
testDirs []string
|
||||||
config = `improbable`
|
config = `improbable`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
testDirs = []string{`a a`, `b`, `D_`}
|
||||||
|
} else {
|
||||||
|
testDirs = []string{`a a`, `b`, `c\c`, `D_`}
|
||||||
|
}
|
||||||
|
|
||||||
root, err := ioutil.TempDir("", "")
|
root, err := ioutil.TempDir("", "")
|
||||||
|
|
||||||
cleanup := true
|
cleanup := true
|
||||||
|
|
Loading…
Add table
Reference in a new issue