Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jsonpb instead of json for protobuf object output #190

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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