printer_fields.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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) MemberList(r v3.MemberListResponse) {
  106. p.hdr(r.Header)
  107. for _, m := range r.Members {
  108. fmt.Println(`"ID" :`, m.ID)
  109. fmt.Printf("\"Name\" : %q\n", m.Name)
  110. for _, u := range m.PeerURLs {
  111. fmt.Printf("\"PeerURL\" : %q\n", u)
  112. }
  113. for _, u := range m.ClientURLs {
  114. fmt.Printf("\"ClientURL\" : %q\n", u)
  115. }
  116. fmt.Println()
  117. }
  118. }
  119. func (p *fieldsPrinter) EndpointHealth(hs []epHealth) {
  120. for _, h := range hs {
  121. fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
  122. fmt.Println(`"Health" :`, h.Health)
  123. fmt.Println(`"Took" :`, h.Took)
  124. fmt.Println(`"Error" :`, h.Error)
  125. fmt.Println()
  126. }
  127. }
  128. func (p *fieldsPrinter) EndpointStatus(eps []epStatus) {
  129. for _, ep := range eps {
  130. p.hdr(ep.Resp.Header)
  131. fmt.Printf("\"Version\" : %q\n", ep.Resp.Version)
  132. fmt.Println(`"DBSize" :`, ep.Resp.DbSize)
  133. fmt.Println(`"Leader" :`, ep.Resp.Leader)
  134. fmt.Println(`"RaftIndex" :`, ep.Resp.RaftIndex)
  135. fmt.Println(`"RaftTerm" :`, ep.Resp.RaftTerm)
  136. fmt.Printf("\"Endpoint\" : %q\n", ep.Ep)
  137. fmt.Println()
  138. }
  139. }
  140. func (p *fieldsPrinter) Alarm(r v3.AlarmResponse) {
  141. p.hdr(r.Header)
  142. for _, a := range r.Alarms {
  143. fmt.Println(`"MemberID" :`, a.MemberID)
  144. fmt.Println(`"AlarmType" :`, a.Alarm)
  145. fmt.Println()
  146. }
  147. }
  148. func (p *fieldsPrinter) DBStatus(r dbstatus) {
  149. fmt.Println(`"Hash" :`, r.Hash)
  150. fmt.Println(`"Revision" :`, r.Revision)
  151. fmt.Println(`"Keys" :`, r.TotalKey)
  152. fmt.Println(`"Size" :`, r.TotalSize)
  153. }
  154. func (p *fieldsPrinter) RoleAdd(role string, r v3.AuthRoleAddResponse) { p.hdr(r.Header) }
  155. func (p *fieldsPrinter) RoleGet(role string, r v3.AuthRoleGetResponse) {
  156. p.hdr(r.Header)
  157. for _, p := range r.Perm {
  158. fmt.Println(`"PermType" : `, p.PermType.String())
  159. fmt.Printf("\"Key\" : %q\n", string(p.Key))
  160. fmt.Printf("\"RangeEnd\" : %q\n", string(p.RangeEnd))
  161. }
  162. }
  163. func (p *fieldsPrinter) RoleDelete(role string, r v3.AuthRoleDeleteResponse) { p.hdr(r.Header) }
  164. func (p *fieldsPrinter) RoleList(r v3.AuthRoleListResponse) {
  165. p.hdr(r.Header)
  166. fmt.Printf(`"Roles" :`)
  167. for _, r := range r.Roles {
  168. fmt.Printf(" %q", r)
  169. }
  170. fmt.Println()
  171. }
  172. func (p *fieldsPrinter) RoleGrantPermission(role string, r v3.AuthRoleGrantPermissionResponse) {
  173. p.hdr(r.Header)
  174. }
  175. func (p *fieldsPrinter) RoleRevokePermission(role string, key string, end string, r v3.AuthRoleRevokePermissionResponse) {
  176. p.hdr(r.Header)
  177. }
  178. func (p *fieldsPrinter) UserAdd(user string, r v3.AuthUserAddResponse) { p.hdr(r.Header) }
  179. func (p *fieldsPrinter) UserChangePassword(r v3.AuthUserChangePasswordResponse) { p.hdr(r.Header) }
  180. func (p *fieldsPrinter) UserGrantRole(user string, role string, r v3.AuthUserGrantRoleResponse) {
  181. p.hdr(r.Header)
  182. }
  183. func (p *fieldsPrinter) UserRevokeRole(user string, role string, r v3.AuthUserRevokeRoleResponse) {
  184. p.hdr(r.Header)
  185. }
  186. func (p *fieldsPrinter) UserDelete(user string, r v3.AuthUserDeleteResponse) { p.hdr(r.Header) }