printer_simple.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. "os"
  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. "go.etcd.io/etcd/pkg/types"
  23. )
  24. type simplePrinter struct {
  25. isHex bool
  26. valueOnly bool
  27. }
  28. func (s *simplePrinter) Del(resp v3.DeleteResponse) {
  29. fmt.Println(resp.Deleted)
  30. for _, kv := range resp.PrevKvs {
  31. printKV(s.isHex, s.valueOnly, kv)
  32. }
  33. }
  34. func (s *simplePrinter) Get(resp v3.GetResponse) {
  35. for _, kv := range resp.Kvs {
  36. printKV(s.isHex, s.valueOnly, kv)
  37. }
  38. }
  39. func (s *simplePrinter) Put(r v3.PutResponse) {
  40. fmt.Println("OK")
  41. if r.PrevKv != nil {
  42. printKV(s.isHex, s.valueOnly, r.PrevKv)
  43. }
  44. }
  45. func (s *simplePrinter) Txn(resp v3.TxnResponse) {
  46. if resp.Succeeded {
  47. fmt.Println("SUCCESS")
  48. } else {
  49. fmt.Println("FAILURE")
  50. }
  51. for _, r := range resp.Responses {
  52. fmt.Println("")
  53. switch v := r.Response.(type) {
  54. case *pb.ResponseOp_ResponseDeleteRange:
  55. s.Del((v3.DeleteResponse)(*v.ResponseDeleteRange))
  56. case *pb.ResponseOp_ResponsePut:
  57. s.Put((v3.PutResponse)(*v.ResponsePut))
  58. case *pb.ResponseOp_ResponseRange:
  59. s.Get(((v3.GetResponse)(*v.ResponseRange)))
  60. default:
  61. fmt.Printf("unexpected response %+v\n", r)
  62. }
  63. }
  64. }
  65. func (s *simplePrinter) Watch(resp v3.WatchResponse) {
  66. for _, e := range resp.Events {
  67. fmt.Println(e.Type)
  68. if e.PrevKv != nil {
  69. printKV(s.isHex, s.valueOnly, e.PrevKv)
  70. }
  71. printKV(s.isHex, s.valueOnly, e.Kv)
  72. }
  73. }
  74. func (s *simplePrinter) Grant(resp v3.LeaseGrantResponse) {
  75. fmt.Printf("lease %016x granted with TTL(%ds)\n", resp.ID, resp.TTL)
  76. }
  77. func (s *simplePrinter) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) {
  78. fmt.Printf("lease %016x revoked\n", id)
  79. }
  80. func (s *simplePrinter) KeepAlive(resp v3.LeaseKeepAliveResponse) {
  81. fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL)
  82. }
  83. func (s *simplePrinter) TimeToLive(resp v3.LeaseTimeToLiveResponse, keys bool) {
  84. if resp.GrantedTTL == 0 && resp.TTL == -1 {
  85. fmt.Printf("lease %016x already expired\n", resp.ID)
  86. return
  87. }
  88. txt := fmt.Sprintf("lease %016x granted with TTL(%ds), remaining(%ds)", resp.ID, resp.GrantedTTL, resp.TTL)
  89. if keys {
  90. ks := make([]string, len(resp.Keys))
  91. for i := range resp.Keys {
  92. ks[i] = string(resp.Keys[i])
  93. }
  94. txt += fmt.Sprintf(", attached keys(%v)", ks)
  95. }
  96. fmt.Println(txt)
  97. }
  98. func (s *simplePrinter) Leases(resp v3.LeaseLeasesResponse) {
  99. fmt.Printf("found %d leases\n", len(resp.Leases))
  100. for _, item := range resp.Leases {
  101. fmt.Printf("%016x\n", item.ID)
  102. }
  103. }
  104. func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
  105. for _, e := range resp.Alarms {
  106. fmt.Printf("%+v\n", e)
  107. }
  108. }
  109. func (s *simplePrinter) MemberAdd(r v3.MemberAddResponse) {
  110. fmt.Printf("Member %16x added to cluster %16x\n", r.Member.ID, r.Header.ClusterId)
  111. }
  112. func (s *simplePrinter) MemberRemove(id uint64, r v3.MemberRemoveResponse) {
  113. fmt.Printf("Member %16x removed from cluster %16x\n", id, r.Header.ClusterId)
  114. }
  115. func (s *simplePrinter) MemberUpdate(id uint64, r v3.MemberUpdateResponse) {
  116. fmt.Printf("Member %16x updated in cluster %16x\n", id, r.Header.ClusterId)
  117. }
  118. func (s *simplePrinter) MemberPromote(id uint64, r v3.MemberPromoteResponse) {
  119. fmt.Printf("Member %16x promoted in cluster %16x\n", id, r.Header.ClusterId)
  120. }
  121. func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
  122. _, rows := makeMemberListTable(resp)
  123. for _, row := range rows {
  124. fmt.Println(strings.Join(row, ", "))
  125. }
  126. }
  127. func (s *simplePrinter) EndpointHealth(hs []epHealth) {
  128. for _, h := range hs {
  129. if h.Error == "" {
  130. fmt.Fprintf(os.Stderr, "%s is healthy: successfully committed proposal: took = %v\n", h.Ep, h.Took)
  131. } else {
  132. fmt.Fprintf(os.Stderr, "%s is unhealthy: failed to commit proposal: %v\n", h.Ep, h.Error)
  133. }
  134. }
  135. }
  136. func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
  137. _, rows := makeEndpointStatusTable(statusList)
  138. for _, row := range rows {
  139. fmt.Println(strings.Join(row, ", "))
  140. }
  141. }
  142. func (s *simplePrinter) EndpointHashKV(hashList []epHashKV) {
  143. _, rows := makeEndpointHashKVTable(hashList)
  144. for _, row := range rows {
  145. fmt.Println(strings.Join(row, ", "))
  146. }
  147. }
  148. func (s *simplePrinter) DBStatus(ds snapshot.Status) {
  149. _, rows := makeDBStatusTable(ds)
  150. for _, row := range rows {
  151. fmt.Println(strings.Join(row, ", "))
  152. }
  153. }
  154. func (s *simplePrinter) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) {
  155. fmt.Printf("Leadership transferred from %s to %s\n", types.ID(leader), types.ID(target))
  156. }
  157. func (s *simplePrinter) RoleAdd(role string, r v3.AuthRoleAddResponse) {
  158. fmt.Printf("Role %s created\n", role)
  159. }
  160. func (s *simplePrinter) RoleGet(role string, r v3.AuthRoleGetResponse) {
  161. fmt.Printf("Role %s\n", role)
  162. fmt.Println("KV Read:")
  163. printRange := func(perm *v3.Permission) {
  164. sKey := string(perm.Key)
  165. sRangeEnd := string(perm.RangeEnd)
  166. if sRangeEnd != "\x00" {
  167. fmt.Printf("\t[%s, %s)", sKey, sRangeEnd)
  168. } else {
  169. fmt.Printf("\t[%s, <open ended>", sKey)
  170. }
  171. if v3.GetPrefixRangeEnd(sKey) == sRangeEnd {
  172. fmt.Printf(" (prefix %s)", sKey)
  173. }
  174. fmt.Printf("\n")
  175. }
  176. for _, perm := range r.Perm {
  177. if perm.PermType == v3.PermRead || perm.PermType == v3.PermReadWrite {
  178. if len(perm.RangeEnd) == 0 {
  179. fmt.Printf("\t%s\n", string(perm.Key))
  180. } else {
  181. printRange((*v3.Permission)(perm))
  182. }
  183. }
  184. }
  185. fmt.Println("KV Write:")
  186. for _, perm := range r.Perm {
  187. if perm.PermType == v3.PermWrite || perm.PermType == v3.PermReadWrite {
  188. if len(perm.RangeEnd) == 0 {
  189. fmt.Printf("\t%s\n", string(perm.Key))
  190. } else {
  191. printRange((*v3.Permission)(perm))
  192. }
  193. }
  194. }
  195. }
  196. func (s *simplePrinter) RoleList(r v3.AuthRoleListResponse) {
  197. for _, role := range r.Roles {
  198. fmt.Printf("%s\n", role)
  199. }
  200. }
  201. func (s *simplePrinter) RoleDelete(role string, r v3.AuthRoleDeleteResponse) {
  202. fmt.Printf("Role %s deleted\n", role)
  203. }
  204. func (s *simplePrinter) RoleGrantPermission(role string, r v3.AuthRoleGrantPermissionResponse) {
  205. fmt.Printf("Role %s updated\n", role)
  206. }
  207. func (s *simplePrinter) RoleRevokePermission(role string, key string, end string, r v3.AuthRoleRevokePermissionResponse) {
  208. if len(end) == 0 {
  209. fmt.Printf("Permission of key %s is revoked from role %s\n", key, role)
  210. return
  211. }
  212. if end != "\x00" {
  213. fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", key, end, role)
  214. } else {
  215. fmt.Printf("Permission of range [%s, <open ended> is revoked from role %s\n", key, role)
  216. }
  217. }
  218. func (s *simplePrinter) UserAdd(name string, r v3.AuthUserAddResponse) {
  219. fmt.Printf("User %s created\n", name)
  220. }
  221. func (s *simplePrinter) UserGet(name string, r v3.AuthUserGetResponse) {
  222. fmt.Printf("User: %s\n", name)
  223. fmt.Printf("Roles:")
  224. for _, role := range r.Roles {
  225. fmt.Printf(" %s", role)
  226. }
  227. fmt.Printf("\n")
  228. }
  229. func (s *simplePrinter) UserChangePassword(v3.AuthUserChangePasswordResponse) {
  230. fmt.Println("Password updated")
  231. }
  232. func (s *simplePrinter) UserGrantRole(user string, role string, r v3.AuthUserGrantRoleResponse) {
  233. fmt.Printf("Role %s is granted to user %s\n", role, user)
  234. }
  235. func (s *simplePrinter) UserRevokeRole(user string, role string, r v3.AuthUserRevokeRoleResponse) {
  236. fmt.Printf("Role %s is revoked from user %s\n", role, user)
  237. }
  238. func (s *simplePrinter) UserDelete(user string, r v3.AuthUserDeleteResponse) {
  239. fmt.Printf("User %s deleted\n", user)
  240. }
  241. func (s *simplePrinter) UserList(r v3.AuthUserListResponse) {
  242. for _, user := range r.Users {
  243. fmt.Printf("%s\n", user)
  244. }
  245. }