Skip to content
/ etcd Public
forked from etcd-io/etcd

Commit

Permalink
ctlv3: add 'print-value-only' flag to get command
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Aug 26, 2016
1 parent c388b2f commit 0011a33
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions etcdctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions etcdctl/ctlv3/command/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
getFromKey bool
getRev int64
getKeysOnly bool
printValueOnly bool
)

// NewGetCommand returns the cobra command for "get".
Expand All @@ -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
}

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

Expand Down
13 changes: 7 additions & 6 deletions etcdctl/ctlv3/command/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 4 additions & 2 deletions etcdctl/ctlv3/command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 0011a33

Please sign in to comment.