-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkctl.go
151 lines (128 loc) · 3.22 KB
/
zkctl.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"github.com/samuel/go-zookeeper/zk"
"github.com/spf13/cobra"
"github.com/xuannnaux/zkctl/op"
"github.com/xuannnaux/zkctl/util"
"time"
"log"
)
var (
version = "0.0.1"
zkConn *zk.Conn
zkSessionTimeout = 3 * time.Second
serverList []string
node string
data string
)
func init() {
log.SetFlags(0)
}
func PreInit() {
var err error
zkConn, _, err = zk.Connect(serverList, zkSessionTimeout, zk.WithLogInfo(false))
if err != nil {
log.Fatalf("connect to zk failed, %s", err)
return
}
}
func main() {
defer func() {
if zkConn != nil {
zkConn.Close()
}
}()
// root command and global flag
cmdRoot := &cobra.Command{
Version: version,
Use: "zkctl",
Short: "zookeeper management tool",
Long: "a tool to manage zookeeper node",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error
serverList, err = util.ParseServer(cmd.Flags())
if err != nil {
log.Fatal(err)
}
node, err = util.ParseNode(cmd.Flags())
if err != nil {
log.Fatal(err)
}
data, err = util.ParseData(cmd.Flags())
if err != nil {
log.Fatal(err)
}
PreInit()
},
}
cmdRoot.ValidArgs = []string{"create", "list", "get", "exist", "set", "delete", "meta", "size"}
cmdRoot.PersistentFlags().StringP("server", "s", "127.0.0.1:2181", "zk server address")
cmdRoot.PersistentFlags().StringP("node", "n", "", "node path")
cmdRoot.PersistentFlags().StringP("data", "d", "", "node data")
// create node
cmdCreate := &cobra.Command{
Use: "create",
Short: "create a zk node",
Long: "create a zk node, default to permanent node",
Run: func(cmd *cobra.Command, args []string) {
force, err := cmd.Flags().GetBool("force")
if err != nil {
log.Fatal("invalid force flag")
}
ephemeral, err := cmd.Flags().GetBool("ephemeral")
if err != nil {
log.Fatal("invalid ephemeral flag")
}
op.Create(zkConn, node, data, force, ephemeral)
},
}
cmdCreate.Flags().BoolP("force", "f", false, "force replace node if node exists")
cmdCreate.Flags().BoolP("ephemeral", "e", false, "set node as ephemeral node")
// list node children
cmdList := &cobra.Command{
Use: "list",
Short: "list path children",
Long: "list path children",
Run: func(cmd *cobra.Command, args []string) {
op.List(zkConn, node)
},
}
// get node data
cmdGet := &cobra.Command{
Use: "get",
Short: "show node data",
Long: "show node data",
Run: func(cmd *cobra.Command, args []string) {
op.Get(zkConn, node)
},
}
// node exists
cmdExists := &cobra.Command{
Use: "exist",
Short: "check if node exist or not",
Long: "check if node exist or not",
Run: func(cmd *cobra.Command, args []string) {
op.Exists(zkConn, node)
},
}
// set node
cmdSet := &cobra.Command{
Use: "set",
Short: "set node data",
Long: "set node data, node must exists",
Run: func(cmd *cobra.Command, args []string) {
op.Set(zkConn, node, data)
},
}
// delete node
cmdDelete := &cobra.Command{
Use: "delete",
Short: "delete node",
Long: "delete node",
Run: func(cmd *cobra.Command, args []string) {
op.Delete(zkConn, node)
},
}
cmdRoot.AddCommand(cmdCreate, cmdList, cmdGet, cmdExists, cmdSet, cmdDelete)
cmdRoot.Execute()
}