From 0011a33e7bdce99397339d747224b62382cacff9 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Fri, 26 Aug 2016 13:50:08 -0700 Subject: [PATCH] ctlv3: add 'print-value-only' flag to get command Fix https://github.com/coreos/etcd/issues/6234. --- etcdctl/README.md | 2 ++ etcdctl/ctlv3/command/get_command.go | 9 +++++++++ etcdctl/ctlv3/command/printer.go | 13 +++++++------ etcdctl/ctlv3/command/util.go | 6 ++++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/etcdctl/README.md b/etcdctl/README.md index aa4155d38b18..860d49233f92 100644 --- a/etcdctl/README.md +++ b/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 diff --git a/etcdctl/ctlv3/command/get_command.go b/etcdctl/ctlv3/command/get_command.go index e0257b9dec88..1e8009525e5b 100644 --- a/etcdctl/ctlv3/command/get_command.go +++ b/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, "Print only value when used with write-out=simple") 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) } diff --git a/etcdctl/ctlv3/command/printer.go b/etcdctl/ctlv3/command/printer.go index b19f952632e2..2be8aad2ee9b 100644 --- a/etcdctl/ctlv3/command/printer.go +++ b/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) } } diff --git a/etcdctl/ctlv3/command/util.go b/etcdctl/ctlv3/command/util.go index 6080f46dd7b1..7ca9291358c8 100644 --- a/etcdctl/ctlv3/command/util.go +++ b/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, 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) }