Skip to content

Commit 05ad343

Browse files
authored
Merge pull request #190 from miaoyq/protobuf-obj-using-jsonpb
Use `jsonpb` instead of `json` for protobuf object output
2 parents b42fc3f + 7bb9796 commit 05ad343

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

cmd/crictl/container.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,9 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
533533

534534
switch opts.output {
535535
case "json":
536-
return outputJSON(r.Containers)
536+
return outputProtobufObjAsJSON(r)
537537
case "yaml":
538-
return outputYAML(r.Containers)
538+
return outputProtobufObjAsYAML(r)
539539
}
540540

541541
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)

cmd/crictl/image.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ var listImageCommand = cli.Command{
119119

120120
switch context.String("output") {
121121
case "json":
122-
return outputJSON(r.Images)
122+
return outputProtobufObjAsJSON(r)
123123
case "yaml":
124-
return outputYAML(r.Images)
124+
return outputProtobufObjAsYAML(r)
125125
}
126126

127127
// output in table format by default.

cmd/crictl/sandbox.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
364364

365365
switch opts.output {
366366
case "json":
367-
return outputJSON(r.Items)
367+
return outputProtobufObjAsJSON(r)
368368
case "yaml":
369-
return outputYAML(r.Items)
369+
return outputProtobufObjAsJSON(r)
370370
}
371371

372372
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)

cmd/crictl/util.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,37 @@ func closeConnection(context *cli.Context) error {
165165
return conn.Close()
166166
}
167167

168-
func outputJSON(v interface{}) error {
169-
marshaledJSON, err := json.MarshalIndent(v, "", " ")
168+
func protobufObjectToJSON(obj proto.Message) (string, error) {
169+
jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "}
170+
marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj)
170171
if err != nil {
171-
return err
172+
return "", err
172173
}
173-
174-
fmt.Println(string(marshaledJSON))
175-
return nil
174+
return marshaledJSON, nil
176175
}
177176

178-
func outputYAML(v interface{}) error {
179-
marshaledYAML, err := yaml.Marshal(v)
177+
func outputProtobufObjAsJSON(obj proto.Message) error {
178+
marshaledJSON, err := protobufObjectToJSON(obj)
180179
if err != nil {
181180
return err
182181
}
183182

184-
fmt.Println(string(marshaledYAML))
183+
fmt.Println(marshaledJSON)
185184
return nil
186185
}
187186

188-
func protobufObjectToJSON(obj proto.Message) (string, error) {
189-
jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "}
190-
marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj)
187+
func outputProtobufObjAsYAML(obj proto.Message) error {
188+
marshaledJSON, err := protobufObjectToJSON(obj)
191189
if err != nil {
192-
return "", err
190+
return err
193191
}
194-
return marshaledJSON, nil
192+
marshaledYAML, err := yaml.JSONToYAML([]byte(marshaledJSON))
193+
if err != nil {
194+
return err
195+
}
196+
197+
fmt.Println(string(marshaledYAML))
198+
return nil
195199
}
196200

197201
func outputStatusInfo(status proto.Message, info map[string]string, format string) error {

0 commit comments

Comments
 (0)