From d90f2bb139e1c12fa276ee449cd68e8c6c5c1602 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Tue, 27 Dec 2016 21:49:59 -0600 Subject: [PATCH] Only save config on success in ReadInConfig If the user creates a invalid config file while watching for config changes, the previous, valid config is not retained. This commit only overwrites the running config if unmarshalling was successful. --- viper.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 2603c786d..fce13b1e9 100644 --- a/viper.go +++ b/viper.go @@ -1093,9 +1093,15 @@ func (v *Viper) ReadInConfig() error { return err } - v.config = make(map[string]interface{}) + config := make(map[string]interface{}) + + err = v.unmarshalReader(bytes.NewReader(file), config) + if err != nil { + return err + } - return v.unmarshalReader(bytes.NewReader(file), v.config) + v.config = config + return nil } // MergeInConfig merges a new configuration with an existing config.