From 535064924c4ac4429697b68074d493a94e525d96 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 17 Feb 2016 19:34:00 -0800 Subject: [PATCH] etcdctlv3: make PUT command clean and documented --- etcdctlv3/README.md | 44 ++++++++++++++++++++++++++++++++ etcdctlv3/command/global.go | 8 +++--- etcdctlv3/command/put_command.go | 18 +++++++------ 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 etcdctlv3/README.md diff --git a/etcdctlv3/README.md b/etcdctlv3/README.md new file mode 100644 index 000000000..8d21e2dc8 --- /dev/null +++ b/etcdctlv3/README.md @@ -0,0 +1,44 @@ +etcdctl +======== + +## Commands + +### PUT [options] \ \ + +PUT assigns the specified value with the specified key. If key already holds a value, it is overwritten. + +#### Options + +- lease -- lease ID (in hexadecimal) to attach to the key. + +#### Return value + +Simple reply + +- OK if PUT executed correctly. Exit code is zero. + +- Error string if PUT failed. Exit code is non-zero. + +TODO: probably json and binary encoded proto + +#### Examples + +``` bash +./etcdctl PUT foo bar --lease=0x1234abcd +OK +./etcdctl range foo +bar +``` + +#### Notes + +If \ isn't given as command line argument, this command tries to read the value from standard input. + +When \ begins with '-', \ is interpreted as a flag. +Insert '--' for workaround: + +``` bash +./etcdctl put -- +./etcdctl put -- +``` + diff --git a/etcdctlv3/command/global.go b/etcdctlv3/command/global.go index 6d0d814f5..961f3c385 100644 --- a/etcdctlv3/command/global.go +++ b/etcdctlv3/command/global.go @@ -93,13 +93,13 @@ func mustClient(endpoint, cert, key, cacert string) *clientv3.Client { return client } -func argOrStdin(args []string, stdin io.Reader, i int) ([]byte, error) { +func argOrStdin(args []string, stdin io.Reader, i int) (string, error) { if i < len(args) { - return []byte(args[i]), nil + return args[i], nil } bytes, err := ioutil.ReadAll(stdin) if string(bytes) == "" || err != nil { - return nil, errors.New("no available argument and stdin") + return "", errors.New("no available argument and stdin") } - return bytes, nil + return string(bytes), nil } diff --git a/etcdctlv3/command/put_command.go b/etcdctlv3/command/put_command.go index 5334a2918..d36689d58 100644 --- a/etcdctlv3/command/put_command.go +++ b/etcdctlv3/command/put_command.go @@ -21,7 +21,8 @@ import ( "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" - pb "github.com/coreos/etcd/etcdserver/etcdserverpb" + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/lease" ) var ( @@ -42,14 +43,14 @@ Insert '--' for workaround: $ put -- $ put -- -If isn't given, this command tries to read the value from standard input. +If isn't given as command line arguement, this command tries to read the value from standard input. For example, $ cat file | put will store the content of the file to . `, Run: putCommandFunc, } - cmd.Flags().StringVar(&leaseStr, "lease", "0", "lease ID attached to the put key") + cmd.Flags().StringVar(&leaseStr, "lease", "0", "lease ID (in hexadecimal) to attach to the key") return cmd } @@ -59,7 +60,7 @@ func putCommandFunc(cmd *cobra.Command, args []string) { ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 1 argument and input from stdin or 2 arguments.")) } - key := []byte(args[0]) + key := args[0] value, err := argOrStdin(args, os.Stdin, 1) if err != nil { ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 1 argument and input from stdin or 2 arguments.")) @@ -67,13 +68,14 @@ func putCommandFunc(cmd *cobra.Command, args []string) { id, err := strconv.ParseInt(leaseStr, 16, 64) if err != nil { - ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err)) + ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID (%v), expecting ID in Hex", err)) } - req := &pb.PutRequest{Key: key, Value: value, Lease: id} - _, err = mustClientFromCmd(cmd).KV.Put(context.Background(), req) + c := mustClientFromCmd(cmd) + kvapi := clientv3.NewKV(c) + _, err = kvapi.Put(context.TODO(), key, value, clientv3.WithLease(lease.LeaseID(id))) if err != nil { ExitWithError(ExitError, err) } - fmt.Printf("%s %s\n", key, value) + fmt.Println("OK") }