From 7bb979623e395d2ead627aa3f35a1027b5a9f41c Mon Sep 17 00:00:00 2001 From: Yanqiang Miao Date: Wed, 8 Nov 2017 22:31:54 +0800 Subject: [PATCH] Use `jsonpb` instead of `json` for protobuf object output Signed-off-by: Yanqiang Miao --- cmd/crictl/container.go | 4 ++-- cmd/crictl/image.go | 4 ++-- cmd/crictl/sandbox.go | 4 ++-- cmd/crictl/util.go | 32 ++++++++++++++++++-------------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index c4bc9efa78..3a179d36ba 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -533,9 +533,9 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { switch opts.output { case "json": - return outputJSON(r.Containers) + return outputProtobufObjAsJSON(r) case "yaml": - return outputYAML(r.Containers) + return outputProtobufObjAsYAML(r) } w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 69c724491e..cc6b4fbe69 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -119,9 +119,9 @@ var listImageCommand = cli.Command{ switch context.String("output") { case "json": - return outputJSON(r.Images) + return outputProtobufObjAsJSON(r) case "yaml": - return outputYAML(r.Images) + return outputProtobufObjAsYAML(r) } // output in table format by default. diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index bbac57d1a6..a1947f9833 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -364,9 +364,9 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { switch opts.output { case "json": - return outputJSON(r.Items) + return outputProtobufObjAsJSON(r) case "yaml": - return outputYAML(r.Items) + return outputProtobufObjAsJSON(r) } w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index abca48ce60..f410f5365c 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -165,33 +165,37 @@ func closeConnection(context *cli.Context) error { return conn.Close() } -func outputJSON(v interface{}) error { - marshaledJSON, err := json.MarshalIndent(v, "", " ") +func protobufObjectToJSON(obj proto.Message) (string, error) { + jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "} + marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj) if err != nil { - return err + return "", err } - - fmt.Println(string(marshaledJSON)) - return nil + return marshaledJSON, nil } -func outputYAML(v interface{}) error { - marshaledYAML, err := yaml.Marshal(v) +func outputProtobufObjAsJSON(obj proto.Message) error { + marshaledJSON, err := protobufObjectToJSON(obj) if err != nil { return err } - fmt.Println(string(marshaledYAML)) + fmt.Println(marshaledJSON) return nil } -func protobufObjectToJSON(obj proto.Message) (string, error) { - jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "} - marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj) +func outputProtobufObjAsYAML(obj proto.Message) error { + marshaledJSON, err := protobufObjectToJSON(obj) if err != nil { - return "", err + return err } - return marshaledJSON, nil + marshaledYAML, err := yaml.JSONToYAML([]byte(marshaledJSON)) + if err != nil { + return err + } + + fmt.Println(string(marshaledYAML)) + return nil } func outputStatusInfo(status proto.Message, info map[string]string, format string) error {