etcdctlv3: use clientv3 api for txn command

This commit is contained in:
Anthony Romano 2016-02-19 14:46:52 -08:00
parent 67472d1ee0
commit 54d15256e7

View File

@ -23,7 +23,7 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb" "github.com/coreos/etcd/clientv3"
) )
// NewTxnCommand returns the cobra command for "txn". // NewTxnCommand returns the cobra command for "txn".
@ -43,13 +43,15 @@ func txnCommandFunc(cmd *cobra.Command, args []string) {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
next := compareState txn := clientv3.NewKV(mustClientFromCmd(cmd)).Txn(context.Background())
txn := &pb.TxnRequest{} fmt.Println("entry comparison[key target expected_result compare_value] (end with empty line):")
for next != nil { txn.If(readCompares(reader)...)
next = next(txn, reader) fmt.Println("entry success request[method key value(end_range)] (end with empty line):")
} txn.Then(readOps(reader)...)
fmt.Println("entry failure request[method key value(end_range)] (end with empty line):")
txn.Else(readOps(reader)...)
resp, err := mustClientFromCmd(cmd).KV.Txn(context.Background(), txn) resp, err := txn.Commit()
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
@ -60,179 +62,124 @@ func txnCommandFunc(cmd *cobra.Command, args []string) {
} }
} }
type stateFunc func(txn *pb.TxnRequest, r *bufio.Reader) stateFunc func readCompares(r *bufio.Reader) (cmps []clientv3.Cmp) {
for {
func compareState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
fmt.Println("entry comparison[key target expected_result compare_value] (end with empty line):")
line, err := r.ReadString('\n') line, err := r.ReadString('\n')
if err != nil { if err != nil {
ExitWithError(ExitInvalidInput, err) ExitWithError(ExitInvalidInput, err)
} }
if len(line) == 1 { if len(line) == 1 {
return successState break
} }
// remove trialling \n // remove trialling \n
line = line[:len(line)-1] line = line[:len(line)-1]
c, err := parseCompare(line) cmp, err := parseCompare(line)
if err != nil { if err != nil {
ExitWithError(ExitInvalidInput, err) ExitWithError(ExitInvalidInput, err)
} }
cmps = append(cmps, *cmp)
}
txn.Compare = append(txn.Compare, c) return cmps
return compareState
} }
func successState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc { func readOps(r *bufio.Reader) (ops []clientv3.Op) {
fmt.Println("entry success request[method key value(end_range)] (end with empty line):") for {
line, err := r.ReadString('\n') line, err := r.ReadString('\n')
if err != nil { if err != nil {
ExitWithError(ExitInvalidInput, err) ExitWithError(ExitInvalidInput, err)
} }
if len(line) == 1 { if len(line) == 1 {
return failureState break
} }
// remove trialling \n // remove trialling \n
line = line[:len(line)-1] line = line[:len(line)-1]
ru, err := parseRequestUnion(line) op, err := parseRequestUnion(line)
if err != nil { if err != nil {
ExitWithError(ExitInvalidInput, err) ExitWithError(ExitInvalidInput, err)
} }
ops = append(ops, *op)
}
txn.Success = append(txn.Success, ru) return ops
return successState
} }
func failureState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc { func parseRequestUnion(line string) (*clientv3.Op, error) {
fmt.Println("entry failure request[method key value(end_range)] (end with empty line):")
line, err := r.ReadString('\n')
if err != nil {
ExitWithError(ExitInvalidInput, err)
}
if len(line) == 1 {
return nil
}
// remove trialling \n
line = line[:len(line)-1]
ru, err := parseRequestUnion(line)
if err != nil {
ExitWithError(ExitInvalidInput, err)
}
txn.Failure = append(txn.Failure, ru)
return failureState
}
func parseRequestUnion(line string) (*pb.RequestUnion, error) {
parts := strings.Split(line, " ") parts := strings.Split(line, " ")
if len(parts) < 2 { if len(parts) < 2 {
return nil, fmt.Errorf("invalid txn compare request: %s", line) return nil, fmt.Errorf("invalid txn compare request: %s", line)
} }
ru := &pb.RequestUnion{} op := &clientv3.Op{}
key := []byte(parts[1]) key := parts[1]
switch parts[0] { switch parts[0] {
case "r", "range": case "r", "range":
if len(parts) == 3 { if len(parts) == 3 {
ru.Request = &pb.RequestUnion_RequestRange{ *op = clientv3.OpGet(key, clientv3.WithRange(parts[2]))
RequestRange: &pb.RangeRequest{
Key: key,
RangeEnd: []byte(parts[2]),
}}
} else { } else {
ru.Request = &pb.RequestUnion_RequestRange{ *op = clientv3.OpGet(key)
RequestRange: &pb.RangeRequest{
Key: key,
}}
} }
case "p", "put": case "p", "put":
ru.Request = &pb.RequestUnion_RequestPut{ *op = clientv3.OpPut(key, parts[2])
RequestPut: &pb.PutRequest{
Key: key,
Value: []byte(parts[2]),
}}
case "d", "deleteRange": case "d", "deleteRange":
if len(parts) == 3 { if len(parts) == 3 {
ru.Request = &pb.RequestUnion_RequestDeleteRange{ *op = clientv3.OpDelete(key, clientv3.WithRange(parts[2]))
RequestDeleteRange: &pb.DeleteRangeRequest{
Key: key,
RangeEnd: []byte(parts[2]),
}}
} else { } else {
ru.Request = &pb.RequestUnion_RequestDeleteRange{ *op = clientv3.OpDelete(key)
RequestDeleteRange: &pb.DeleteRangeRequest{
Key: key,
}}
} }
default: default:
return nil, fmt.Errorf("invalid txn request: %s", line) return nil, fmt.Errorf("invalid txn request: %s", line)
} }
return ru, nil return op, nil
} }
func parseCompare(line string) (*pb.Compare, error) { func parseCompare(line string) (*clientv3.Cmp, error) {
parts := strings.Split(line, " ") parts := strings.Split(line, " ")
if len(parts) != 4 { if len(parts) != 4 {
return nil, fmt.Errorf("invalid txn compare request: %s", line) return nil, fmt.Errorf("invalid txn compare request: %s", line)
} }
var err error cmpType := ""
c := &pb.Compare{} switch parts[2] {
c.Key = []byte(parts[0]) case "g", "greater":
switch parts[1] { cmpType = ">"
case "ver", "version": case "e", "equal":
tv, _ := c.TargetUnion.(*pb.Compare_Version) cmpType = "="
if tv != nil { case "l", "less":
tv.Version, err = strconv.ParseInt(parts[3], 10, 64) cmpType = "<"
if err != nil {
return nil, fmt.Errorf("invalid txn compare request: %s", line)
}
}
case "c", "create":
tv, _ := c.TargetUnion.(*pb.Compare_CreateRevision)
if tv != nil {
tv.CreateRevision, err = strconv.ParseInt(parts[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid txn compare request: %s", line)
}
}
case "m", "mod":
tv, _ := c.TargetUnion.(*pb.Compare_ModRevision)
if tv != nil {
tv.ModRevision, err = strconv.ParseInt(parts[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid txn compare request: %s", line)
}
}
case "val", "value":
tv, _ := c.TargetUnion.(*pb.Compare_Value)
if tv != nil {
tv.Value = []byte(parts[3])
}
default: default:
return nil, fmt.Errorf("invalid txn compare request: %s", line) return nil, fmt.Errorf("invalid txn compare request: %s", line)
} }
switch parts[2] { var (
case "g", "greater": v int64
c.Result = pb.Compare_GREATER err error
case "e", "equal": cmp clientv3.Cmp
c.Result = pb.Compare_EQUAL )
case "l", "less":
c.Result = pb.Compare_LESS key := parts[0]
default: switch parts[1] {
case "ver", "version":
if v, err = strconv.ParseInt(parts[3], 10, 64); err != nil {
cmp = clientv3.Compare(clientv3.Version(key), cmpType, v)
}
case "c", "create":
if v, err = strconv.ParseInt(parts[3], 10, 64); err != nil {
cmp = clientv3.Compare(clientv3.CreatedRevision(key), cmpType, v)
}
case "m", "mod":
if v, err = strconv.ParseInt(parts[3], 10, 64); err != nil {
cmp = clientv3.Compare(clientv3.ModifiedRevision(key), cmpType, v)
}
case "val", "value":
cmp = clientv3.Compare(clientv3.Value(key), cmpType, parts[3])
}
if err != nil {
return nil, fmt.Errorf("invalid txn compare request: %s", line) return nil, fmt.Errorf("invalid txn compare request: %s", line)
} }
return c, nil
return &cmp, nil
} }