txn_command.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2015 CoreOS, Inc.
  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. "bufio"
  17. "fmt"
  18. "os"
  19. "strconv"
  20. "strings"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  25. )
  26. // NewTxnCommand returns the CLI command for "txn".
  27. func NewTxnCommand() cli.Command {
  28. return cli.Command{
  29. Name: "txn",
  30. Action: func(c *cli.Context) {
  31. txnCommandFunc(c)
  32. },
  33. }
  34. }
  35. // txnCommandFunc executes the "txn" command.
  36. func txnCommandFunc(c *cli.Context) {
  37. if len(c.Args()) != 0 {
  38. panic("unexpected args")
  39. }
  40. reader := bufio.NewReader(os.Stdin)
  41. next := compareState
  42. txn := &pb.TxnRequest{}
  43. for next != nil {
  44. next = next(txn, reader)
  45. }
  46. conn, err := grpc.Dial(c.GlobalString("endpoint"))
  47. if err != nil {
  48. panic(err)
  49. }
  50. etcd := pb.NewEtcdClient(conn)
  51. resp, err := etcd.Txn(context.Background(), txn)
  52. if err != nil {
  53. fmt.Println(err)
  54. }
  55. if resp.Succeeded {
  56. fmt.Println("executed success request list")
  57. } else {
  58. fmt.Println("executed failure request list")
  59. }
  60. }
  61. type stateFunc func(txn *pb.TxnRequest, r *bufio.Reader) stateFunc
  62. func compareState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  63. fmt.Println("entry comparison[key target expected_result compare_value] (end with empty line):")
  64. line, err := r.ReadString('\n')
  65. if err != nil {
  66. os.Exit(1)
  67. }
  68. if len(line) == 1 {
  69. return successState
  70. }
  71. // remove trialling \n
  72. line = line[:len(line)-1]
  73. c, err := parseCompare(line)
  74. if err != nil {
  75. fmt.Println(err)
  76. os.Exit(1)
  77. }
  78. txn.Compare = append(txn.Compare, c)
  79. return compareState
  80. }
  81. func successState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  82. fmt.Println("entry success request[method key value(end_range)] (end with empty line):")
  83. line, err := r.ReadString('\n')
  84. if err != nil {
  85. os.Exit(1)
  86. }
  87. if len(line) == 1 {
  88. return failureState
  89. }
  90. // remove trialling \n
  91. line = line[:len(line)-1]
  92. ru, err := parseRequestUnion(line)
  93. if err != nil {
  94. fmt.Println(err)
  95. os.Exit(1)
  96. }
  97. txn.Success = append(txn.Success, ru)
  98. return successState
  99. }
  100. func failureState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  101. fmt.Println("entry failure request[method key value(end_range)] (end with empty line):")
  102. line, err := r.ReadString('\n')
  103. if err != nil {
  104. os.Exit(1)
  105. }
  106. if len(line) == 1 {
  107. return nil
  108. }
  109. // remove trialling \n
  110. line = line[:len(line)-1]
  111. ru, err := parseRequestUnion(line)
  112. if err != nil {
  113. fmt.Println(err)
  114. os.Exit(1)
  115. }
  116. txn.Failure = append(txn.Failure, ru)
  117. return failureState
  118. }
  119. func parseRequestUnion(line string) (*pb.RequestUnion, error) {
  120. parts := strings.Split(line, " ")
  121. if len(parts) < 2 {
  122. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  123. }
  124. ru := &pb.RequestUnion{}
  125. key := []byte(parts[1])
  126. switch parts[0] {
  127. case "r", "range":
  128. ru.RequestRange = &pb.RangeRequest{Key: key}
  129. if len(parts) == 3 {
  130. ru.RequestRange.RangeEnd = []byte(parts[2])
  131. }
  132. case "p", "put":
  133. ru.RequestPut = &pb.PutRequest{Key: key, Value: []byte(parts[2])}
  134. case "d", "deleteRange":
  135. ru.RequestDeleteRange = &pb.DeleteRangeRequest{Key: key}
  136. if len(parts) == 3 {
  137. ru.RequestRange.RangeEnd = []byte(parts[2])
  138. }
  139. default:
  140. return nil, fmt.Errorf("invalid txn request: %s", line)
  141. }
  142. return ru, nil
  143. }
  144. func parseCompare(line string) (*pb.Compare, error) {
  145. parts := strings.Split(line, " ")
  146. if len(parts) != 4 {
  147. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  148. }
  149. var err error
  150. c := &pb.Compare{}
  151. c.Key = []byte(parts[0])
  152. switch parts[1] {
  153. case "ver", "version":
  154. c.Target = pb.Compare_VERSION
  155. c.Version, err = strconv.ParseInt(parts[3], 10, 64)
  156. if err != nil {
  157. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  158. }
  159. case "c", "create":
  160. c.Target = pb.Compare_CREATE
  161. c.CreateRevision, err = strconv.ParseInt(parts[3], 10, 64)
  162. if err != nil {
  163. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  164. }
  165. case "m", "mod":
  166. c.Target = pb.Compare_MOD
  167. c.ModRevision, err = strconv.ParseInt(parts[3], 10, 64)
  168. if err != nil {
  169. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  170. }
  171. case "val", "value":
  172. c.Target = pb.Compare_VALUE
  173. c.Value = []byte(parts[3])
  174. default:
  175. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  176. }
  177. switch parts[2] {
  178. case "g", "greater":
  179. c.Result = pb.Compare_GREATER
  180. case "e", "equal":
  181. c.Result = pb.Compare_EQUAL
  182. case "l", "less":
  183. c.Result = pb.Compare_LESS
  184. default:
  185. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  186. }
  187. return c, nil
  188. }