|
@@ -16,6 +16,7 @@ package command
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "os"
|
|
|
"strconv"
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
|
|
@@ -30,7 +31,7 @@ var (
|
|
|
// NewPutCommand returns the cobra command for "put".
|
|
// NewPutCommand returns the cobra command for "put".
|
|
|
func NewPutCommand() *cobra.Command {
|
|
func NewPutCommand() *cobra.Command {
|
|
|
cmd := &cobra.Command{
|
|
cmd := &cobra.Command{
|
|
|
- Use: "put [options] <key> <value>",
|
|
|
|
|
|
|
+ Use: "put [options] <key> <value> (<value> can also be given from stdin)",
|
|
|
Short: "Put puts the given key into the store.",
|
|
Short: "Put puts the given key into the store.",
|
|
|
Long: `
|
|
Long: `
|
|
|
Put puts the given key into the store.
|
|
Put puts the given key into the store.
|
|
@@ -40,6 +41,11 @@ 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.
|
|
|
|
|
+For example,
|
|
|
|
|
+$ cat file | put <key>
|
|
|
|
|
+will store the content of the file to <key>.
|
|
|
`,
|
|
`,
|
|
|
Run: putCommandFunc,
|
|
Run: putCommandFunc,
|
|
|
}
|
|
}
|
|
@@ -49,8 +55,10 @@ $ put -- <key> <value>
|
|
|
|
|
|
|
|
// putCommandFunc executes the "put" command.
|
|
// putCommandFunc executes the "put" command.
|
|
|
func putCommandFunc(cmd *cobra.Command, args []string) {
|
|
func putCommandFunc(cmd *cobra.Command, args []string) {
|
|
|
- if len(args) != 2 {
|
|
|
|
|
- ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 2 arguments."))
|
|
|
|
|
|
|
+ key := []byte(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."))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(leaseStr, 16, 64)
|
|
id, err := strconv.ParseInt(leaseStr, 16, 64)
|
|
@@ -58,9 +66,6 @@ func putCommandFunc(cmd *cobra.Command, args []string) {
|
|
|
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
|
|
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- key := []byte(args[0])
|
|
|
|
|
- value := []byte(args[1])
|
|
|
|
|
-
|
|
|
|
|
req := &pb.PutRequest{Key: key, Value: value, Lease: id}
|
|
req := &pb.PutRequest{Key: key, Value: value, Lease: id}
|
|
|
mustClient(cmd).KV.Put(context.Background(), req)
|
|
mustClient(cmd).KV.Put(context.Background(), req)
|
|
|
fmt.Printf("%s %s\n", key, value)
|
|
fmt.Printf("%s %s\n", key, value)
|