Bläddra i källkod

Merge pull request #6273 from gyuho/get-cmd

ctlv3: add 'print-value-only' flag to get command
Gyu-Ho Lee 9 år sedan
förälder
incheckning
396fac416e

+ 9 - 4
e2e/ctl_v3_kv_test.go

@@ -100,18 +100,23 @@ func getFormatTest(cx ctlCtx) {
 	}
 
 	tests := []struct {
-		format string
+		format    string
+		valueOnly bool
 
 		wstr string
 	}{
-		{"simple", "abc"},
-		{"json", `"kvs":[{"key":"YWJj"`},
-		{"protobuf", "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
+		{"simple", false, "abc"},
+		{"simple", true, "123"},
+		{"json", false, `"kvs":[{"key":"YWJj"`},
+		{"protobuf", false, "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"},
 	}
 
 	for i, tt := range tests {
 		cmdArgs := append(cx.PrefixArgs(), "get")
 		cmdArgs = append(cmdArgs, "--write-out="+tt.format)
+		if tt.valueOnly {
+			cmdArgs = append(cmdArgs, "--print-value-only")
+		}
 		cmdArgs = append(cmdArgs, "abc")
 		if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil {
 			cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr)

+ 2 - 0
etcdctl/README.md

@@ -69,6 +69,8 @@ GET gets the key or a range of keys [key, range_end) if `range-end` is given.
 
 - rev -- specify the kv revision
 
+- print-value-only -- print only value when used with write-out=simple
+
 TODO: add consistency, from, prefix
 
 #### Return value

+ 9 - 0
etcdctl/ctlv3/command/get_command.go

@@ -31,6 +31,7 @@ var (
 	getFromKey     bool
 	getRev         int64
 	getKeysOnly    bool
+	printValueOnly bool
 )
 
 // NewGetCommand returns the cobra command for "get".
@@ -49,6 +50,7 @@ func NewGetCommand() *cobra.Command {
 	cmd.Flags().BoolVar(&getFromKey, "from-key", false, "Get keys that are greater than or equal to the given key")
 	cmd.Flags().Int64Var(&getRev, "rev", 0, "Specify the kv revision")
 	cmd.Flags().BoolVar(&getKeysOnly, "keys-only", false, "Get only the keys")
+	cmd.Flags().BoolVar(&printValueOnly, "print-value-only", false, `Only write values when using the "simple" output format`)
 	return cmd
 }
 
@@ -62,6 +64,13 @@ func getCommandFunc(cmd *cobra.Command, args []string) {
 		ExitWithError(ExitError, err)
 	}
 
+	if printValueOnly {
+		dp, simple := (display).(*simplePrinter)
+		if !simple {
+			ExitWithError(ExitBadArgs, fmt.Errorf("print-value-only is only for `--write-out=simple`."))
+		}
+		dp.valueOnly = true
+	}
 	display.Get(*resp)
 }
 

+ 7 - 6
etcdctl/ctlv3/command/printer.go

@@ -103,26 +103,27 @@ func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
 }
 
 type simplePrinter struct {
-	isHex bool
+	isHex     bool
+	valueOnly bool
 }
 
 func (s *simplePrinter) Del(resp v3.DeleteResponse) {
 	fmt.Println(resp.Deleted)
 	for _, kv := range resp.PrevKvs {
-		printKV(s.isHex, kv)
+		printKV(s.isHex, s.valueOnly, kv)
 	}
 }
 
 func (s *simplePrinter) Get(resp v3.GetResponse) {
 	for _, kv := range resp.Kvs {
-		printKV(s.isHex, kv)
+		printKV(s.isHex, s.valueOnly, kv)
 	}
 }
 
 func (s *simplePrinter) Put(r v3.PutResponse) {
 	fmt.Println("OK")
 	if r.PrevKv != nil {
-		printKV(s.isHex, r.PrevKv)
+		printKV(s.isHex, s.valueOnly, r.PrevKv)
 	}
 }
 
@@ -152,9 +153,9 @@ func (s *simplePrinter) Watch(resp v3.WatchResponse) {
 	for _, e := range resp.Events {
 		fmt.Println(e.Type)
 		if e.PrevKv != nil {
-			printKV(s.isHex, e.PrevKv)
+			printKV(s.isHex, s.valueOnly, e.PrevKv)
 		}
-		printKV(s.isHex, e.Kv)
+		printKV(s.isHex, s.valueOnly, e.Kv)
 	}
 }
 

+ 4 - 2
etcdctl/ctlv3/command/util.go

@@ -24,13 +24,15 @@ import (
 	"golang.org/x/net/context"
 )
 
-func printKV(isHex bool, kv *pb.KeyValue) {
+func printKV(isHex bool, valueOnly bool, kv *pb.KeyValue) {
 	k, v := string(kv.Key), string(kv.Value)
 	if isHex {
 		k = addHexPrefix(hex.EncodeToString(kv.Key))
 		v = addHexPrefix(hex.EncodeToString(kv.Value))
 	}
-	fmt.Println(k)
+	if !valueOnly {
+		fmt.Println(k)
+	}
 	fmt.Println(v)
 }