-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (47 loc) · 971 Bytes
/
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
package goconfig_center
import (
"bytes"
"crypto/md5"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"log"
)
func Equal(v1, v2 *viper.Viper) bool {
if v1 == v2 {
return true
}
if v1 == nil || v2 == nil {
return v1 == v2
}
return genHash(v1) == genHash(v2)
}
func genHash(v *viper.Viper) string {
if v == nil {
// 如果为空则返回空字符串
return ""
}
c := v.AllSettings()
bs, err := yaml.Marshal(c)
if err != nil {
log.Fatalf("unable to marshal config to YAML: %v", err)
}
hash := md5.Sum(bs)
return string(hash[:])
}
func mergeConfig(viper, sourceViper *viper.Viper, prefix ...string) {
if len(prefix) == 0 {
prefix = []string{""}
}
viper.SetConfigType("yml")
cfg := sourceViper.AllSettings()
if prefix[0] != "" {
cfg = map[string]interface{}{
prefix[0]: cfg,
}
}
bs, err := yaml.Marshal(cfg)
if err != nil {
log.Fatalf("unable to marshal config to YAML: %v", err)
}
_ = viper.MergeConfig(bytes.NewBuffer(bs))
}