printer_fields.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. "fmt"
  17. v3 "go.etcd.io/etcd/clientv3"
  18. "go.etcd.io/etcd/clientv3/snapshot"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. spb "go.etcd.io/etcd/mvcc/mvccpb"
  21. )
  22. type fieldsPrinter struct{ printer }
  23. func (p *fieldsPrinter) kv(pfx string, kv *spb.KeyValue) {
  24. fmt.Printf("\"%sKey\" : %q\n", pfx, string(kv.Key))
  25. fmt.Printf("\"%sCreateRevision\" : %d\n", pfx, kv.CreateRevision)
  26. fmt.Printf("\"%sModRevision\" : %d\n", pfx, kv.ModRevision)
  27. fmt.Printf("\"%sVersion\" : %d\n", pfx, kv.Version)
  28. fmt.Printf("\"%sValue\" : %q\n", pfx, string(kv.Value))
  29. fmt.Printf("\"%sLease\" : %d\n", pfx, kv.Lease)
  30. }
  31. func (p *fieldsPrinter) hdr(h *pb.ResponseHeader) {
  32. fmt.Println(`"ClusterID" :`, h.ClusterId)
  33. fmt.Println(`"MemberID" :`, h.MemberId)
  34. fmt.Println(`"Revision" :`, h.Revision)
  35. fmt.Println(`"RaftTerm" :`, h.RaftTerm)
  36. }
  37. func (p *fieldsPrinter) Del(r v3.DeleteResponse) {
  38. p.hdr(r.Header)
  39. fmt.Println(`"Deleted" :`, r.Deleted)
  40. for _, kv := range r.PrevKvs {
  41. p.kv("Prev", kv)
  42. }
  43. }
  44. func (p *fieldsPrinter) Get(r v3.GetResponse) {
  45. p.hdr(r.Header)
  46. for _, kv := range r.Kvs {
  47. p.kv("", kv)
  48. }
  49. fmt.Println(`"More" :`, r.More)
  50. fmt.Println(`"Count" :`, r.Count)
  51. }
  52. func (p *fieldsPrinter) Put(r v3.PutResponse) {
  53. p.hdr(r.Header)
  54. if r.PrevKv != nil {
  55. p.kv("Prev", r.PrevKv)
  56. }
  57. }
  58. func (p *fieldsPrinter) Txn(r v3.TxnResponse) {
  59. p.hdr(r.Header)
  60. fmt.Println(`"Succeeded" :`, r.Succeeded)
  61. for _, resp := range r.Responses {
  62. switch v := resp.Response.(type) {
  63. case *pb.ResponseOp_ResponseDeleteRange:
  64. p.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  65. case *pb.ResponseOp_ResponsePut:
  66. p.Put((v3.PutResponse)(*v.ResponsePut))
  67. case *pb.ResponseOp_ResponseRange:
  68. p.Get((v3.GetResponse)(*v.ResponseRange))
  69. default:
  70. fmt.Printf("\"Unknown\" : %q\n", fmt.Sprintf("%+v", v))
  71. }
  72. }
  73. }
  74. func (p *fieldsPrinter) Watch(resp v3.WatchResponse) {
  75. p.hdr(&resp.Header)
  76. for _, e := range resp.Events {
  77. fmt.Println(`"Type" :`, e.Type)
  78. if e.PrevKv != nil {
  79. p.kv("Prev", e.PrevKv)
  80. }
  81. p.kv("", e.Kv)
  82. }
  83. }
  84. func (p *fieldsPrinter) Grant(r v3.LeaseGrantResponse) {
  85. p.hdr(r.ResponseHeader)
  86. fmt.Println(`"ID" :`, r.ID)
  87. fmt.Println(`"TTL" :`, r.TTL)
  88. }
  89. func (p *fieldsPrinter) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) {
  90. p.hdr(r.Header)
  91. }
  92. func (p *fieldsPrinter) KeepAlive(r v3.LeaseKeepAliveResponse) {
  93. p.hdr(r.ResponseHeader)
  94. fmt.Println(`"ID" :`, r.ID)
  95. fmt.Println(`"TTL" :`, r.TTL)
  96. }
  97. func (p *fieldsPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) {
  98. p.hdr(r.ResponseHeader)
  99. fmt.Println(`"ID" :`, r.ID)
  100. fmt.Println(`"TTL" :`, r.TTL)
  101. fmt.Println(`"GrantedTTL" :`, r.GrantedTTL)
  102. for _, k := range r.Keys {
  103. fmt.Printf("\"Key\" : %q\n", string(k))
  104. }
  105. }
  106. func (p *fieldsPrinter) Leases(r v3.LeaseLeasesResponse) {
  107. p.hdr(r.ResponseHeader)
  108. for _, item := range r.Leases {
  109. fmt.Println(`"ID" :`, item.ID)
  110. }
  111. }
  112. func (p *fieldsPrinter) MemberList(r v3.MemberListResponse) {
  113. p.hdr(r.Header)
  114. for _, m := range r.Members {
  115. fmt.Println(`"ID" :`, m.ID)
  116. fmt.Printf("\"Name\" : %q\n", m.Name)
  117. for _, u := range m.PeerURLs {
  118. fmt.Printf("\"PeerURL\" : %q\n", u)
  119. }
  120. for _, u := range m.ClientURLs {
  121. fmt.Printf("\"ClientURL\" : %q\n", u)
  122. }
  123. fmt.Println(`"IsLearner" :`, m.IsLearner)
  124. fmt.Println()
  125. }
  126. }
  127. func (p *fieldsPrinter) EndpointHealth(hs []epHealth) {
  128. for _, h := range hs {
  129. fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
  130. fmt.Println(`"Health" :`, h.Health)
  131. fmt.Println(`"Took" :`, h.Took)
  132. fmt.Println(`"Error" :`, h.Error)
  133. fmt.Println()
  134. }
  135. }
  136. func (p *fieldsPrinter) EndpointStatus(eps []epStatus) {
  137. for _, ep := range eps {
  138. p.hdr(ep.Resp.Header)
  139. fmt.Printf("\"Version\" : %q\n", ep.Resp.Version)
  140. fmt.Println(`"DBSize" :`, ep.Resp.DbSize)
  141. fmt.Println(`"Leader" :`, ep.Resp.Leader)
  142. fmt.Println(`"IsLearner" :`, ep.Resp.IsLearner)
  143. fmt.Println(`"RaftIndex" :`, ep.Resp.RaftIndex)
  144. fmt.Println(`"RaftTerm" :`, ep.Resp.RaftTerm)
  145. fmt.Println(`"RaftAppliedIndex" :`, ep.Resp.RaftAppliedIndex)
  146. fmt.Println(`"Errors" :`, ep.Resp.Errors)
  147. fmt.Printf("\"Endpoint\" : %q\n", ep.Ep)
  148. fmt.Println()
  149. }
  150. }
  151. func (p *fieldsPrinter) EndpointHashKV(hs []epHashKV) {
  152. for _, h := range hs {
  153. p.hdr(h.Resp.Header)
  154. fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
  155. fmt.Println(`"Hash" :`, h.Resp.Hash)
  156. fmt.Println()
  157. }
  158. }
  159. func (p *fieldsPrinter) Alarm(r v3.AlarmResponse) {
  160. p.hdr(r.Header)
  161. for _, a := range r.Alarms {
  162. fmt.Println(`"MemberID" :`, a.MemberID)
  163. fmt.Println(`"AlarmType" :`, a.Alarm)
  164. fmt.Println()
  165. }
  166. }
  167. func (p *fieldsPrinter) DBStatus(r snapshot.Status) {
  168. fmt.Println(`"Hash" :`, r.Hash)
  169. fmt.Println(`"Revision" :`, r.Revision)
  170. fmt.Println(`"Keys" :`, r.TotalKey)
  171. fmt.Println(`"Size" :`, r.TotalSize)
  172. }
  173. func (p *fieldsPrinter) RoleAdd(role string, r v3.AuthRoleAddResponse) { p.hdr(r.Header) }
  174. func (p *fieldsPrinter) RoleGet(role string, r v3.AuthRoleGetResponse) {
  175. p.hdr(r.Header)
  176. for _, p := range r.Perm {
  177. fmt.Println(`"PermType" : `, p.PermType.String())
  178. fmt.Printf("\"Key\" : %q\n", string(p.Key))
  179. fmt.Printf("\"RangeEnd\" : %q\n", string(p.RangeEnd))
  180. }
  181. }
  182. func (p *fieldsPrinter) RoleDelete(role string, r v3.AuthRoleDeleteResponse) { p.hdr(r.Header) }
  183. func (p *fieldsPrinter) RoleList(r v3.AuthRoleListResponse) {
  184. p.hdr(r.Header)
  185. fmt.Printf(`"Roles" :`)
  186. for _, r := range r.Roles {
  187. fmt.Printf(" %q", r)
  188. }
  189. fmt.Println()
  190. }
  191. func (p *fieldsPrinter) RoleGrantPermission(role string, r v3.AuthRoleGrantPermissionResponse) {
  192. p.hdr(r.Header)
  193. }
  194. func (p *fieldsPrinter) RoleRevokePermission(role string, key string, end string, r v3.AuthRoleRevokePermissionResponse) {
  195. p.hdr(r.Header)
  196. }
  197. func (p *fieldsPrinter) UserAdd(user string, r v3.AuthUserAddResponse) { p.hdr(r.Header) }
  198. func (p *fieldsPrinter) UserChangePassword(r v3.AuthUserChangePasswordResponse) { p.hdr(r.Header) }
  199. func (p *fieldsPrinter) UserGrantRole(user string, role string, r v3.AuthUserGrantRoleResponse) {
  200. p.hdr(r.Header)
  201. }
  202. func (p *fieldsPrinter) UserRevokeRole(user string, role string, r v3.AuthUserRevokeRoleResponse) {
  203. p.hdr(r.Header)
  204. }
  205. func (p *fieldsPrinter) UserDelete(user string, r v3.AuthUserDeleteResponse) { p.hdr(r.Header) }