printer_simple.go 7.7 KB

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