printer_simple.go 7.0 KB

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