printer.go 7.2 KB

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