Browse Source

etcdctlv3: protobuf formatting

Anthony Romano 9 years ago
parent
commit
386c64be7f
2 changed files with 56 additions and 0 deletions
  1. 20 0
      etcdctlv3/README.md
  2. 36 0
      etcdctlv3/command/printer.go

+ 20 - 0
etcdctlv3/README.md

@@ -23,6 +23,10 @@ PUT assigns the specified value with the specified key. If key already holds a v
 
 The JSON encoding of the PUT [RPC response][etcdrpc].
 
+##### Protobuf reply
+
+The protobuf encoding of the PUT [RPC response][etcdrpc].
+
 #### Examples
 
 ``` bash
@@ -72,6 +76,10 @@ TODO: add consistency, from, prefix
 
 The JSON encoding of the [RPC message][etcdrpc] for a key-value pair for each fetched key-value.
 
+##### Protobuf reply
+
+The protobuf encoding of the [RPC message][etcdrpc] for a key-value pair for each fetched key-value.
+
 #### Examples
 
 ``` bash
@@ -105,6 +113,10 @@ TODO: --prefix, --from
 
 The JSON encoding of the DeleteRange [RPC response][etcdrpc].
 
+##### Protobuf reply
+
+The protobuf encoding of the DeleteRange [RPC response][etcdrpc].
+
 #### Examples
 
 ``` bash
@@ -160,6 +172,10 @@ TODO: non-interactive mode
 
 The JSON encoding of the Txn [RPC response][etcdrpc].
 
+##### Protobuf reply
+
+The protobuf encoding of the Txn [RPC response][etcdrpc].
+
 #### Examples
 
 ``` bash
@@ -217,6 +233,10 @@ watch [options] <key or prefix>\n
 
 The JSON encoding of the [RPC message][storagerpc] for each received Event.
 
+##### Protobuf reply
+
+The protobuf encoding of the [RPC message][storagerpc] for each received Event.
+
 #### Examples
 
 ##### Non-interactive

+ 36 - 0
etcdctlv3/command/printer.go

@@ -21,6 +21,7 @@ import (
 
 	v3 "github.com/coreos/etcd/clientv3"
 	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
+	spb "github.com/coreos/etcd/storage/storagepb"
 )
 
 type printer interface {
@@ -37,6 +38,8 @@ func NewPrinter(printerType string, isHex bool) printer {
 		return &simplePrinter{isHex: isHex}
 	case "json":
 		return &jsonPrinter{}
+	case "protobuf":
+		return &pbPrinter{}
 	}
 	return nil
 }
@@ -108,3 +111,36 @@ func printJSON(v interface{}) {
 	}
 	fmt.Println(string(b))
 }
+
+type pbPrinter struct{}
+
+type pbMarshal interface {
+	Marshal() ([]byte, error)
+}
+
+func (p *pbPrinter) Del(r v3.DeleteResponse) {
+	printPB((*pb.DeleteRangeResponse)(&r))
+}
+func (p *pbPrinter) Get(r v3.GetResponse) {
+	printPB((*pb.RangeResponse)(&r))
+}
+func (p *pbPrinter) Put(r v3.PutResponse) {
+	printPB((*pb.PutResponse)(&r))
+}
+func (p *pbPrinter) Txn(r v3.TxnResponse) {
+	printPB((*pb.TxnResponse)(&r))
+}
+func (p *pbPrinter) Watch(r v3.WatchResponse) {
+	for _, ev := range r.Events {
+		printPB((*spb.Event)(ev))
+	}
+}
+
+func printPB(m pbMarshal) {
+	b, err := m.Marshal()
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "%v\n", err)
+		return
+	}
+	fmt.Printf(string(b))
+}