printer.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. MemberStatus([]statusInfo)
  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", "Is Leader"})
  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. fmt.Sprint(m.IsLeader),
  107. })
  108. }
  109. table.Render()
  110. }
  111. func (s *simplePrinter) MemberStatus(statusList []statusInfo) {
  112. table := tablewriter.NewWriter(os.Stdout)
  113. table.SetHeader([]string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"})
  114. for _, status := range statusList {
  115. table.Append([]string{
  116. fmt.Sprint(status.ep),
  117. fmt.Sprintf("%x", status.resp.Header.MemberId),
  118. fmt.Sprint(status.resp.Version),
  119. fmt.Sprint(humanize.Bytes(uint64(status.resp.DbSize))),
  120. fmt.Sprint(status.resp.Leader == status.resp.Header.MemberId),
  121. fmt.Sprint(status.resp.RaftTerm),
  122. fmt.Sprint(status.resp.RaftIndex),
  123. })
  124. }
  125. table.Render()
  126. }
  127. func (s *simplePrinter) DBStatus(ds dbstatus) {
  128. table := tablewriter.NewWriter(os.Stdout)
  129. table.SetHeader([]string{"hash", "revision", "total keys", "total size"})
  130. table.Append([]string{
  131. fmt.Sprintf("%x", ds.hash),
  132. fmt.Sprint(ds.revision),
  133. fmt.Sprint(ds.totalKey),
  134. humanize.Bytes(uint64(ds.totalSize)),
  135. })
  136. table.Render()
  137. }
  138. type jsonPrinter struct{}
  139. func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
  140. func (p *jsonPrinter) Get(r v3.GetResponse) {
  141. for _, kv := range r.Kvs {
  142. printJSON(kv)
  143. }
  144. }
  145. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  146. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  147. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  148. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  149. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  150. func (p *jsonPrinter) MemberStatus(r []statusInfo) { printJSON(r) }
  151. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  152. func printJSON(v interface{}) {
  153. b, err := json.Marshal(v)
  154. if err != nil {
  155. fmt.Fprintf(os.Stderr, "%v\n", err)
  156. return
  157. }
  158. fmt.Println(string(b))
  159. }
  160. type pbPrinter struct{}
  161. type pbMarshal interface {
  162. Marshal() ([]byte, error)
  163. }
  164. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  165. printPB((*pb.DeleteRangeResponse)(&r))
  166. }
  167. func (p *pbPrinter) Get(r v3.GetResponse) {
  168. printPB((*pb.RangeResponse)(&r))
  169. }
  170. func (p *pbPrinter) Put(r v3.PutResponse) {
  171. printPB((*pb.PutResponse)(&r))
  172. }
  173. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  174. printPB((*pb.TxnResponse)(&r))
  175. }
  176. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  177. for _, ev := range r.Events {
  178. printPB((*spb.Event)(ev))
  179. }
  180. }
  181. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  182. printPB((*pb.AlarmResponse)(&r))
  183. }
  184. func (pb *pbPrinter) MemberList(r v3.MemberListResponse) {
  185. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  186. }
  187. func (pb *pbPrinter) MemberStatus(r []statusInfo) {
  188. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  189. }
  190. func (pb *pbPrinter) DBStatus(r dbstatus) {
  191. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  192. }
  193. func printPB(m pbMarshal) {
  194. b, err := m.Marshal()
  195. if err != nil {
  196. fmt.Fprintf(os.Stderr, "%v\n", err)
  197. return
  198. }
  199. fmt.Printf(string(b))
  200. }