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
|
|
|
|
2014-12-05 16:04:40 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-04-14 18:15:02 +00:00
|
|
|
"github.com/magiconair/properties"
|
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
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
lower := strings.ToLower(key)
|
|
|
|
if key != lower {
|
|
|
|
delete(m, key)
|
|
|
|
m[lower] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
jww.ERROR.Println("Couldn't discover absolute path")
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if File / Directory Exists
|
|
|
|
func exists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func findCWD() (string, error) {
|
|
|
|
serverFile, err := filepath.Abs(os.Args[0])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Can't get absolute path for executable: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Dir(serverFile)
|
|
|
|
realFile, err := filepath.EvalSymlinks(serverFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if _, err = os.Stat(serverFile + ".exe"); err == nil {
|
|
|
|
realFile = filepath.Clean(serverFile + ".exe")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && realFile != serverFile {
|
|
|
|
path = filepath.Dir(realFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
2014-12-05 16:04:40 +00:00
|
|
|
|
|
|
|
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) {
|
|
|
|
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 {
|
|
|
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "json":
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
|
|
|
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "toml":
|
|
|
|
if _, err := toml.Decode(buf.String(), &c); err != nil {
|
|
|
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
|
|
}
|
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 {
|
|
|
|
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
|
|
|
}
|
|
|
|
for _, key := range p.Keys() {
|
|
|
|
value, _ := p.Get(key)
|
|
|
|
c[key] = value
|
|
|
|
}
|
2014-12-05 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 07:43:42 +00:00
|
|
|
insensitiviseMap(c)
|
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)
|
|
|
|
}
|