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

This commit is contained in:
Gyu-Ho Lee 2016-08-29 12:09:07 -07:00
parent 45079ec6c1
commit 524a5a1afb
4 changed files with 22 additions and 8 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)
}