-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
61 lines (50 loc) · 1.33 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package local
import (
"github.com/hopeio/cherry/utils/log"
"os"
"time"
)
func (configor *Configor) getConfigurationFiles(files ...string) ([]string, map[string]time.Time) {
var resultKeys []string
var results = map[string]time.Time{}
for i := len(files) - 1; i >= 0; i-- {
file := files[i]
// check configuration
if fileInfo, err := os.Stat(file); err == nil && fileInfo.Mode().IsRegular() {
resultKeys = append(resultKeys, file)
results[file] = fileInfo.ModTime()
}
}
return resultKeys, results
}
func (configor *Configor) handle(handle func([]byte), watchMode bool, files ...string) (err error, changed bool) {
defer func() {
if err != nil {
log.Errorf("Failed to load configuration from %v, got %v\n", files, err)
}
}()
configFiles, configModTimeMap := configor.getConfigurationFiles(files...)
if watchMode {
if len(configModTimeMap) == len(configor.configModTimes) {
var changed bool
for f, t := range configModTimeMap {
if v, ok := configor.configModTimes[f]; !ok || t.After(v) {
changed = true
}
}
if !changed {
return nil, false
}
}
}
for _, file := range configFiles {
log.Debugf("load config from: '%v'", file)
data, err := os.ReadFile(file)
if err != nil {
return err, true
}
handle(data)
}
configor.configModTimes = configModTimeMap
return err, true
}