Skip to content

Commit

Permalink
Don't overwrite current configuration if failed to parse new one
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 13, 2019
1 parent 49245f4 commit 7fe8380
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions configor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package configor

import (
"fmt"
"os"
"reflect"
"regexp"
"time"
)
Expand Down Expand Up @@ -77,13 +79,24 @@ func (configor *Configor) GetErrorOnUnmatchedKeys() bool {

// Load will unmarshal configurations to struct from files that you provide
func (configor *Configor) Load(config interface{}, files ...string) (err error) {
defaultValue := reflect.Indirect(reflect.ValueOf(config))
if !defaultValue.CanAddr() {
return fmt.Errorf("Config %v should be addressable", config)
}
err = configor.load(config, files...)

if err == nil && configor.Config.AutoReload {
if configor.Config.AutoReload {
go func() {
timer := time.NewTimer(configor.Config.AutoReloadInterval)
for range timer.C {
configor.load(config, files...)
reflectPtr := reflect.New(reflect.ValueOf(config).Elem().Type())
reflectPtr.Elem().Set(defaultValue)

if err = configor.load(reflectPtr.Interface(), files...); err == nil {
reflect.ValueOf(config).Elem().Set(reflectPtr.Elem())
} else {
fmt.Printf("Failed to reload configuration from %v, got error %v\n", files, err)
}
timer.Reset(configor.Config.AutoReloadInterval)
}
}()
Expand Down

0 comments on commit 7fe8380

Please sign in to comment.