@@ -10,6 +10,7 @@ import (
10
10
"path"
11
11
"reflect"
12
12
"strings"
13
+ "time"
13
14
14
15
"github.com/BurntSushi/toml"
15
16
"gopkg.in/yaml.v2"
@@ -37,7 +38,7 @@ func (configor *Configor) getENVPrefix(config interface{}) string {
37
38
return configor .Config .ENVPrefix
38
39
}
39
40
40
- func getConfigurationFileWithENVPrefix (file , env string ) (string , error ) {
41
+ func getConfigurationFileWithENVPrefix (file , env string ) (string , time. Time , error ) {
41
42
var (
42
43
envFile string
43
44
extname = path .Ext (file )
@@ -50,13 +51,14 @@ func getConfigurationFileWithENVPrefix(file, env string) (string, error) {
50
51
}
51
52
52
53
if fileInfo , err := os .Stat (envFile ); err == nil && fileInfo .Mode ().IsRegular () {
53
- return envFile , nil
54
+ return envFile , fileInfo . ModTime (), nil
54
55
}
55
- return "" , fmt .Errorf ("failed to find file %v" , file )
56
+ return "" , time . Now (), fmt .Errorf ("failed to find file %v" , file )
56
57
}
57
58
58
- func (configor * Configor ) getConfigurationFiles (files ... string ) []string {
59
- var results []string
59
+ func (configor * Configor ) getConfigurationFiles (files ... string ) ([]string , map [string ]time.Time ) {
60
+ var resultKeys []string
61
+ var results = map [string ]time.Time {}
60
62
61
63
if configor .Config .Debug || configor .Config .Verbose {
62
64
fmt .Printf ("Current environment: '%v'\n " , configor .GetEnvironment ())
@@ -69,28 +71,31 @@ func (configor *Configor) getConfigurationFiles(files ...string) []string {
69
71
// check configuration
70
72
if fileInfo , err := os .Stat (file ); err == nil && fileInfo .Mode ().IsRegular () {
71
73
foundFile = true
72
- results = append (results , file )
74
+ resultKeys = append (resultKeys , file )
75
+ results [file ] = fileInfo .ModTime ()
73
76
}
74
77
75
78
// check configuration with env
76
- if file , err := getConfigurationFileWithENVPrefix (file , configor .GetEnvironment ()); err == nil {
79
+ if file , modTime , err := getConfigurationFileWithENVPrefix (file , configor .GetEnvironment ()); err == nil {
77
80
foundFile = true
78
- results = append (results , file )
81
+ resultKeys = append (resultKeys , file )
82
+ results [file ] = modTime
79
83
}
80
84
81
85
// check example configuration
82
86
if ! foundFile {
83
- if example , err := getConfigurationFileWithENVPrefix (file , "example" ); err == nil {
87
+ if example , modTime , err := getConfigurationFileWithENVPrefix (file , "example" ); err == nil {
84
88
if ! configor .Silent {
85
89
fmt .Printf ("Failed to find configuration %v, using example file %v\n " , file , example )
86
90
}
87
- results = append (results , example )
91
+ resultKeys = append (resultKeys , file )
92
+ results [example ] = modTime
88
93
} else if ! configor .Silent {
89
94
fmt .Printf ("Failed to find configuration %v\n " , file )
90
95
}
91
96
}
92
97
}
93
- return results
98
+ return resultKeys , results
94
99
}
95
100
96
101
func processFile (config interface {}, file string , errorOnUnmatchedKeys bool ) error {
@@ -294,7 +299,7 @@ func (configor *Configor) processTags(config interface{}, prefixes ...string) er
294
299
return nil
295
300
}
296
301
297
- func (configor * Configor ) load (config interface {}, files ... string ) (err error ) {
302
+ func (configor * Configor ) load (config interface {}, watchMode bool , files ... string ) (err error , changed bool ) {
298
303
defer func () {
299
304
if configor .Config .Debug || configor .Config .Verbose {
300
305
if err != nil {
@@ -305,20 +310,38 @@ func (configor *Configor) load(config interface{}, files ...string) (err error)
305
310
}
306
311
}()
307
312
308
- for _ , file := range configor .getConfigurationFiles (files ... ) {
313
+ configFiles , configModTimeMap := configor .getConfigurationFiles (files ... )
314
+
315
+ if watchMode {
316
+ if len (configModTimeMap ) == len (configor .configModTimes ) {
317
+ var changed bool
318
+ for f , t := range configModTimeMap {
319
+ if v , ok := configor .configModTimes [f ]; ! ok || t .After (v ) {
320
+ changed = true
321
+ }
322
+ }
323
+
324
+ if ! changed {
325
+ return nil , false
326
+ }
327
+ }
328
+ }
329
+
330
+ for _ , file := range configFiles {
309
331
if configor .Config .Debug || configor .Config .Verbose {
310
332
fmt .Printf ("Loading configurations from file '%v'...\n " , file )
311
333
}
312
334
if err = processFile (config , file , configor .GetErrorOnUnmatchedKeys ()); err != nil {
313
- return err
335
+ return err , true
314
336
}
315
337
}
338
+ configor .configModTimes = configModTimeMap
316
339
317
340
if prefix := configor .getENVPrefix (config ); prefix == "-" {
318
341
err = configor .processTags (config )
319
342
} else {
320
343
err = configor .processTags (config , prefix )
321
344
}
322
345
323
- return err
346
+ return err , true
324
347
}
0 commit comments