mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdctlv3: make PUT command clean and documented
This commit is contained in:
parent
6bfd45a83e
commit
535064924c
44
etcdctlv3/README.md
Normal file
44
etcdctlv3/README.md
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
etcdctl
|
||||||
|
========
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
### PUT [options] \<key\> \<value\>
|
||||||
|
|
||||||
|
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 \<value\> isn't given as command line argument, this command tries to read the value from standard input.
|
||||||
|
|
||||||
|
When \<value\> begins with '-', \<value\> is interpreted as a flag.
|
||||||
|
Insert '--' for workaround:
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
./etcdctl put <key> -- <value>
|
||||||
|
./etcdctl put -- <key> <value>
|
||||||
|
```
|
||||||
|
|
@ -93,13 +93,13 @@ func mustClient(endpoint, cert, key, cacert string) *clientv3.Client {
|
|||||||
return 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) {
|
if i < len(args) {
|
||||||
return []byte(args[i]), nil
|
return args[i], nil
|
||||||
}
|
}
|
||||||
bytes, err := ioutil.ReadAll(stdin)
|
bytes, err := ioutil.ReadAll(stdin)
|
||||||
if string(bytes) == "" || err != nil {
|
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
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,8 @@ 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"
|
||||||
|
"github.com/coreos/etcd/lease"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -42,14 +43,14 @@ Insert '--' for workaround:
|
|||||||
$ put <key> -- <value>
|
$ put <key> -- <value>
|
||||||
$ put -- <key> <value>
|
$ put -- <key> <value>
|
||||||
|
|
||||||
If <value> isn't given, this command tries to read the value from standard input.
|
If <value> isn't given as command line arguement, this command tries to read the value from standard input.
|
||||||
For example,
|
For example,
|
||||||
$ cat file | put <key>
|
$ cat file | put <key>
|
||||||
will store the content of the file to <key>.
|
will store the content of the file to <key>.
|
||||||
`,
|
`,
|
||||||
Run: putCommandFunc,
|
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
|
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."))
|
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)
|
value, err := argOrStdin(args, os.Stdin, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 1 argument and input from stdin or 2 arguments."))
|
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)
|
id, err := strconv.ParseInt(leaseStr, 16, 64)
|
||||||
if err != nil {
|
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}
|
c := mustClientFromCmd(cmd)
|
||||||
_, err = mustClientFromCmd(cmd).KV.Put(context.Background(), req)
|
kvapi := clientv3.NewKV(c)
|
||||||
|
_, err = kvapi.Put(context.TODO(), key, value, clientv3.WithLease(lease.LeaseID(id)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ExitWithError(ExitError, err)
|
ExitWithError(ExitError, err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s %s\n", key, value)
|
fmt.Println("OK")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user