This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
84 lines (68 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/facebookincubator/zk"
)
const usage = `
usage: go run example/main.go [-server] [command] [path]
This CLI is used to get data from a given Zookeeper server node.
The first argument represents the command that the user wants to execute. Available commands:
- 'get' - gets data from a Zookeeper node
- 'list' - lists all children of a Zookeeper node
The second argument is used to specify the Zookeeper node path.
Users can specify server address using the --server flag, which is by default set to localhost:2181.
Example usage:
go run example/main.go get /
go run example/main.go --server=192.168.2.3:1234 list /zookeeper
`
func main() {
flag.Usage = func() {
if _, err := fmt.Fprint(flag.CommandLine.Output(), usage); err != nil {
return
}
}
server := flag.String("server", "127.0.0.1:2181", "Zookeeper server address.")
flag.Parse()
if len(flag.Args()) != 2 {
flag.Usage()
return
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client := zk.Client{}
conn, err := client.DialContext(ctx, "tcp", *server)
if err != nil {
fmt.Println("error dialing server:", err)
return
}
cmd := flag.Args()[0]
path := flag.Args()[1]
switch cmd {
case "get":
data, err := conn.GetData(path)
if err != nil {
fmt.Println("getData error:", err)
return
}
fmt.Printf("Data for node %s: %v\n", path, string(data))
case "list":
children, err := conn.GetChildren(path)
if err != nil {
fmt.Println("getChildren error:", err)
return
}
fmt.Printf("Children of node %s: %v\n", path, children)
default:
fmt.Printf("Cannot recognize command \"%s\", exiting.\n", cmd)
}
}