1 // Copyright © 2014 Steve Francia <spf@spf13.com>.
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
6 // Viper is a application configuration system.
7 // It believes that applications can be configured a variety of ways
8 // via flags, ENVIRONMENT variables, configuration files retrieved
9 // from the file system, or a remote key/value store.
24 "github.com/hashicorp/hcl"
25 "github.com/magiconair/properties"
26 toml "github.com/pelletier/go-toml"
27 "github.com/spf13/cast"
28 jww "github.com/spf13/jwalterweatherman"
32 // ConfigParseError denotes failing to parse configuration file.
33 type ConfigParseError struct {
37 // Error returns the formatted configuration error.
38 func (pe ConfigParseError) Error() string {
39 return fmt.Sprintf("While parsing config: %s", pe.err.Error())
42 // toCaseInsensitiveValue checks if the value is a map;
43 // if so, create a copy and lower-case the keys recursively.
44 func toCaseInsensitiveValue(value interface{}) interface{} {
45 switch v := value.(type) {
46 case map[interface{}]interface{}:
47 value = copyAndInsensitiviseMap(cast.ToStringMap(v))
48 case map[string]interface{}:
49 value = copyAndInsensitiviseMap(v)
55 // copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
56 // any map it makes case insensitive.
57 func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
58 nm := make(map[string]interface{})
60 for key, val := range m {
61 lkey := strings.ToLower(key)
62 switch v := val.(type) {
63 case map[interface{}]interface{}:
64 nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
65 case map[string]interface{}:
66 nm[lkey] = copyAndInsensitiviseMap(v)
75 func insensitiviseMap(m map[string]interface{}) {
76 for key, val := range m {
78 case map[interface{}]interface{}:
79 // nested map: cast and recursively insensitivise
80 val = cast.ToStringMap(val)
81 insensitiviseMap(val.(map[string]interface{}))
82 case map[string]interface{}:
83 // nested map: recursively insensitivise
84 insensitiviseMap(val.(map[string]interface{}))
87 lower := strings.ToLower(key)
89 // remove old key (not lower-cased)
97 func absPathify(inPath string) string {
98 jww.INFO.Println("Trying to resolve absolute path to", inPath)
100 if strings.HasPrefix(inPath, "$HOME") {
101 inPath = userHomeDir() + inPath[5:]
104 if strings.HasPrefix(inPath, "$") {
105 end := strings.Index(inPath, string(os.PathSeparator))
106 inPath = os.Getenv(inPath[1:end]) + inPath[end:]
109 if filepath.IsAbs(inPath) {
110 return filepath.Clean(inPath)
113 p, err := filepath.Abs(inPath)
115 return filepath.Clean(p)
118 jww.ERROR.Println("Couldn't discover absolute path")
119 jww.ERROR.Println(err)
123 // Check if File / Directory Exists
124 func exists(path string) (bool, error) {
125 _, err := v.fs.Stat(path)
129 if os.IsNotExist(err) {
135 func stringInSlice(a string, list []string) bool {
136 for _, b := range list {
144 func userHomeDir() string {
145 if runtime.GOOS == "windows" {
146 home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
148 home = os.Getenv("USERPROFILE")
152 return os.Getenv("HOME")
155 func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
156 buf := new(bytes.Buffer)
159 switch strings.ToLower(configType) {
161 if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
162 return ConfigParseError{err}
166 if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
167 return ConfigParseError{err}
171 obj, err := hcl.Parse(string(buf.Bytes()))
173 return ConfigParseError{err}
175 if err = hcl.DecodeObject(&c, obj); err != nil {
176 return ConfigParseError{err}
180 tree, err := toml.LoadReader(buf)
182 return ConfigParseError{err}
185 for k, v := range tmap {
189 case "properties", "props", "prop":
190 var p *properties.Properties
192 if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
193 return ConfigParseError{err}
195 for _, key := range p.Keys() {
196 value, _ := p.Get(key)
197 // recursively build nested maps
198 path := strings.Split(key, ".")
199 lastKey := strings.ToLower(path[len(path)-1])
200 deepestMap := deepSearch(c, path[0:len(path)-1])
201 // set innermost value
202 deepestMap[lastKey] = value
210 func safeMul(a, b uint) uint {
212 if a > 1 && b > 1 && c/b != a {
218 // parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
219 func parseSizeInBytes(sizeStr string) uint {
220 sizeStr = strings.TrimSpace(sizeStr)
221 lastChar := len(sizeStr) - 1
222 multiplier := uint(1)
225 if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
227 switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
230 sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
233 sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
236 sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
239 sizeStr = strings.TrimSpace(sizeStr[:lastChar])
245 size := cast.ToInt(sizeStr)
250 return safeMul(uint(size), multiplier)
253 // deepSearch scans deep maps, following the key indexes listed in the
255 // The last value is expected to be another map, and is returned.
257 // In case intermediate keys do not exist, or map to a non-map value,
258 // a new map is created and inserted, and the search continues from there:
259 // the initial map "m" may be modified!
260 func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
261 for _, k := range path {
264 // intermediate key does not exist
265 // => create it and continue from there
266 m3 := make(map[string]interface{})
271 m3, ok := m2.(map[string]interface{})
273 // intermediate key is a value
274 // => replace with a new map
275 m3 = make(map[string]interface{})
278 // continue search from here