add format options to get response printing
@@ -27,6 +27,7 @@ import (
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/spf13/cobra"
"github.com/spf13/viper"
+ "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
@@ -102,10 +103,22 @@ func printGetResponse(address string, response *gnmi.GetResponse) {
if numTargets() > 1 && !viper.GetBool("no-prefix") {
printPrefix = fmt.Sprintf("[%s] ", address)
}
- if viper.GetString("format") == "prototext" {
- fmt.Printf("%s\n", indent(printPrefix, prototext.Format(response)))
+ switch viper.GetString("format") {
+ case "protojson":
+ b, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(response)
+ if err != nil {
+ logger.Printf("error marshaling protojson msg: %v", err)
+ if !viper.GetBool("log") {
+ fmt.Printf("error marshaling protojson msg: %v\n", err)
+ }
return
}
+ fmt.Printf("%s\n", indent(printPrefix, string(b)))
+ return
+ case "prototext":
+ fmt.Printf("%s\n", indent(printPrefix, prototext.Format(response)))
+ return
+ case "json":
for _, notif := range response.Notification {
msg := new(msg)
msg.Source = address
@@ -136,6 +149,10 @@ func printGetResponse(address string, response *gnmi.GetResponse) {
}
fmt.Printf("%s%s\n", printPrefix, string(dMsg))
}
+ case "event":
+ fmt.Println("event format is not implemented for get command")
+ return
+ }
fmt.Println()
}
