printer.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool)
  34. MemberList(v3.MemberListResponse)
  35. EndpointStatus([]epStatus)
  36. Alarm(v3.AlarmResponse)
  37. DBStatus(dbstatus)
  38. }
  39. func NewPrinter(printerType string, isHex bool) printer {
  40. switch printerType {
  41. case "simple":
  42. return &simplePrinter{isHex: isHex}
  43. case "json":
  44. return &jsonPrinter{}
  45. case "protobuf":
  46. return &pbPrinter{}
  47. case "table":
  48. return &tablePrinter{}
  49. }
  50. return nil
  51. }
  52. func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string) {
  53. hdr = []string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"}
  54. for _, m := range r.Members {
  55. status := "started"
  56. if len(m.Name) == 0 {
  57. status = "unstarted"
  58. }
  59. rows = append(rows, []string{
  60. fmt.Sprintf("%x", m.ID),
  61. status,
  62. m.Name,
  63. strings.Join(m.PeerURLs, ","),
  64. strings.Join(m.ClientURLs, ","),
  65. })
  66. }
  67. return
  68. }
  69. func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) {
  70. hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"}
  71. for _, status := range statusList {
  72. rows = append(rows, []string{
  73. fmt.Sprint(status.Ep),
  74. fmt.Sprintf("%x", status.Resp.Header.MemberId),
  75. fmt.Sprint(status.Resp.Version),
  76. fmt.Sprint(humanize.Bytes(uint64(status.Resp.DbSize))),
  77. fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
  78. fmt.Sprint(status.Resp.RaftTerm),
  79. fmt.Sprint(status.Resp.RaftIndex),
  80. })
  81. }
  82. return
  83. }
  84. func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
  85. hdr = []string{"hash", "revision", "total keys", "total size"}
  86. rows = append(rows, []string{
  87. fmt.Sprintf("%x", ds.Hash),
  88. fmt.Sprint(ds.Revision),
  89. fmt.Sprint(ds.TotalKey),
  90. humanize.Bytes(uint64(ds.TotalSize)),
  91. })
  92. return
  93. }
  94. type simplePrinter struct {
  95. isHex bool
  96. valueOnly bool
  97. }
  98. func (s *simplePrinter) Del(resp v3.DeleteResponse) {
  99. fmt.Println(resp.Deleted)
  100. for _, kv := range resp.PrevKvs {
  101. printKV(s.isHex, s.valueOnly, kv)
  102. }
  103. }
  104. func (s *simplePrinter) Get(resp v3.GetResponse) {
  105. for _, kv := range resp.Kvs {
  106. printKV(s.isHex, s.valueOnly, kv)
  107. }
  108. }
  109. func (s *simplePrinter) Put(r v3.PutResponse) {
  110. fmt.Println("OK")
  111. if r.PrevKv != nil {
  112. printKV(s.isHex, s.valueOnly, r.PrevKv)
  113. }
  114. }
  115. func (s *simplePrinter) Txn(resp v3.TxnResponse) {
  116. if resp.Succeeded {
  117. fmt.Println("SUCCESS")
  118. } else {
  119. fmt.Println("FAILURE")
  120. }
  121. for _, r := range resp.Responses {
  122. fmt.Println("")
  123. switch v := r.Response.(type) {
  124. case *pb.ResponseOp_ResponseDeleteRange:
  125. s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  126. case *pb.ResponseOp_ResponsePut:
  127. s.Put((v3.PutResponse)(*v.ResponsePut))
  128. case *pb.ResponseOp_ResponseRange:
  129. s.Get(((v3.GetResponse)(*v.ResponseRange)))
  130. default:
  131. fmt.Printf("unexpected response %+v\n", r)
  132. }
  133. }
  134. }
  135. func (s *simplePrinter) Watch(resp v3.WatchResponse) {
  136. for _, e := range resp.Events {
  137. fmt.Println(e.Type)
  138. if e.PrevKv != nil {
  139. printKV(s.isHex, s.valueOnly, e.PrevKv)
  140. }
  141. printKV(s.isHex, s.valueOnly, e.Kv)
  142. }
  143. }
  144. func (s *simplePrinter) TimeToLive(resp v3.LeaseTimeToLiveResponse, keys bool) {
  145. txt := fmt.Sprintf("lease %016x granted with TTL(%ds), remaining(%ds)", resp.ID, resp.GrantedTTL, resp.TTL)
  146. if keys {
  147. ks := make([]string, len(resp.Keys))
  148. for i := range resp.Keys {
  149. ks[i] = string(resp.Keys[i])
  150. }
  151. txt += fmt.Sprintf(", attached keys(%v)", ks)
  152. }
  153. fmt.Println(txt)
  154. }
  155. func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
  156. for _, e := range resp.Alarms {
  157. fmt.Printf("%+v\n", e)
  158. }
  159. }
  160. func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
  161. _, rows := makeMemberListTable(resp)
  162. for _, row := range rows {
  163. fmt.Println(strings.Join(row, ", "))
  164. }
  165. }
  166. func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
  167. _, rows := makeEndpointStatusTable(statusList)
  168. for _, row := range rows {
  169. fmt.Println(strings.Join(row, ", "))
  170. }
  171. }
  172. func (s *simplePrinter) DBStatus(ds dbstatus) {
  173. _, rows := makeDBStatusTable(ds)
  174. for _, row := range rows {
  175. fmt.Println(strings.Join(row, ", "))
  176. }
  177. }
  178. type tablePrinter struct{}
  179. func (tp *tablePrinter) Del(r v3.DeleteResponse) {
  180. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  181. }
  182. func (tp *tablePrinter) Get(r v3.GetResponse) {
  183. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  184. }
  185. func (tp *tablePrinter) Put(r v3.PutResponse) {
  186. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  187. }
  188. func (tp *tablePrinter) Txn(r v3.TxnResponse) {
  189. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  190. }
  191. func (tp *tablePrinter) Watch(r v3.WatchResponse) {
  192. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  193. }
  194. func (tp *tablePrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) {
  195. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  196. }
  197. func (tp *tablePrinter) Alarm(r v3.AlarmResponse) {
  198. ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
  199. }
  200. func (tp *tablePrinter) MemberList(r v3.MemberListResponse) {
  201. hdr, rows := makeMemberListTable(r)
  202. table := tablewriter.NewWriter(os.Stdout)
  203. table.SetHeader(hdr)
  204. for _, row := range rows {
  205. table.Append(row)
  206. }
  207. table.Render()
  208. }
  209. func (tp *tablePrinter) EndpointStatus(r []epStatus) {
  210. hdr, rows := makeEndpointStatusTable(r)
  211. table := tablewriter.NewWriter(os.Stdout)
  212. table.SetHeader(hdr)
  213. for _, row := range rows {
  214. table.Append(row)
  215. }
  216. table.Render()
  217. }
  218. func (tp *tablePrinter) DBStatus(r dbstatus) {
  219. hdr, rows := makeDBStatusTable(r)
  220. table := tablewriter.NewWriter(os.Stdout)
  221. table.SetHeader(hdr)
  222. for _, row := range rows {
  223. table.Append(row)
  224. }
  225. table.Render()
  226. }
  227. type jsonPrinter struct{}
  228. func (p *jsonPrinter) Del(r v3.DeleteResponse) { printJSON(r) }
  229. func (p *jsonPrinter) Get(r v3.GetResponse) { printJSON(r) }
  230. func (p *jsonPrinter) Put(r v3.PutResponse) { printJSON(r) }
  231. func (p *jsonPrinter) Txn(r v3.TxnResponse) { printJSON(r) }
  232. func (p *jsonPrinter) Watch(r v3.WatchResponse) { printJSON(r) }
  233. func (p *jsonPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { printJSON(r) }
  234. func (p *jsonPrinter) Alarm(r v3.AlarmResponse) { printJSON(r) }
  235. func (p *jsonPrinter) MemberList(r v3.MemberListResponse) { printJSON(r) }
  236. func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
  237. func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
  238. func printJSON(v interface{}) {
  239. b, err := json.Marshal(v)
  240. if err != nil {
  241. fmt.Fprintf(os.Stderr, "%v\n", err)
  242. return
  243. }
  244. fmt.Println(string(b))
  245. }
  246. type pbPrinter struct{}
  247. type pbMarshal interface {
  248. Marshal() ([]byte, error)
  249. }
  250. func (p *pbPrinter) Del(r v3.DeleteResponse) {
  251. printPB((*pb.DeleteRangeResponse)(&r))
  252. }
  253. func (p *pbPrinter) Get(r v3.GetResponse) {
  254. printPB((*pb.RangeResponse)(&r))
  255. }
  256. func (p *pbPrinter) Put(r v3.PutResponse) {
  257. printPB((*pb.PutResponse)(&r))
  258. }
  259. func (p *pbPrinter) Txn(r v3.TxnResponse) {
  260. printPB((*pb.TxnResponse)(&r))
  261. }
  262. func (p *pbPrinter) Watch(r v3.WatchResponse) {
  263. for _, ev := range r.Events {
  264. printPB((*spb.Event)(ev))
  265. }
  266. }
  267. func (p *pbPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) {
  268. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  269. }
  270. func (p *pbPrinter) Alarm(r v3.AlarmResponse) {
  271. printPB((*pb.AlarmResponse)(&r))
  272. }
  273. func (p *pbPrinter) MemberList(r v3.MemberListResponse) {
  274. printPB((*pb.MemberListResponse)(&r))
  275. }
  276. func (p *pbPrinter) EndpointStatus(statusList []epStatus) {
  277. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  278. }
  279. func (p *pbPrinter) DBStatus(r dbstatus) {
  280. ExitWithError(ExitBadFeature, errors.New("only support simple or json as output format"))
  281. }
  282. func printPB(m pbMarshal) {
  283. b, err := m.Marshal()
  284. if err != nil {
  285. fmt.Fprintf(os.Stderr, "%v\n", err)
  286. return
  287. }
  288. fmt.Printf(string(b))
  289. }