2014-12-05 02:55:51 +00:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Viper is a application configuration system.
|
|
|
|
// It believes that applications can be configured a variety of ways
|
|
|
|
// via flags, ENVIRONMENT variables, configuration files retrieved
|
|
|
|
// from the file system, or a remote key/value store.
|
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
2014-12-05 16:04:40 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-12-05 02:55:51 +00:00
|
|
|
"fmt"
|
2014-12-05 16:04:40 +00:00
|
|
|
"io"
|
2014-12-05 02:55:51 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-02-28 21:03:22 +00:00
|
|
|
"unicode"
|
2014-12-05 02:55:51 +00:00
|
|
|
|
2015-12-11 22:51:11 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2015-04-14 18:15:02 +00:00
|
|
|
"github.com/magiconair/properties"
|
2016-08-12 18:14:35 +00:00
|
|
|
toml "github.com/pelletier/go-toml"
|
2015-02-28 21:03:22 +00:00
|
|
|
"github.com/spf13/cast"
|
2014-12-05 02:55:51 +00:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2015-01-26 13:56:55 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-12-05 02:55:51 +00:00
|
|
|
)
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
// ConfigParseError denotes failing to parse configuration file.
|
2015-08-02 01:32:35 +00:00
|
|
|
type ConfigParseError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:17:41 +00:00
|
|
|
// Error returns the formatted configuration error.
|
2015-08-02 01:32:35 +00:00
|
|
|
func (pe ConfigParseError) Error() string {
|
|
|
|
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:49:00 +00:00
|
|
|
// toCaseInsensitiveValue checks if the value is a map;
|
|
|
|
// if so, create a copy and lower-case the keys recursively.
|
|
|
|
func toCaseInsensitiveValue(value interface{}) interface{} {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
value = copyAndInsensitiviseMap(cast.ToStringMap(v))
|
|
|
|
case map[string]interface{}:
|
|
|
|
value = copyAndInsensitiviseMap(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
|
|
|
|
// any map it makes case insensitive.
|
|
|
|
func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
|
|
|
|
nm := make(map[string]interface{})
|
|
|
|
|
|
|
|
for key, val := range m {
|
|
|
|
lkey := strings.ToLower(key)
|
|
|
|
switch v := val.(type) {
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
|
|
|
|
case map[string]interface{}:
|
|
|
|
nm[lkey] = copyAndInsensitiviseMap(v)
|
|
|
|
default:
|
|
|
|
nm[lkey] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
2015-01-22 07:43:42 +00:00
|
|
|
func insensitiviseMap(m map[string]interface{}) {
|
2014-12-05 02:55:51 +00:00
|
|
|
for key, val := range m {
|
2016-10-14 09:24:45 +00:00
|
|
|
switch val.(type) {
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
// nested map: cast and recursively insensitivise
|
|
|
|
val = cast.ToStringMap(val)
|
|
|
|
insensitiviseMap(val.(map[string]interface{}))
|
|
|
|
case map[string]interface{}:
|
|
|
|
// nested map: recursively insensitivise
|
|
|
|
insensitiviseMap(val.(map[string]interface{}))
|
|
|
|
}
|
|
|
|
|
2014-12-05 02:55:51 +00:00
|
|
|
lower := strings.ToLower(key)
|
|
|
|
if key != lower {
|
2016-10-14 09:24:45 +00:00
|
|
|
// remove old key (not lower-cased)
|
2014-12-05 02:55:51 +00:00
|
|
|
delete(m, key)
|
|
|
|
}
|
2016-10-14 09:24:45 +00:00
|
|
|
// update map
|
|
|
|
m[lower] = val
|
2014-12-05 02:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func absPathify(inPath string) string {
|
|
|
|
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) {
|
|
|
|
return filepath.Clean(inPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := filepath.Abs(inPath)
|
|
|
|
if err == nil {
|
|
|
|
return filepath.Clean(p)
|
|
|
|
}
|
2016-09-20 08:17:41 +00:00
|
|
|
|
|
|
|
jww.ERROR.Println("Couldn't discover absolute path")
|
|
|
|
jww.ERROR.Println(err)
|
2014-12-05 02:55:51 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if File / Directory Exists
|
|
|
|
func exists(path string) (bool, error) {
|
2016-08-05 07:45:58 +00:00
|
|
|
_, err := v.fs.Stat(path)
|
2014-12-05 02:55:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-08-24 03:40:56 +00:00
|
|
|
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
2014-12-05 16:04:40 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(in)
|
|
|
|
|
2015-05-11 04:33:35 +00:00
|
|
|
switch strings.ToLower(configType) {
|
2014-12-05 16:04:40 +00:00
|
|
|
case "yaml", "yml":
|
|
|
|
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
|
2015-08-02 01:32:35 +00:00
|
|
|
return ConfigParseError{err}
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "json":
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
2015-08-02 01:32:35 +00:00
|
|
|
return ConfigParseError{err}
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 22:51:11 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:04:40 +00:00
|
|
|
case "toml":
|
2016-08-12 18:14:35 +00:00
|
|
|
tree, err := toml.LoadReader(buf)
|
|
|
|
if err != nil {
|
2015-08-02 01:32:35 +00:00
|
|
|
return ConfigParseError{err}
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
2016-08-12 18:14:35 +00:00
|
|
|
tmap := tree.ToMap()
|
|
|
|
for k, v := range tmap {
|
|
|
|
c[k] = v
|
|
|
|
}
|
2015-04-14 18:15:02 +00:00
|
|
|
|
|
|
|
case "properties", "props", "prop":
|
|
|
|
var p *properties.Properties
|
|
|
|
var err error
|
|
|
|
if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
|
2015-08-02 01:32:35 +00:00
|
|
|
return ConfigParseError{err}
|
2015-04-14 18:15:02 +00:00
|
|
|
}
|
|
|
|
for _, key := range p.Keys() {
|
|
|
|
value, _ := p.Get(key)
|
2016-10-08 08:00:18 +00:00
|
|
|
// 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
|
2015-04-14 18:15:02 +00:00
|
|
|
}
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 07:43:42 +00:00
|
|
|
insensitiviseMap(c)
|
2015-08-02 01:32:35 +00:00
|
|
|
return nil
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
2015-02-28 21:03:22 +00:00
|
|
|
|
|
|
|
func safeMul(a, b uint) uint {
|
|
|
|
c := a * b
|
|
|
|
if a > 1 && b > 1 && c/b != a {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
|
|
|
|
func parseSizeInBytes(sizeStr string) uint {
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr)
|
|
|
|
lastChar := len(sizeStr) - 1
|
|
|
|
multiplier := uint(1)
|
|
|
|
|
|
|
|
if lastChar > 0 {
|
|
|
|
if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
|
|
|
|
if lastChar > 1 {
|
|
|
|
switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
|
|
|
|
case 'k':
|
|
|
|
multiplier = 1 << 10
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
case 'm':
|
|
|
|
multiplier = 1 << 20
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
case 'g':
|
|
|
|
multiplier = 1 << 30
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
|
|
|
|
default:
|
|
|
|
multiplier = 1
|
|
|
|
sizeStr = strings.TrimSpace(sizeStr[:lastChar])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size := cast.ToInt(sizeStr)
|
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return safeMul(uint(size), multiplier)
|
|
|
|
}
|
2016-10-08 08:00:18 +00:00
|
|
|
|
|
|
|
// deepSearch scans deep maps, following the key indexes listed in the
|
|
|
|
// sequence "path".
|
|
|
|
// The last value is expected to be another map, and is returned.
|
|
|
|
//
|
|
|
|
// In case intermediate keys do not exist, or map to a non-map value,
|
|
|
|
// a new map is created and inserted, and the search continues from there:
|
|
|
|
// the initial map "m" may be modified!
|
|
|
|
func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
|
|
|
|
for _, k := range path {
|
|
|
|
m2, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
// intermediate key does not exist
|
|
|
|
// => create it and continue from there
|
|
|
|
m3 := make(map[string]interface{})
|
|
|
|
m[k] = m3
|
|
|
|
m = m3
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m3, ok := m2.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
// intermediate key is a value
|
|
|
|
// => replace with a new map
|
|
|
|
m3 = make(map[string]interface{})
|
|
|
|
m[k] = m3
|
|
|
|
}
|
|
|
|
// continue search from here
|
|
|
|
m = m3
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|