/
main.go
219 lines (206 loc) · 4.27 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"errors"
"fmt"
"github.com/RealFax/RedQueen/internal/version"
"github.com/RealFax/RedQueen/pkg/client"
"net/netip"
"os"
"strings"
"syscall"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
helpText = `put the endpoint info into the environment variable: RQ_ENDPOINTS
format: 172.16.0.100:5230,172.16.0.101:5230,172.16.0.102:5230
`
)
var (
invoker *client.Client
)
func dialRQ() error {
endpointString, ok := syscall.Getenv("RQ_ENDPOINTS")
if !ok {
fmt.Print(helpText)
return errors.New("can not read endpoints from environment variable RQ_ENDPOINTS")
}
dirtyEndpoints := strings.Split(endpointString, ",")
var (
err error
endpoints = make([]string, len(dirtyEndpoints))
)
for i, endpoint := range dirtyEndpoints {
addr := strings.TrimSpace(endpoint)
if _, err = netip.ParseAddrPort(addr); err != nil {
return err
}
endpoints[i] = addr
}
client.SetMaxOpenConn(1)
if invoker, err = client.New(
context.Background(),
endpoints,
grpc.WithTransportCredentials(insecure.NewCredentials()),
); err != nil {
return err
}
return nil
}
func main() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Print(version.String())
}
app := &cli.App{
Name: "rqctl",
Version: version.String(),
Usage: "cli client for RedQueen 🤖",
UsageText: helpText,
Commands: []*cli.Command{
{
Name: "append-cluster",
UsageText: "Append a new node to the raft cluster",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server-id",
Usage: "RedQueen node server id",
},
&cli.StringFlag{
Name: "peer-addr",
Usage: "RedQueen peer address",
},
},
Action: NodeAppendCluster,
}, {
Name: "leader-monitor",
UsageText: "Monitor the election (voting) status of the specified node",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Usage: "RedQueen server endpoint address",
},
},
Action: NodeLeaderMonitor,
}, {
Name: "snapshot",
UsageText: "Snapshot create server snapshot in server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Usage: "RedQueen server endpoint address",
},
&cli.StringFlag{
Name: "path",
Usage: "The path of the snapshot in the server",
},
},
Action: NodeSnapshot,
}, {
Name: "set",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
},
&cli.StringFlag{
Name: "value",
Usage: "if you need to set a binary value, please enter it after base64/hex and carry the -base64/-hex flag",
},
&cli.StringFlag{
Name: "namespace",
},
&cli.UintFlag{
Name: "ttl",
},
&cli.BoolFlag{
Name: "hex",
},
&cli.BoolFlag{
Name: "base64",
},
},
Action: RPCSet,
}, {
Name: "get",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
},
&cli.StringFlag{
Name: "namespace",
},
},
Action: RPCGet,
}, {
Name: "prefix-scan",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prefix",
},
&cli.StringFlag{
Name: "reg",
Usage: "Regular expression scan",
},
&cli.StringFlag{
Name: "namespace",
},
&cli.Uint64Flag{
Name: "offset",
Value: 0,
},
&cli.Uint64Flag{
Name: "limit",
Value: 10,
},
},
Action: RPCPrefixScan,
}, {
Name: "del",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
},
&cli.StringFlag{
Name: "namespace",
},
},
Action: RPCDel,
}, {
Name: "watch",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
},
&cli.StringFlag{
Name: "namespace",
},
&cli.BoolFlag{
Name: "ignoreError",
},
},
Action: RPCWatch,
}, {
Name: "watch-prefix",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prefix",
},
&cli.StringFlag{
Name: "namespace",
},
},
Action: RPCWatchPrefix,
},
},
}
if err := dialRQ(); err != nil {
fmt.Printf("[-] %s.", err)
return
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("[-] %s.", err)
return
}
fmt.Println("\n[+] command completed successfully.")
}