printer_fields.go 6.4 KB

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