Browse Source

ctlv3: add 'print-value-only' flag to get command

Gyu-Ho Lee 9 years ago
parent
commit
524a5a1afb

+ 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
 - rev -- specify the kv revision
 
 
+- print-value-only -- print only value when used with write-out=simple
+
 TODO: add consistency, from, prefix
 TODO: add consistency, from, prefix
 
 
 #### Return value
 #### Return value

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

@@ -31,6 +31,7 @@ var (
 	getFromKey     bool
 	getFromKey     bool
 	getRev         int64
 	getRev         int64
 	getKeysOnly    bool
 	getKeysOnly    bool
+	printValueOnly bool
 )
 )
 
 
 // NewGetCommand returns the cobra command for "get".
 // 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().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().Int64Var(&getRev, "rev", 0, "Specify the kv revision")
 	cmd.Flags().BoolVar(&getKeysOnly, "keys-only", false, "Get only the keys")
 	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
 	return cmd
 }
 }
 
 
@@ -62,6 +64,13 @@ func getCommandFunc(cmd *cobra.Command, args []string) {
 		ExitWithError(ExitError, err)
 		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)
 	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 {
 type simplePrinter struct {
-	isHex bool
+	isHex     bool
+	valueOnly bool
 }
 }
 
 
 func (s *simplePrinter) Del(resp v3.DeleteResponse) {
 func (s *simplePrinter) Del(resp v3.DeleteResponse) {
 	fmt.Println(resp.Deleted)
 	fmt.Println(resp.Deleted)
 	for _, kv := range resp.PrevKvs {
 	for _, kv := range resp.PrevKvs {
-		printKV(s.isHex, kv)
+		printKV(s.isHex, s.valueOnly, kv)
 	}
 	}
 }
 }
 
 
 func (s *simplePrinter) Get(resp v3.GetResponse) {
 func (s *simplePrinter) Get(resp v3.GetResponse) {
 	for _, kv := range resp.Kvs {
 	for _, kv := range resp.Kvs {
-		printKV(s.isHex, kv)
+		printKV(s.isHex, s.valueOnly, kv)
 	}
 	}
 }
 }
 
 
 func (s *simplePrinter) Put(r v3.PutResponse) {
 func (s *simplePrinter) Put(r v3.PutResponse) {
 	fmt.Println("OK")
 	fmt.Println("OK")
 	if r.PrevKv != nil {
 	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 {
 	for _, e := range resp.Events {
 		fmt.Println(e.Type)
 		fmt.Println(e.Type)
 		if e.PrevKv != nil {
 		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"
 	"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)
 	k, v := string(kv.Key), string(kv.Value)
 	if isHex {
 	if isHex {
 		k = addHexPrefix(hex.EncodeToString(kv.Key))
 		k = addHexPrefix(hex.EncodeToString(kv.Key))
 		v = addHexPrefix(hex.EncodeToString(kv.Value))
 		v = addHexPrefix(hex.EncodeToString(kv.Value))
 	}
 	}
-	fmt.Println(k)
+	if !valueOnly {
+		fmt.Println(k)
+	}
 	fmt.Println(v)
 	fmt.Println(v)
 }
 }