Skip to content

Commit

Permalink
Merge pull request #190 from miaoyq/protobuf-obj-using-jsonpb
Browse files Browse the repository at this point in the history
Use `jsonpb` instead of `json` for protobuf object output
  • Loading branch information
Random-Liu authored Nov 13, 2017
2 parents b42fc3f + 7bb9796 commit 05ad343
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 18 additions & 14 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 05ad343

Please sign in to comment.