-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
96 lines (89 loc) · 1.88 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bufio"
"fmt"
"os"
"strings"
"zk"
)
var (
TESTIP = []string{
//"172.19.32.16",
"192.168.56.101",
}
)
func main() {
fmt.Println("You can use the following commands:")
fmt.Println("exist path")
fmt.Println("create path data")
fmt.Println("get path")
fmt.Println("set path data")
fmt.Println("ls path")
fmt.Println("del path")
fmt.Println("delrec path")
fmt.Println("Or you will enter 'quit' to exit.")
conn := zk.New()
err := conn.Connect(TESTIP)
if err != nil {
panic(err)
}
//defer conn.Close()
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf(" > ")
data, _, _ := reader.ReadLine()
cmd := string(data)
s := strings.Split(cmd, " ")
if len(s) == 1 {
if s[0] == "quit" {
return
}
} else if len(s) == 2 {
if s[0] == "ls" {
children, err := conn.Children(s[1])
if err != nil {
panic(err)
}
fmt.Printf("List children of [%d] [%s]: %+v\n", len(children), s[1], children)
} else if s[0] == "get" {
data, err := conn.Get(s[1])
if err != nil {
panic(err)
}
fmt.Printf("Data of [%s]: %+v\n", s[1], string(data))
} else if s[0] == "exist" {
flag, err := conn.Exists(s[1])
if err != nil {
panic(err)
}
fmt.Printf("[%s] exist: %+v\n", s[1], flag)
} else if s[0] == "del" {
err := conn.Delete(s[1])
if err != nil {
panic(err)
}
fmt.Printf("[%s] delete!\n", s[1])
} else if s[0] == "delrec" {
err := conn.DeleteRecur(s[1])
if err != nil {
panic(err)
}
fmt.Printf("[%s] recur delete!\n", s[1])
}
} else if len(s) == 3 {
if s[0] == "create" {
err := conn.Create(s[1], []byte(s[2]))
if err != nil {
panic(err)
}
fmt.Printf("[%s] created!\n", s[1])
} else if s[0] == "set" {
err := conn.Set(s[1], []byte(s[2]))
if err != nil {
panic(err)
}
fmt.Printf("[%s] set!\n", s[1])
}
}
}
}