This commit is contained in:
Vlad Didenko 2015-06-09 02:46:08 -05:00
parent 1241d2e04b
commit ad82755e41
2 changed files with 27 additions and 16 deletions

21
util.go
View file

@ -28,6 +28,16 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// Denotes failing to parse configuration file.
type ConfigFileParseError struct {
err error
}
// Returns the formatted configuration error.
func (pe ConfigFileParseError) Error() string {
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
}
func insensitiviseMap(m map[string]interface{}) { func insensitiviseMap(m map[string]interface{}) {
for key, val := range m { for key, val := range m {
lower := strings.ToLower(key) lower := strings.ToLower(key)
@ -119,31 +129,31 @@ func findCWD() (string, error) {
return path, nil return path, nil
} }
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) { func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(in) buf.ReadFrom(in)
switch strings.ToLower(configType) { switch strings.ToLower(configType) {
case "yaml", "yml": case "yaml", "yml":
if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err) return ConfigFileParseError{err}
} }
case "json": case "json":
if err := json.Unmarshal(buf.Bytes(), &c); err != nil { if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err) return ConfigFileParseError{err}
} }
case "toml": case "toml":
if _, err := toml.Decode(buf.String(), &c); err != nil { if _, err := toml.Decode(buf.String(), &c); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err) return ConfigFileParseError{err}
} }
case "properties", "props", "prop": case "properties", "props", "prop":
var p *properties.Properties var p *properties.Properties
var err error var err error
if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil { if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err) return ConfigFileParseError{err}
} }
for _, key := range p.Keys() { for _, key := range p.Keys() {
value, _ := p.Get(key) value, _ := p.Get(key)
@ -152,6 +162,7 @@ func marshallConfigReader(in io.Reader, c map[string]interface{}, configType str
} }
insensitiviseMap(c) insensitiviseMap(c)
return nil
} }
func safeMul(a, b uint) uint { func safeMul(a, b uint) uint {

View file

@ -745,22 +745,19 @@ func (v *Viper) ReadInConfig() error {
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.marshalReader(bytes.NewReader(file), v.config) return v.marshalReader(bytes.NewReader(file), v.config)
return nil
} }
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) } func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
func (v *Viper) ReadConfig(in io.Reader) error { func (v *Viper) ReadConfig(in io.Reader) error {
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.marshalReader(in, v.config) return v.marshalReader(in, v.config)
return nil
} }
// func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) } // func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) }
// func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error { // func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error {
// v.config = make(map[string]interface{}) // v.config = make(map[string]interface{})
// v.marshalReader(buf, v.config) // return v.marshalReader(buf, v.config)
// return nil
// } // }
// Attempts to get configuration from a remote source // Attempts to get configuration from a remote source
@ -785,9 +782,12 @@ func (v *Viper) WatchRemoteConfig() error {
// Marshall a Reader into a map // Marshall a Reader into a map
// Should probably be an unexported function // Should probably be an unexported function
func marshalReader(in io.Reader, c map[string]interface{}) { v.marshalReader(in, c) } func marshalReader(in io.Reader, c map[string]interface{}) error {
func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) { return v.marshalReader(in, c)
marshallConfigReader(in, c, v.getConfigType()) }
func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) error {
return marshallConfigReader(in, c, v.getConfigType())
} }
func (v *Viper) insensitiviseMaps() { func (v *Viper) insensitiviseMaps() {
@ -820,7 +820,7 @@ func (v *Viper) getRemoteConfig(provider *defaultRemoteProvider) (map[string]int
if err != nil { if err != nil {
return nil, err return nil, err
} }
v.marshalReader(reader, v.kvstore) err = v.marshalReader(reader, v.kvstore)
return v.kvstore, err return v.kvstore, err
} }
@ -842,7 +842,7 @@ func (v *Viper) watchRemoteConfig(provider *defaultRemoteProvider) (map[string]i
if err != nil { if err != nil {
return nil, err return nil, err
} }
v.marshalReader(reader, v.kvstore) err = v.marshalReader(reader, v.kvstore)
return v.kvstore, err return v.kvstore, err
} }