From fa137328f661c0f680980a7aaf3463ffb67c5361 Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Sat, 1 Aug 2015 20:32:35 -0500 Subject: [PATCH] Fixed #68 --- util.go | 21 ++++++++++++++++----- viper.go | 22 +++++++++++----------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/util.go b/util.go index 7904b1a..a605e62 100644 --- a/util.go +++ b/util.go @@ -28,6 +28,16 @@ import ( "gopkg.in/yaml.v2" ) +// Denotes failing to parse configuration file. +type ConfigParseError struct { + err error +} + +// Returns the formatted configuration error. +func (pe ConfigParseError) Error() string { + return fmt.Sprintf("While parsing config: %s", pe.err.Error()) +} + func insensitiviseMap(m map[string]interface{}) { for key, val := range m { lower := strings.ToLower(key) @@ -119,31 +129,31 @@ func findCWD() (string, error) { 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.ReadFrom(in) switch strings.ToLower(configType) { case "yaml", "yml": if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil { - jww.ERROR.Fatalf("Error parsing config: %s", err) + return ConfigParseError{err} } case "json": if err := json.Unmarshal(buf.Bytes(), &c); err != nil { - jww.ERROR.Fatalf("Error parsing config: %s", err) + return ConfigParseError{err} } case "toml": if _, err := toml.Decode(buf.String(), &c); err != nil { - jww.ERROR.Fatalf("Error parsing config: %s", err) + return ConfigParseError{err} } 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) + return ConfigParseError{err} } for _, key := range p.Keys() { value, _ := p.Get(key) @@ -152,6 +162,7 @@ func marshallConfigReader(in io.Reader, c map[string]interface{}, configType str } insensitiviseMap(c) + return nil } func safeMul(a, b uint) uint { diff --git a/viper.go b/viper.go index f2c09ce..84878d5 100644 --- a/viper.go +++ b/viper.go @@ -738,22 +738,19 @@ func (v *Viper) ReadInConfig() error { v.config = make(map[string]interface{}) - v.marshalReader(bytes.NewReader(file), v.config) - return nil + return v.marshalReader(bytes.NewReader(file), v.config) } func ReadConfig(in io.Reader) error { return v.ReadConfig(in) } func (v *Viper) ReadConfig(in io.Reader) error { v.config = make(map[string]interface{}) - v.marshalReader(in, v.config) - return nil + return v.marshalReader(in, v.config) } // func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) } // func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error { // v.config = make(map[string]interface{}) -// v.marshalReader(buf, v.config) -// return nil +// return v.marshalReader(buf, v.config) // } // Attempts to get configuration from a remote source @@ -778,9 +775,12 @@ func (v *Viper) WatchRemoteConfig() error { // Marshall a Reader into a map // Should probably be an unexported function -func marshalReader(in io.Reader, c map[string]interface{}) { v.marshalReader(in, c) } -func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) { - marshallConfigReader(in, c, v.getConfigType()) +func marshalReader(in io.Reader, c map[string]interface{}) error { + return v.marshalReader(in, c) +} + +func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) error { + return marshallConfigReader(in, c, v.getConfigType()) } func (v *Viper) insensitiviseMaps() { @@ -813,7 +813,7 @@ func (v *Viper) getRemoteConfig(provider *defaultRemoteProvider) (map[string]int if err != nil { return nil, err } - v.marshalReader(reader, v.kvstore) + err = v.marshalReader(reader, v.kvstore) return v.kvstore, err } @@ -835,7 +835,7 @@ func (v *Viper) watchRemoteConfig(provider *defaultRemoteProvider) (map[string]i if err != nil { return nil, err } - v.marshalReader(reader, v.kvstore) + err = v.marshalReader(reader, v.kvstore) return v.kvstore, err }