printer.go 8.3 KB

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