printer.go 7.8 KB

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