printer.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/storage/storagepb"
  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) {
  140. for _, kv := range r.Kvs {
  141. printJSON(kv)
  142. }
  143. }
  144. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  145. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  146. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  147. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  148. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  149. func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
  150. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  151. func printJSON(v interface{}) {
  152. b, err := json.Marshal(v)
  153. if err != nil {
  154. fmt.Fprintf(os.Stderr, "%v\n", err)
  155. return
  156. }
  157. fmt.Println(string(b))
  158. }
  159. type pbPrinter struct{}
  160. type pbMarshal interface {
  161. Marshal() ([]byte, error)
  162. }
  163. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  164. printPB((*pb.DeleteRangeResponse)(&r))
  165. }
  166. func (p *pbPrinter) Get(r v3.GetResponse) {
  167. printPB((*pb.RangeResponse)(&r))
  168. }
  169. func (p *pbPrinter) Put(r v3.PutResponse) {
  170. printPB((*pb.PutResponse)(&r))
  171. }
  172. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  173. printPB((*pb.TxnResponse)(&r))
  174. }
  175. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  176. for _, ev := range r.Events {
  177. printPB((*spb.Event)(ev))
  178. }
  179. }
  180. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  181. printPB((*pb.AlarmResponse)(&r))
  182. }
  183. func (pb *pbPrinter) MemberList(r v3.MemberListResponse) {
  184. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  185. }
  186. func (pb *pbPrinter) EndpointStatus(statusList []epStatus) {
  187. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  188. }
  189. func (pb *pbPrinter) DBStatus(r dbstatus) {
  190. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  191. }
  192. func printPB(m pbMarshal) {
  193. b, err := m.Marshal()
  194. if err != nil {
  195. fmt.Fprintf(os.Stderr, "%v\n", err)
  196. return
  197. }
  198. fmt.Printf(string(b))
  199. }