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

Commit

Permalink
etcdctl: add 'simple-value' to print only value
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Aug 26, 2016
1 parent 3a49cbb commit 0024693
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions etcdctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ TODO: add consistency, from, prefix

- Error string if GET failed. Exit code is non-zero.

##### Simple value reply

- \<value\>\n\<next_value\>...

- Error string if GET failed. Exit code is non-zero.

##### JSON reply

The JSON encoding of the [RPC response][etcdrpc] for the GET's Range request.
Expand Down
15 changes: 9 additions & 6 deletions etcdctl/ctlv3/command/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func NewPrinter(printerType string, isHex bool) printer {
switch printerType {
case "simple":
return &simplePrinter{isHex: isHex}
case "simple-value":
return &simplePrinter{isHex: isHex, onlyValue: true}
case "json":
return &jsonPrinter{}
case "protobuf":
Expand Down Expand Up @@ -103,26 +105,27 @@ func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
}

type simplePrinter struct {
isHex bool
isHex bool
onlyValue 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.onlyValue, kv)
}
}

func (s *simplePrinter) Get(resp v3.GetResponse) {
for _, kv := range resp.Kvs {
printKV(s.isHex, kv)
printKV(s.isHex, s.onlyValue, kv)
}
}

func (s *simplePrinter) Put(r v3.PutResponse) {
fmt.Println("OK")
if r.PrevKv != nil {
printKV(s.isHex, r.PrevKv)
printKV(s.isHex, s.onlyValue, r.PrevKv)
}
}

Expand Down Expand Up @@ -152,9 +155,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.onlyValue, e.PrevKv)
}
printKV(s.isHex, e.Kv)
printKV(s.isHex, s.onlyValue, 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 bool, onlyValue 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 !onlyValue {
fmt.Println(k)
}
fmt.Println(v)
}

Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
func init() {
rootCmd.PersistentFlags().StringSliceVar(&globalFlags.Endpoints, "endpoints", []string{"127.0.0.1:2379"}, "gRPC endpoints")

rootCmd.PersistentFlags().StringVarP(&globalFlags.OutputFormat, "write-out", "w", "simple", "set the output format (json, proto, simple, table)")
rootCmd.PersistentFlags().StringVarP(&globalFlags.OutputFormat, "write-out", "w", "simple", "set the output format (simple-value, json, proto, simple, table)")
rootCmd.PersistentFlags().BoolVar(&globalFlags.IsHex, "hex", false, "print byte strings as hex encoded strings")

rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections")
Expand Down

0 comments on commit 0024693

Please sign in to comment.