Skip to content

Commit 5986c70

Browse files
committed
The output of look command can be used to the loki
1 parent ce2f9e2 commit 5986c70

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ $ etcdctl+ distribute -h
2323
Download the [etcdctl+.ts](ts/etcdctl+.ts), and [config it](https://simfg.github.io/fig). You can also use the [etcdctl.ts](https://simfg.github.io/etcdctl.ts) config the `etcdctl` command.
2424

2525
## Function List
26+
27+
1. **distribute** View data distribution according to data size
28+
2. **look** Show or export all the etcd data, and be used with terminal or loki
29+
3. **find** Get key based on certain characters
30+
4. **leader** Get the leader node info
31+
5. **clear** Clear all the etcd data
32+
6. **decode** decode the etcd value that is encoded
33+
2634
### distribute
2735
View data distribution in etcd according to the `key` size , `value` size or `key + value` size by setting the `type` command param.
2836
```shell
@@ -81,6 +89,19 @@ Kv List
8189
| by-dev/meta/channelwatch/-9223372036854775808/by-dev-rootcoord-dml_4_435191634150817793v0 | - | 326013 | 326013 | 1 | 0 |
8290
```
8391

92+
### find
93+
Get key based on certain characters
94+
```shell
95+
$ ./etcdctl+ find --key=index
96+
Kv List
97+
| Key | Value |
98+
| by-dev/meta/field-index/438660758500016136/438660903999573339 | |
99+
| by-dev/meta/segment-index/438660758500016136/438660758500016137/438660758500216145/438660903999573340 | |
100+
| by-dev/meta/segment-index/438660758500016136/438660758500016137/438660758500216146/438660903999573341 | |
101+
| by-dev/meta/segment-index/438660758500016136/438660758500016137/438672571137461581/438672571137461597 | |
102+
| by-dev/meta/segment-index/438660758500016136/438660758500016137/438672571137461582/438672571137461598 | |
103+
```
104+
84105
### leader
85106
Get the leader node info
86107
```shell

bin/etcdctl+

0 Bytes
Binary file not shown.

cmd/look_cmd.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,25 @@ Sometimes, you may only want to observe data of a certain range size, you can se
5959
cmd.Flags().IntVar(&filterMin, "filter-min", -1, "The filter min value")
6060

6161
cmd.RegisterFlagCompletionFunc("write-out", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
62-
return []string{"stdout", "file"}, cobra.ShellCompDirectiveDefault
62+
return []string{"stdout", "file", "log"}, cobra.ShellCompDirectiveDefault
6363
})
6464
cmd.RegisterFlagCompletionFunc("filter", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
6565
return []string{"none", "key", "value", "kv"}, cobra.ShellCompDirectiveDefault
6666
})
6767
return cmd
6868
}
6969

70+
func isLog() bool {
71+
return writeOut == "log"
72+
}
73+
7074
func lookFunc(cmd *cobra.Command, args []string) {
7175
core.InitClient()
7276
resp, datac := core.GetAllData()
7377

7478
var writer io.Writer
7579
switch writeOut {
76-
case "file":
80+
case "file", "log":
7781
f := GetFileWriter()
7882
defer f.Close()
7983
writer = f
@@ -104,10 +108,12 @@ func appendBuffer(resp *clientv3.GetResponse, datac <-chan []*mvccpb.KeyValue, w
104108
f.Seek(0, 0)
105109
}
106110
var buffer bytes.Buffer
107-
buffer.WriteString("Current Stage\n")
108-
buffer.WriteString(fmt.Sprintf(" %s", resp.Header.String()))
109-
buffer.WriteString("\nKv List\n")
110-
buffer.WriteString("| Key | Value | Size | CreateRevision | ModRevision | Version | Lease |\n")
111+
if !isLog() {
112+
buffer.WriteString("Current Stage\n")
113+
buffer.WriteString(fmt.Sprintf(" %s", resp.Header.String()))
114+
buffer.WriteString("\nKv List\n")
115+
buffer.WriteString("| Key | Value | Size | CreateRevision | ModRevision | Version | Lease |\n")
116+
}
111117

112118
for data := range datac {
113119
for _, kv := range data {
@@ -119,7 +125,11 @@ func appendBuffer(resp *clientv3.GetResponse, datac <-chan []*mvccpb.KeyValue, w
119125
if showValue {
120126
v = base64.StdEncoding.EncodeToString(kv.Value)
121127
}
122-
buffer.WriteString(fmt.Sprintf("| %s | %s | %s | %d | %d | %d | %d |\n",
128+
format := "| %s | %s | %s | %d | %d | %d | %d |\n"
129+
if isLog() {
130+
format = "key=%s value=%s size=%s create_revision=%d mod_revision=%d version=%d lease=%d\n"
131+
}
132+
buffer.WriteString(fmt.Sprintf(format,
123133
string(kv.Key), v,
124134
core.ReadableSize(size),
125135
kv.CreateRevision, kv.ModRevision, kv.Version,

core/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
byteUnits = []string{"B", "KB", "MB", "GB"}
12+
byteUnits = []string{"b", "kb", "mb", "gb", "tb", "pb", "eb"}
1313
)
1414

1515
func Exit(err error) {
@@ -42,7 +42,7 @@ func ReadableSize(s int) string {
4242
i++
4343
sf /= 1024
4444
}
45-
return fmt.Sprintf("%.1f %s", sf, byteUnits[i])
45+
return fmt.Sprintf("%.1f%s", sf, byteUnits[i])
4646
}
4747

4848
func Interrupt(f func()) {

0 commit comments

Comments
 (0)