printer.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2016 CoreOS, Inc.
  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. }
  47. return nil
  48. }
  49. type simplePrinter struct {
  50. isHex bool
  51. }
  52. func (s *simplePrinter) Del(resp v3.DeleteResponse) {
  53. fmt.Println(resp.Deleted)
  54. }
  55. func (s *simplePrinter) Get(resp v3.GetResponse) {
  56. for _, kv := range resp.Kvs {
  57. printKV(s.isHex, kv)
  58. }
  59. }
  60. func (s *simplePrinter) Put(r v3.PutResponse) { fmt.Println("OK") }
  61. func (s *simplePrinter) Txn(resp v3.TxnResponse) {
  62. if resp.Succeeded {
  63. fmt.Println("SUCCESS")
  64. } else {
  65. fmt.Println("FAILURE")
  66. }
  67. for _, r := range resp.Responses {
  68. fmt.Println("")
  69. switch v := r.Response.(type) {
  70. case *pb.ResponseUnion_ResponseDeleteRange:
  71. s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  72. case *pb.ResponseUnion_ResponsePut:
  73. s.Put((v3.PutResponse)(*v.ResponsePut))
  74. case *pb.ResponseUnion_ResponseRange:
  75. s.Get(((v3.GetResponse)(*v.ResponseRange)))
  76. default:
  77. fmt.Printf("unexpected response %+v\n", r)
  78. }
  79. }
  80. }
  81. func (s *simplePrinter) Watch(resp v3.WatchResponse) {
  82. for _, e := range resp.Events {
  83. fmt.Println(e.Type)
  84. printKV(s.isHex, e.Kv)
  85. }
  86. }
  87. func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
  88. for _, e := range resp.Alarms {
  89. fmt.Printf("%+v\n", e)
  90. }
  91. }
  92. func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
  93. table := tablewriter.NewWriter(os.Stdout)
  94. table.SetHeader([]string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"})
  95. for _, m := range resp.Members {
  96. status := "started"
  97. if len(m.Name) == 0 {
  98. status = "unstarted"
  99. }
  100. table.Append([]string{
  101. fmt.Sprintf("%x", m.ID),
  102. status,
  103. m.Name,
  104. strings.Join(m.PeerURLs, ","),
  105. strings.Join(m.ClientURLs, ","),
  106. })
  107. }
  108. table.Render()
  109. }
  110. func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
  111. table := tablewriter.NewWriter(os.Stdout)
  112. table.SetHeader([]string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"})
  113. for _, status := range statusList {
  114. table.Append([]string{
  115. fmt.Sprint(status.Ep),
  116. fmt.Sprintf("%x", status.Resp.Header.MemberId),
  117. fmt.Sprint(status.Resp.Version),
  118. fmt.Sprint(humanize.Bytes(uint64(status.Resp.DbSize))),
  119. fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
  120. fmt.Sprint(status.Resp.RaftTerm),
  121. fmt.Sprint(status.Resp.RaftIndex),
  122. })
  123. }
  124. table.Render()
  125. }
  126. func (s *simplePrinter) DBStatus(ds dbstatus) {
  127. table := tablewriter.NewWriter(os.Stdout)
  128. table.SetHeader([]string{"hash", "revision", "total keys", "total size"})
  129. table.Append([]string{
  130. fmt.Sprintf("%x", ds.Hash),
  131. fmt.Sprint(ds.Revision),
  132. fmt.Sprint(ds.TotalKey),
  133. humanize.Bytes(uint64(ds.TotalSize)),
  134. })
  135. table.Render()
  136. }
  137. type jsonPrinter struct{}
  138. func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
  139. func (p *jsonPrinter) Get(r v3.GetResponse) { printJSON(r) }
  140. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  141. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  142. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  143. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  144. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  145. func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
  146. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  147. func printJSON(v interface{}) {
  148. b, err := json.Marshal(v)
  149. if err != nil {
  150. fmt.Fprintf(os.Stderr, "%v\n", err)
  151. return
  152. }
  153. fmt.Println(string(b))
  154. }
  155. type pbPrinter struct{}
  156. type pbMarshal interface {
  157. Marshal() ([]byte, error)
  158. }
  159. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  160. printPB((*pb.DeleteRangeResponse)(&r))
  161. }
  162. func (p *pbPrinter) Get(r v3.GetResponse) {
  163. printPB((*pb.RangeResponse)(&r))
  164. }
  165. func (p *pbPrinter) Put(r v3.PutResponse) {
  166. printPB((*pb.PutResponse)(&r))
  167. }
  168. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  169. printPB((*pb.TxnResponse)(&r))
  170. }
  171. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  172. for _, ev := range r.Events {
  173. printPB((*spb.Event)(ev))
  174. }
  175. }
  176. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  177. printPB((*pb.AlarmResponse)(&r))
  178. }
  179. func (pb *pbPrinter) MemberList(r v3.MemberListResponse) {
  180. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  181. }
  182. func (pb *pbPrinter) EndpointStatus(statusList []epStatus) {
  183. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  184. }
  185. func (pb *pbPrinter) DBStatus(r dbstatus) {
  186. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  187. }
  188. func printPB(m pbMarshal) {
  189. b, err := m.Marshal()
  190. if err != nil {
  191. fmt.Fprintf(os.Stderr, "%v\n", err)
  192. return
  193. }
  194. fmt.Printf(string(b))
  195. }