printer.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "strings"
  21. v3 "github.com/coreos/etcd/clientv3"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. spb "github.com/coreos/etcd/mvcc/mvccpb"
  24. "github.com/dustin/go-humanize"
  25. "github.com/olekukonko/tablewriter"
  26. )
  27. type printer interface {
  28. Del(v3.DeleteResponse)
  29. Get(v3.GetResponse)
  30. Put(v3.PutResponse)
  31. Txn(v3.TxnResponse)
  32. Watch(v3.WatchResponse)
  33. MemberList(v3.MemberListResponse)
  34. EndpointStatus([]epStatus)
  35. Alarm(v3.AlarmResponse)
  36. DBStatus(dbstatus)
  37. }
  38. func NewPrinter(printerType string, isHex bool) printer {
  39. switch printerType {
  40. case "simple":
  41. return &simplePrinter{isHex: isHex}
  42. case "json":
  43. return &jsonPrinter{}
  44. case "protobuf":
  45. return &pbPrinter{}
  46. case "table":
  47. return &tablePrinter{}
  48. }
  49. return nil
  50. }
  51. func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string) {
  52. hdr = []string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"}
  53. for _, m := range r.Members {
  54. status := "started"
  55. if len(m.Name) == 0 {
  56. status = "unstarted"
  57. }
  58. rows = append(rows, []string{
  59. fmt.Sprintf("%x", m.ID),
  60. status,
  61. m.Name,
  62. strings.Join(m.PeerURLs, ","),
  63. strings.Join(m.ClientURLs, ","),
  64. })
  65. }
  66. return
  67. }
  68. func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) {
  69. hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"}
  70. for _, status := range statusList {
  71. rows = append(rows, []string{
  72. fmt.Sprint(status.Ep),
  73. fmt.Sprintf("%x", status.Resp.Header.MemberId),
  74. fmt.Sprint(status.Resp.Version),
  75. fmt.Sprint(humanize.Bytes(uint64(status.Resp.DbSize))),
  76. fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
  77. fmt.Sprint(status.Resp.RaftTerm),
  78. fmt.Sprint(status.Resp.RaftIndex),
  79. })
  80. }
  81. return
  82. }
  83. func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
  84. hdr = []string{"hash", "revision", "total keys", "total size"}
  85. rows = append(rows, []string{
  86. fmt.Sprintf("%x", ds.Hash),
  87. fmt.Sprint(ds.Revision),
  88. fmt.Sprint(ds.TotalKey),
  89. humanize.Bytes(uint64(ds.TotalSize)),
  90. })
  91. return
  92. }
  93. type simplePrinter struct {
  94. isHex bool
  95. }
  96. func (s *simplePrinter) Del(resp v3.DeleteResponse) {
  97. fmt.Println(resp.Deleted)
  98. for _, kv := range resp.KVs {
  99. printKV(s.isHex, kv)
  100. }
  101. }
  102. func (s *simplePrinter) Get(resp v3.GetResponse) {
  103. for _, kv := range resp.Kvs {
  104. printKV(s.isHex, kv)
  105. }
  106. }
  107. func (s *simplePrinter) Put(r v3.PutResponse) { fmt.Println("OK") }
  108. func (s *simplePrinter) Txn(resp v3.TxnResponse) {
  109. if resp.Succeeded {
  110. fmt.Println("SUCCESS")
  111. } else {
  112. fmt.Println("FAILURE")
  113. }
  114. for _, r := range resp.Responses {
  115. fmt.Println("")
  116. switch v := r.Response.(type) {
  117. case *pb.ResponseOp_ResponseDeleteRange:
  118. s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  119. case *pb.ResponseOp_ResponsePut:
  120. s.Put((v3.PutResponse)(*v.ResponsePut))
  121. case *pb.ResponseOp_ResponseRange:
  122. s.Get(((v3.GetResponse)(*v.ResponseRange)))
  123. default:
  124. fmt.Printf("unexpected response %+v\n", r)
  125. }
  126. }
  127. }
  128. func (s *simplePrinter) Watch(resp v3.WatchResponse) {
  129. for _, e := range resp.Events {
  130. fmt.Println(e.Type)
  131. printKV(s.isHex, e.Kv)
  132. }
  133. }
  134. func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
  135. for _, e := range resp.Alarms {
  136. fmt.Printf("%+v\n", e)
  137. }
  138. }
  139. func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
  140. _, rows := makeMemberListTable(resp)
  141. for _, row := range rows {
  142. fmt.Println(strings.Join(row, ", "))
  143. }
  144. }
  145. func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
  146. _, rows := makeEndpointStatusTable(statusList)
  147. for _, row := range rows {
  148. fmt.Println(strings.Join(row, ", "))
  149. }
  150. }
  151. func (s *simplePrinter) DBStatus(ds dbstatus) {
  152. _, rows := makeDBStatusTable(ds)
  153. for _, row := range rows {
  154. fmt.Println(strings.Join(row, ", "))
  155. }
  156. }
  157. type tablePrinter struct{}
  158. func (tp *tablePrinter) Del(r v3.DeleteResponse) {
  159. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  160. }
  161. func (tp *tablePrinter) Get(r v3.GetResponse) {
  162. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  163. }
  164. func (tp *tablePrinter) Put(r v3.PutResponse) {
  165. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  166. }
  167. func (tp *tablePrinter) Txn(r v3.TxnResponse) {
  168. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  169. }
  170. func (tp *tablePrinter) Watch(r v3.WatchResponse) {
  171. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  172. }
  173. func (tp *tablePrinter) Alarm(r v3.AlarmResponse) {
  174. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  175. }
  176. func (tp *tablePrinter) MemberList(r v3.MemberListResponse) {
  177. hdr, rows := makeMemberListTable(r)
  178. table := tablewriter.NewWriter(os.Stdout)
  179. table.SetHeader(hdr)
  180. for _, row := range rows {
  181. table.Append(row)
  182. }
  183. table.Render()
  184. }
  185. func (tp *tablePrinter) EndpointStatus(r []epStatus) {
  186. hdr, rows := makeEndpointStatusTable(r)
  187. table := tablewriter.NewWriter(os.Stdout)
  188. table.SetHeader(hdr)
  189. for _, row := range rows {
  190. table.Append(row)
  191. }
  192. table.Render()
  193. }
  194. func (tp *tablePrinter) DBStatus(r dbstatus) {
  195. hdr, rows := makeDBStatusTable(r)
  196. table := tablewriter.NewWriter(os.Stdout)
  197. table.SetHeader(hdr)
  198. for _, row := range rows {
  199. table.Append(row)
  200. }
  201. table.Render()
  202. }
  203. type jsonPrinter struct{}
  204. func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
  205. func (p *jsonPrinter) Get(r v3.GetResponse) { printJSON(r) }
  206. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  207. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  208. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  209. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  210. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  211. func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
  212. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  213. func printJSON(v interface{}) {
  214. b, err := json.Marshal(v)
  215. if err != nil {
  216. fmt.Fprintf(os.Stderr, "%v\n", err)
  217. return
  218. }
  219. fmt.Println(string(b))
  220. }
  221. type pbPrinter struct{}
  222. type pbMarshal interface {
  223. Marshal() ([]byte, error)
  224. }
  225. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  226. printPB((*pb.DeleteRangeResponse)(&r))
  227. }
  228. func (p *pbPrinter) Get(r v3.GetResponse) {
  229. printPB((*pb.RangeResponse)(&r))
  230. }
  231. func (p *pbPrinter) Put(r v3.PutResponse) {
  232. printPB((*pb.PutResponse)(&r))
  233. }
  234. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  235. printPB((*pb.TxnResponse)(&r))
  236. }
  237. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  238. for _, ev := range r.Events {
  239. printPB((*spb.Event)(ev))
  240. }
  241. }
  242. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  243. printPB((*pb.AlarmResponse)(&r))
  244. }
  245. func (p *pbPrinter) MemberList(r v3.MemberListResponse) {
  246. printPB((*pb.MemberListResponse)(&r))
  247. }
  248. func (p *pbPrinter) EndpointStatus(statusList []epStatus) {
  249. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  250. }
  251. func (p *pbPrinter) DBStatus(r dbstatus) {
  252. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  253. }
  254. func printPB(m pbMarshal) {
  255. b, err := m.Marshal()
  256. if err != nil {
  257. fmt.Fprintf(os.Stderr, "%v\n", err)
  258. return
  259. }
  260. fmt.Printf(string(b))
  261. }