printer.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. valueOnly bool
  96. }
  97. func (s *simplePrinter) Del(resp v3.DeleteResponse) {
  98. fmt.Println(resp.Deleted)
  99. for _, kv := range resp.PrevKvs {
  100. printKV(s.isHex, s.valueOnly, kv)
  101. }
  102. }
  103. func (s *simplePrinter) Get(resp v3.GetResponse) {
  104. for _, kv := range resp.Kvs {
  105. printKV(s.isHex, s.valueOnly, kv)
  106. }
  107. }
  108. func (s *simplePrinter) Put(r v3.PutResponse) {
  109. fmt.Println("OK")
  110. if r.PrevKv != nil {
  111. printKV(s.isHex, s.valueOnly, r.PrevKv)
  112. }
  113. }
  114. func (s *simplePrinter) Txn(resp v3.TxnResponse) {
  115. if resp.Succeeded {
  116. fmt.Println("SUCCESS")
  117. } else {
  118. fmt.Println("FAILURE")
  119. }
  120. for _, r := range resp.Responses {
  121. fmt.Println("")
  122. switch v := r.Response.(type) {
  123. case *pb.ResponseOp_ResponseDeleteRange:
  124. s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  125. case *pb.ResponseOp_ResponsePut:
  126. s.Put((v3.PutResponse)(*v.ResponsePut))
  127. case *pb.ResponseOp_ResponseRange:
  128. s.Get(((v3.GetResponse)(*v.ResponseRange)))
  129. default:
  130. fmt.Printf("unexpected response %+v\n", r)
  131. }
  132. }
  133. }
  134. func (s *simplePrinter) Watch(resp v3.WatchResponse) {
  135. for _, e := range resp.Events {
  136. fmt.Println(e.Type)
  137. if e.PrevKv != nil {
  138. printKV(s.isHex, s.valueOnly, e.PrevKv)
  139. }
  140. printKV(s.isHex, s.valueOnly, e.Kv)
  141. }
  142. }
  143. func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
  144. for _, e := range resp.Alarms {
  145. fmt.Printf("%+v\n", e)
  146. }
  147. }
  148. func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
  149. _, rows := makeMemberListTable(resp)
  150. for _, row := range rows {
  151. fmt.Println(strings.Join(row, ", "))
  152. }
  153. }
  154. func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
  155. _, rows := makeEndpointStatusTable(statusList)
  156. for _, row := range rows {
  157. fmt.Println(strings.Join(row, ", "))
  158. }
  159. }
  160. func (s *simplePrinter) DBStatus(ds dbstatus) {
  161. _, rows := makeDBStatusTable(ds)
  162. for _, row := range rows {
  163. fmt.Println(strings.Join(row, ", "))
  164. }
  165. }
  166. type tablePrinter struct{}
  167. func (tp *tablePrinter) Del(r v3.DeleteResponse) {
  168. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  169. }
  170. func (tp *tablePrinter) Get(r v3.GetResponse) {
  171. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  172. }
  173. func (tp *tablePrinter) Put(r v3.PutResponse) {
  174. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  175. }
  176. func (tp *tablePrinter) Txn(r v3.TxnResponse) {
  177. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  178. }
  179. func (tp *tablePrinter) Watch(r v3.WatchResponse) {
  180. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  181. }
  182. func (tp *tablePrinter) Alarm(r v3.AlarmResponse) {
  183. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  184. }
  185. func (tp *tablePrinter) MemberList(r v3.MemberListResponse) {
  186. hdr, rows := makeMemberListTable(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) EndpointStatus(r []epStatus) {
  195. hdr, rows := makeEndpointStatusTable(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. func (tp *tablePrinter) DBStatus(r dbstatus) {
  204. hdr, rows := makeDBStatusTable(r)
  205. table := tablewriter.NewWriter(os.Stdout)
  206. table.SetHeader(hdr)
  207. for _, row := range rows {
  208. table.Append(row)
  209. }
  210. table.Render()
  211. }
  212. type jsonPrinter struct{}
  213. func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
  214. func (p *jsonPrinter) Get(r v3.GetResponse) { printJSON(r) }
  215. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  216. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  217. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  218. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  219. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  220. func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
  221. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  222. func printJSON(v interface{}) {
  223. b, err := json.Marshal(v)
  224. if err != nil {
  225. fmt.Fprintf(os.Stderr, "%v\n", err)
  226. return
  227. }
  228. fmt.Println(string(b))
  229. }
  230. type pbPrinter struct{}
  231. type pbMarshal interface {
  232. Marshal() ([]byte, error)
  233. }
  234. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  235. printPB((*pb.DeleteRangeResponse)(&r))
  236. }
  237. func (p *pbPrinter) Get(r v3.GetResponse) {
  238. printPB((*pb.RangeResponse)(&r))
  239. }
  240. func (p *pbPrinter) Put(r v3.PutResponse) {
  241. printPB((*pb.PutResponse)(&r))
  242. }
  243. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  244. printPB((*pb.TxnResponse)(&r))
  245. }
  246. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  247. for _, ev := range r.Events {
  248. printPB((*spb.Event)(ev))
  249. }
  250. }
  251. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  252. printPB((*pb.AlarmResponse)(&r))
  253. }
  254. func (p *pbPrinter) MemberList(r v3.MemberListResponse) {
  255. printPB((*pb.MemberListResponse)(&r))
  256. }
  257. func (p *pbPrinter) EndpointStatus(statusList []epStatus) {
  258. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  259. }
  260. func (p *pbPrinter) DBStatus(r dbstatus) {
  261. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  262. }
  263. func printPB(m pbMarshal) {
  264. b, err := m.Marshal()
  265. if err != nil {
  266. fmt.Fprintf(os.Stderr, "%v\n", err)
  267. return
  268. }
  269. fmt.Printf(string(b))
  270. }