Skip to content

Commit 982252f

Browse files
author
tietang
committed
add Eval & Backup
1 parent a5813bd commit 982252f

9 files changed

+408
-145
lines changed

example/ccs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main_css() {
3131
//zccs := zk.NewZookeeperCompositeConfigSource(contexts, urls, time.Second*3)
3232
configSources := []kvs.ConfigSource{pcs1, pcs2}
3333
ccs := kvs.NewDefaultCompositeConfigSource(configSources...)
34-
ccs.SaveToDisk()
34+
ccs.Backup()
3535

3636
//
3737

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ require (
3939
github.com/gogo/protobuf v1.2.1 // indirect
4040
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
4141
github.com/google/btree v1.0.0 // indirect
42+
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
4243
github.com/gorilla/websocket v1.4.0 // indirect
4344
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
4445
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
@@ -56,7 +57,8 @@ require (
5657
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
5758
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
5859
github.com/sirupsen/logrus v1.6.0
59-
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a
60+
github.com/smartystreets/assertions v1.1.1 // indirect
61+
github.com/smartystreets/goconvey v1.6.4
6062
github.com/soheilhy/cmux v0.1.4 // indirect
6163
github.com/tietang/go-utils v0.1.3
6264
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect

kvs/backup.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package kvs
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"os"
6+
"path/filepath"
7+
"time"
8+
)
9+
10+
const (
11+
BackupFile = ".conf/all.properties"
12+
)
13+
14+
type Backup interface {
15+
Restore()
16+
Backup()
17+
}
18+
19+
var _ Backup = new(CompositeConfigSource)
20+
21+
type DiskBackup struct {
22+
ccs CompositeConfigSource
23+
BackupFileName string
24+
}
25+
26+
func (d *DiskBackup) Restore() {
27+
props := NewPropertiesConfigSource(d.BackupFileName)
28+
d.ccs.Add(props)
29+
}
30+
31+
func (d *DiskBackup) Backup() {
32+
33+
dir, _ := os.Getwd()
34+
now := time.Now()
35+
time := now.Format("20060102150405")
36+
d.BackupFileName = filepath.Join(dir, BackupFile)
37+
if PathExists(d.BackupFileName) {
38+
newFileName := filepath.Join(dir, BackupFile+"."+time)
39+
err := os.Rename(d.BackupFileName, newFileName)
40+
if err != nil {
41+
log.Error(err)
42+
log.Error("重命名本地文件失败:"+d.BackupFileName, " to ", newFileName)
43+
return
44+
}
45+
}
46+
os.MkdirAll(filepath.Dir(d.BackupFileName), os.ModePerm)
47+
f, err := os.Create(d.BackupFileName)
48+
if err != nil {
49+
log.Error(err)
50+
log.Error("备份到本地文件失败:" + d.BackupFileName)
51+
return
52+
}
53+
defer f.Close()
54+
for _, k := range d.ccs.Keys() {
55+
v := d.ccs.GetDefault(k, "")
56+
f.WriteString(k)
57+
f.WriteString("=")
58+
f.WriteString(v)
59+
f.WriteString("\n")
60+
err = f.Sync()
61+
if err != nil {
62+
log.Error(err)
63+
}
64+
}
65+
66+
}
67+
68+
func PathExists(path string) bool {
69+
_, err := os.Stat(path)
70+
if err == nil {
71+
return true
72+
}
73+
if os.IsNotExist(err) {
74+
log.Warn(err)
75+
return false
76+
}
77+
return false
78+
}

kvs/config_composite.go

+20-26
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import (
1111
"time"
1212
)
1313

14-
const (
15-
BackupFile = ".conf/all.properties"
16-
)
17-
1814
//
1915
type CompositeConfigSource struct {
2016
ConfName string
@@ -59,11 +55,12 @@ func NewCompositeConfigSource(name string, isAppendSystemEnv bool, configSources
5955
}
6056
return s
6157
}
62-
func (ccs *CompositeConfigSource) FallbackFromDisk() {
58+
59+
func (ccs *CompositeConfigSource) Restore() {
6360
props := NewPropertiesConfigSource(ccs.backFileName)
6461
ccs.Add(props)
6562
}
66-
func (ccs *CompositeConfigSource) SaveToDisk() {
63+
func (ccs *CompositeConfigSource) Backup() {
6764

6865
dir, _ := os.Getwd()
6966
now := time.Now()
@@ -100,18 +97,6 @@ func (ccs *CompositeConfigSource) SaveToDisk() {
10097

10198
}
10299

103-
func PathExists(path string) bool {
104-
_, err := os.Stat(path)
105-
if err == nil {
106-
return true
107-
}
108-
if os.IsNotExist(err) {
109-
log.Warn(err)
110-
return false
111-
}
112-
return false
113-
}
114-
115100
func (ccs *CompositeConfigSource) Name() string {
116101
return ccs.ConfName
117102
}
@@ -151,19 +136,24 @@ func (ccs *CompositeConfigSource) KeyValue(key string) *KeyValue {
151136
break
152137
}
153138
}
154-
139+
var kv *KeyValue
140+
defer func() {
141+
if kv != nil && kv.err != nil {
142+
log.Warnf("for `%s` err: %s ", key, kv.err.Error())
143+
}
144+
}()
155145
if __reg.MatchString(val) {
156-
v, err := ccs.evalValue(val)
157-
kv := NewKeyValue(key, v)
146+
v, err := ccs.EvalValue(val)
147+
kv = NewKeyValue(key, v)
158148
kv.err = err
159149
return kv
160150
}
161151
if hasExists {
162-
kv := NewKeyValue(key, val)
152+
kv = NewKeyValue(key, val)
163153
kv.err = nil
164154
return kv
165155
} else {
166-
kv := NewKeyValue(key, val)
156+
kv = NewKeyValue(key, val)
167157
kv.err = errors.New("not exists for key: " + key)
168158
return kv
169159
}
@@ -336,7 +326,7 @@ func (ccs *CompositeConfigSource) GetValue(key string) (string, error) {
336326
//}
337327
//
338328
//if __reg.MatchString(val) {
339-
// return ccs.evalValue(val)
329+
// return ccs.EvalValue(val)
340330
//}
341331
//if hasExists {
342332
// return val, nil
@@ -349,7 +339,7 @@ func (ccs *CompositeConfigSource) GetValue(key string) (string, error) {
349339
return kv.value, kv.err
350340
}
351341

352-
func (ccs *CompositeConfigSource) evalValue(value string) (string, error) {
342+
func (ccs *CompositeConfigSource) EvalValue(value string) (string, error) {
353343
if strings.Contains(value, ccs.StartTag) {
354344
eval := fasttemplate.New(value, ccs.StartTag, ccs.EndTag)
355345
str := eval.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
@@ -360,7 +350,11 @@ func (ccs *CompositeConfigSource) evalValue(value string) (string, error) {
360350
return w.Write([]byte(""))
361351
}
362352
})
363-
return str, nil
353+
var err error
354+
if str != value {
355+
str, err = ccs.EvalValue(str)
356+
}
357+
return str, err
364358
}
365359
return value, nil
366360
}

kvs/config_source.go

-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kvs
22

33
import (
44
"bufio"
5-
"regexp"
65
"strings"
76
"time"
87
)
@@ -65,14 +64,6 @@ func ReadContentType(content string) ContentType {
6564
return TextContentType
6665
}
6766

68-
const (
69-
__START_TAG = "${"
70-
__END_TAG = "}"
71-
DEFAULT_VALUE = ""
72-
)
73-
74-
var __reg = regexp.MustCompile("\\$\\{(.*)}")
75-
7667
type ConfigSource interface {
7768
Name() string
7869

0 commit comments

Comments
 (0)