From 4bbbb52892472b16cc932cabf4d26f0b9d987619 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Mon, 14 Mar 2016 13:28:05 -0700 Subject: [PATCH] etcdctlv3: define non-interactive txn format to match interactive input Fixes #4559 --- etcdctlv3/README.md | 25 ++++++++++++++++++------- etcdctlv3/command/txn_command.go | 16 +++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/etcdctlv3/README.md b/etcdctlv3/README.md index 2ebc97f97..00c32f251 100644 --- a/etcdctlv3/README.md +++ b/etcdctlv3/README.md @@ -129,16 +129,16 @@ OK ### TXN [options] -TXN applies multiple etcd requests as a single atomic transaction. A transaction consists of list of conditions, a list of requests to apply if all the conditions are true, and a list of requests to apply if any condition is false. +TXN reads multiple etcd requests from standard input and applies them as a single atomic transaction. +A transaction consists of list of conditions, a list of requests to apply if all the conditions are true, and a list of requests to apply if any condition is false. #### Options - hex -- print out keys and values as hex encoded string -- interactive -- input transaction with interactive mode +- interactive -- input transaction with interactive prompting #### Input Format -Interactive mode: ```ebnf ::= * "\n" "\n" "\n" ::= (|||) "\n" @@ -156,8 +156,6 @@ Interactive mode: ::= "\""[0-9]+"\"" ``` -TODO: non-interactive mode - #### Return value ##### Simple reply @@ -178,6 +176,7 @@ The protobuf encoding of the Txn [RPC response][etcdrpc]. #### Examples +txn in interactive mode: ``` bash ./etcdctl txn -i mod("key1") > "0" @@ -194,10 +193,22 @@ OK OK ``` -#### Notes +txn in non-interactive mode: +``` +./etcdctl txn <<<'mod("key1") > "0" -TODO: non-interactive mode +put key1 "overwrote-key1" +put key1 "created-key1" +put key2 "some extra key" + +' +FAILURE + +OK + +OK +```` ### WATCH [options] [key or prefix] diff --git a/etcdctlv3/command/txn_command.go b/etcdctlv3/command/txn_command.go index d0ae629cf..196e808fb 100644 --- a/etcdctlv3/command/txn_command.go +++ b/etcdctlv3/command/txn_command.go @@ -47,18 +47,14 @@ func txnCommandFunc(cmd *cobra.Command, args []string) { ExitWithError(ExitBadArgs, fmt.Errorf("txn command does not accept argument.")) } - if !txnInteractive { - ExitWithError(ExitBadFeature, fmt.Errorf("txn command only supports interactive mode")) - } - reader := bufio.NewReader(os.Stdin) txn := mustClientFromCmd(cmd).Txn(context.Background()) - fmt.Println("compares:") + promptInteractive("compares:") txn.If(readCompares(reader)...) - fmt.Println("success requests (get, put, delete):") + promptInteractive("success requests (get, put, delete):") txn.Then(readOps(reader)...) - fmt.Println("failure requests (get, put, delete):") + promptInteractive("failure requests (get, put, delete):") txn.Else(readOps(reader)...) resp, err := txn.Commit() @@ -69,6 +65,12 @@ func txnCommandFunc(cmd *cobra.Command, args []string) { display.Txn(*resp) } +func promptInteractive(s string) { + if txnInteractive { + fmt.Println(s) + } +} + func readCompares(r *bufio.Reader) (cmps []clientv3.Cmp) { for { line, err := r.ReadString('\n')