printer.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "errors"
  17. "fmt"
  18. "strings"
  19. v3 "github.com/coreos/etcd/clientv3"
  20. "github.com/dustin/go-humanize"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. )
  23. type printer interface {
  24. Del(v3.DeleteResponse)
  25. Get(v3.GetResponse)
  26. Put(v3.PutResponse)
  27. Txn(v3.TxnResponse)
  28. Watch(v3.WatchResponse)
  29. Grant(r v3.LeaseGrantResponse)
  30. Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse)
  31. KeepAlive(r v3.LeaseKeepAliveResponse)
  32. TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool)
  33. Leases(r v3.LeaseLeasesResponse)
  34. MemberAdd(v3.MemberAddResponse)
  35. MemberRemove(id uint64, r v3.MemberRemoveResponse)
  36. MemberUpdate(id uint64, r v3.MemberUpdateResponse)
  37. MemberList(v3.MemberListResponse)
  38. EndpointHealth([]epHealth)
  39. EndpointStatus([]epStatus)
  40. EndpointHashKV([]epHashKV)
  41. MoveLeader(leader, target uint64, r v3.MoveLeaderResponse)
  42. Alarm(v3.AlarmResponse)
  43. DBStatus(dbstatus)
  44. RoleAdd(role string, r v3.AuthRoleAddResponse)
  45. RoleGet(role string, r v3.AuthRoleGetResponse)
  46. RoleDelete(role string, r v3.AuthRoleDeleteResponse)
  47. RoleList(v3.AuthRoleListResponse)
  48. RoleGrantPermission(role string, r v3.AuthRoleGrantPermissionResponse)
  49. RoleRevokePermission(role string, key string, end string, r v3.AuthRoleRevokePermissionResponse)
  50. UserAdd(user string, r v3.AuthUserAddResponse)
  51. UserGet(user string, r v3.AuthUserGetResponse)
  52. UserList(r v3.AuthUserListResponse)
  53. UserChangePassword(v3.AuthUserChangePasswordResponse)
  54. UserGrantRole(user string, role string, r v3.AuthUserGrantRoleResponse)
  55. UserRevokeRole(user string, role string, r v3.AuthUserRevokeRoleResponse)
  56. UserDelete(user string, r v3.AuthUserDeleteResponse)
  57. }
  58. func NewPrinter(printerType string, isHex bool) printer {
  59. switch printerType {
  60. case "simple":
  61. return &simplePrinter{isHex: isHex}
  62. case "fields":
  63. return &fieldsPrinter{newPrinterUnsupported("fields")}
  64. case "json":
  65. return newJSONPrinter()
  66. case "protobuf":
  67. return newPBPrinter()
  68. case "table":
  69. return &tablePrinter{newPrinterUnsupported("table")}
  70. }
  71. return nil
  72. }
  73. type printerRPC struct {
  74. printer
  75. p func(interface{})
  76. }
  77. func (p *printerRPC) Del(r v3.DeleteResponse) { p.p((*pb.DeleteRangeResponse)(&r)) }
  78. func (p *printerRPC) Get(r v3.GetResponse) { p.p((*pb.RangeResponse)(&r)) }
  79. func (p *printerRPC) Put(r v3.PutResponse) { p.p((*pb.PutResponse)(&r)) }
  80. func (p *printerRPC) Txn(r v3.TxnResponse) { p.p((*pb.TxnResponse)(&r)) }
  81. func (p *printerRPC) Watch(r v3.WatchResponse) { p.p(&r) }
  82. func (p *printerRPC) Grant(r v3.LeaseGrantResponse) { p.p(r) }
  83. func (p *printerRPC) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) { p.p(r) }
  84. func (p *printerRPC) KeepAlive(r v3.LeaseKeepAliveResponse) { p.p(r) }
  85. func (p *printerRPC) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { p.p(&r) }
  86. func (p *printerRPC) Leases(r v3.LeaseLeasesResponse) { p.p(&r) }
  87. func (p *printerRPC) MemberAdd(r v3.MemberAddResponse) { p.p((*pb.MemberAddResponse)(&r)) }
  88. func (p *printerRPC) MemberRemove(id uint64, r v3.MemberRemoveResponse) {
  89. p.p((*pb.MemberRemoveResponse)(&r))
  90. }
  91. func (p *printerRPC) MemberUpdate(id uint64, r v3.MemberUpdateResponse) {
  92. p.p((*pb.MemberUpdateResponse)(&r))
  93. }
  94. func (p *printerRPC) MemberList(r v3.MemberListResponse) { p.p((*pb.MemberListResponse)(&r)) }
  95. func (p *printerRPC) Alarm(r v3.AlarmResponse) { p.p((*pb.AlarmResponse)(&r)) }
  96. func (p *printerRPC) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) {
  97. p.p((*pb.MoveLeaderResponse)(&r))
  98. }
  99. func (p *printerRPC) RoleAdd(_ string, r v3.AuthRoleAddResponse) { p.p((*pb.AuthRoleAddResponse)(&r)) }
  100. func (p *printerRPC) RoleGet(_ string, r v3.AuthRoleGetResponse) { p.p((*pb.AuthRoleGetResponse)(&r)) }
  101. func (p *printerRPC) RoleDelete(_ string, r v3.AuthRoleDeleteResponse) {
  102. p.p((*pb.AuthRoleDeleteResponse)(&r))
  103. }
  104. func (p *printerRPC) RoleList(r v3.AuthRoleListResponse) { p.p((*pb.AuthRoleListResponse)(&r)) }
  105. func (p *printerRPC) RoleGrantPermission(_ string, r v3.AuthRoleGrantPermissionResponse) {
  106. p.p((*pb.AuthRoleGrantPermissionResponse)(&r))
  107. }
  108. func (p *printerRPC) RoleRevokePermission(_ string, _ string, _ string, r v3.AuthRoleRevokePermissionResponse) {
  109. p.p((*pb.AuthRoleRevokePermissionResponse)(&r))
  110. }
  111. func (p *printerRPC) UserAdd(_ string, r v3.AuthUserAddResponse) { p.p((*pb.AuthUserAddResponse)(&r)) }
  112. func (p *printerRPC) UserGet(_ string, r v3.AuthUserGetResponse) { p.p((*pb.AuthUserGetResponse)(&r)) }
  113. func (p *printerRPC) UserList(r v3.AuthUserListResponse) { p.p((*pb.AuthUserListResponse)(&r)) }
  114. func (p *printerRPC) UserChangePassword(r v3.AuthUserChangePasswordResponse) {
  115. p.p((*pb.AuthUserChangePasswordResponse)(&r))
  116. }
  117. func (p *printerRPC) UserGrantRole(_ string, _ string, r v3.AuthUserGrantRoleResponse) {
  118. p.p((*pb.AuthUserGrantRoleResponse)(&r))
  119. }
  120. func (p *printerRPC) UserRevokeRole(_ string, _ string, r v3.AuthUserRevokeRoleResponse) {
  121. p.p((*pb.AuthUserRevokeRoleResponse)(&r))
  122. }
  123. func (p *printerRPC) UserDelete(_ string, r v3.AuthUserDeleteResponse) {
  124. p.p((*pb.AuthUserDeleteResponse)(&r))
  125. }
  126. type printerUnsupported struct{ printerRPC }
  127. func newPrinterUnsupported(n string) printer {
  128. f := func(interface{}) {
  129. ExitWithError(ExitBadFeature, errors.New(n+" not supported as output format"))
  130. }
  131. return &printerUnsupported{printerRPC{nil, f}}
  132. }
  133. func (p *printerUnsupported) EndpointHealth([]epHealth) { p.p(nil) }
  134. func (p *printerUnsupported) EndpointStatus([]epStatus) { p.p(nil) }
  135. func (p *printerUnsupported) EndpointHashKV([]epHashKV) { p.p(nil) }
  136. func (p *printerUnsupported) DBStatus(dbstatus) { p.p(nil) }
  137. func (p *printerUnsupported) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) { p.p(nil) }
  138. func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string) {
  139. hdr = []string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"}
  140. for _, m := range r.Members {
  141. status := "started"
  142. if len(m.Name) == 0 {
  143. status = "unstarted"
  144. }
  145. rows = append(rows, []string{
  146. fmt.Sprintf("%x", m.ID),
  147. status,
  148. m.Name,
  149. strings.Join(m.PeerURLs, ","),
  150. strings.Join(m.ClientURLs, ","),
  151. })
  152. }
  153. return hdr, rows
  154. }
  155. func makeEndpointHealthTable(healthList []epHealth) (hdr []string, rows [][]string) {
  156. hdr = []string{"endpoint", "health", "took", "error"}
  157. for _, h := range healthList {
  158. rows = append(rows, []string{
  159. h.Ep,
  160. fmt.Sprintf("%v", h.Health),
  161. h.Took,
  162. h.Error,
  163. })
  164. }
  165. return hdr, rows
  166. }
  167. func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) {
  168. hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"}
  169. for _, status := range statusList {
  170. rows = append(rows, []string{
  171. status.Ep,
  172. fmt.Sprintf("%x", status.Resp.Header.MemberId),
  173. status.Resp.Version,
  174. humanize.Bytes(uint64(status.Resp.DbSize)),
  175. fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
  176. fmt.Sprint(status.Resp.RaftTerm),
  177. fmt.Sprint(status.Resp.RaftIndex),
  178. })
  179. }
  180. return hdr, rows
  181. }
  182. func makeEndpointHashKVTable(hashList []epHashKV) (hdr []string, rows [][]string) {
  183. hdr = []string{"endpoint", "hash"}
  184. for _, h := range hashList {
  185. rows = append(rows, []string{
  186. h.Ep,
  187. fmt.Sprint(h.Resp.Hash),
  188. })
  189. }
  190. return hdr, rows
  191. }
  192. func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
  193. hdr = []string{"hash", "revision", "total keys", "total size"}
  194. rows = append(rows, []string{
  195. fmt.Sprintf("%x", ds.Hash),
  196. fmt.Sprint(ds.Revision),
  197. fmt.Sprint(ds.TotalKey),
  198. humanize.Bytes(uint64(ds.TotalSize)),
  199. })
  200. return hdr, rows
  201. }