Skip to content

Commit

Permalink
auto create path
Browse files Browse the repository at this point in the history
  • Loading branch information
evanzhang87 committed Jul 31, 2024
1 parent e48d396 commit fb8de00
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions cmd/zk-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func init() {
flag.StringVar(&src, "src", "127.0.0.1:2181", "src zk addr")
flag.StringVar(&dst, "dst", "127.0.0.1:2181", "dst zk addr")
flag.StringVar(&path, "path", "/", "zkpath")
flag.Parse()
}

func main() {
Expand All @@ -41,6 +42,7 @@ func main() {
func iterChild(src, dst *zk.Conn, path string) {
childs, _, _ := src.Children(path)
if len(childs) > 0 {
dstCheckPath(dst, path)
for _, child := range childs {
if path == "/" {
path = ""
Expand All @@ -49,11 +51,33 @@ func iterChild(src, dst *zk.Conn, path string) {
}
} else {
data, stat, _ := src.Get(path)
_, err := dst.Set(path, data, stat.Version)
exist, _, _ := dst.Exists(path)
if exist {
_, err := dst.Set(path, data, stat.Version)
if err != nil {
fmt.Printf("path %s err: %s \n", path, err)
} else {
fmt.Printf("path %s replace ok\n", path)
}
} else {
_, err := dst.Create(path, data, 0, zk.WorldACL(zk.PermAll))
if err != nil {
fmt.Printf("path %s err: %s \n", path, err)
} else {
fmt.Printf("path %s create ok\n", path)
}
}
}
}

func dstCheckPath(dst *zk.Conn, path string) {
exist, _, _ := dst.Exists(path)
if !exist {
_, err := dst.Create(path, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil {
fmt.Printf("path %s err: %s \n", path, err)
} else {
fmt.Printf("path %s ok\n", path)
fmt.Printf("path %s create ok\n", path)
}
}
}

0 comments on commit fb8de00

Please sign in to comment.