forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
183 lines (154 loc) · 4.27 KB
/
printer.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
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/olekukonko/tablewriter"
v3 "github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
spb "github.com/coreos/etcd/storage/storagepb"
)
type printer interface {
Del(v3.DeleteResponse)
Get(v3.GetResponse)
Put(v3.PutResponse)
Txn(v3.TxnResponse)
Watch(v3.WatchResponse)
MemberList(v3.MemberListResponse)
}
func NewPrinter(printerType string, isHex bool) printer {
switch printerType {
case "simple":
return &simplePrinter{isHex: isHex}
case "json":
return &jsonPrinter{}
case "protobuf":
return &pbPrinter{}
}
return nil
}
type simplePrinter struct {
isHex bool
}
func (s *simplePrinter) Del(v3.DeleteResponse) {
// TODO: add number of key removed into the response of delete.
// TODO: print out the number of removed keys.
fmt.Println(0)
}
func (s *simplePrinter) Get(resp v3.GetResponse) {
for _, kv := range resp.Kvs {
printKV(s.isHex, kv)
}
}
func (s *simplePrinter) Put(r v3.PutResponse) { fmt.Println("OK") }
func (s *simplePrinter) Txn(resp v3.TxnResponse) {
if resp.Succeeded {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
for _, r := range resp.Responses {
fmt.Println("")
switch v := r.Response.(type) {
case *pb.ResponseUnion_ResponseDeleteRange:
s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
case *pb.ResponseUnion_ResponsePut:
s.Put((v3.PutResponse)(*v.ResponsePut))
case *pb.ResponseUnion_ResponseRange:
s.Get(((v3.GetResponse)(*v.ResponseRange)))
default:
fmt.Printf("unexpected response %+v\n", r)
}
}
}
func (s *simplePrinter) Watch(resp v3.WatchResponse) {
for _, e := range resp.Events {
fmt.Println(e.Type)
printKV(s.isHex, e.Kv)
}
}
func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs", "Is Leader"})
for _, m := range resp.Members {
status := "started"
if len(m.Name) == 0 {
status = "unstarted"
}
table.Append([]string{
fmt.Sprintf("%x", m.ID),
status,
m.Name,
strings.Join(m.PeerURLs, ","),
strings.Join(m.ClientURLs, ","),
fmt.Sprint(m.IsLeader),
})
}
table.Render()
}
type jsonPrinter struct{}
func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
func (p *jsonPrinter) Get(r v3.GetResponse) {
for _, kv := range r.Kvs {
printJSON(kv)
}
}
func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
func printJSON(v interface{}) {
b, err := json.Marshal(v)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
fmt.Println(string(b))
}
type pbPrinter struct{}
type pbMarshal interface {
Marshal() ([]byte, error)
}
func (p *pbPrinter) Del(r v3.DeleteResponse) {
printPB((*pb.DeleteRangeResponse)(&r))
}
func (p *pbPrinter) Get(r v3.GetResponse) {
printPB((*pb.RangeResponse)(&r))
}
func (p *pbPrinter) Put(r v3.PutResponse) {
printPB((*pb.PutResponse)(&r))
}
func (p *pbPrinter) Txn(r v3.TxnResponse) {
printPB((*pb.TxnResponse)(&r))
}
func (p *pbPrinter) Watch(r v3.WatchResponse) {
for _, ev := range r.Events {
printPB((*spb.Event)(ev))
}
}
func (pb *pbPrinter) MemberList(r v3.MemberListResponse) {
ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
}
func printPB(m pbMarshal) {
b, err := m.Marshal()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
fmt.Printf(string(b))
}