Skip to content

Commit e5f5781

Browse files
author
Yanqiang Miao
committed
Use protobuf/jsonpb instead of golang json in some cases
Signed-off-by: Yanqiang Miao <[email protected]>
1 parent 4e3c997 commit e5f5781

File tree

5 files changed

+849
-21
lines changed

5 files changed

+849
-21
lines changed

cmd/crictl/container.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,8 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID, output string) error {
456456
return err
457457
}
458458

459-
switch output {
460-
case "json":
461-
return outputJSON(r.Status)
462-
463-
case "yaml":
464-
return outputYAML(r.Status)
459+
if output == "json" || output == "yaml" {
460+
return outputStatusInfo(r.Status, r.Info, output)
465461
}
466462

467463
// output in table format by default.
@@ -538,7 +534,6 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
538534
switch opts.output {
539535
case "json":
540536
return outputJSON(r.Containers)
541-
542537
case "yaml":
543538
return outputYAML(r.Containers)
544539
}

cmd/crictl/image.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,9 @@ var imageStatusCommand = cli.Command{
212212
return fmt.Errorf("no such image present")
213213
}
214214

215-
switch context.String("output") {
216-
case "json":
217-
return outputJSON(r.Image)
218-
219-
case "yaml":
220-
return outputYAML(r.Image)
215+
output := context.String("output")
216+
if output == "json" || output == "yaml" {
217+
return outputStatusInfo(r.Image, r.Info, output)
221218
}
222219

223220
// output in table format by default.

cmd/crictl/sandbox.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,8 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string) error {
269269
return err
270270
}
271271

272-
switch output {
273-
case "json":
274-
return outputJSON(r.Status)
275-
276-
case "yaml":
277-
return outputYAML(r.Status)
272+
if output == "json" || output == "yaml" {
273+
return outputStatusInfo(r.Status, r.Info, output)
278274
}
279275

280276
// output in table format by default.

cmd/crictl/util.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"sort"
2525

2626
"github.com/ghodss/yaml"
27+
"github.com/golang/protobuf/jsonpb"
28+
"github.com/golang/protobuf/proto"
2729
"github.com/urfave/cli"
2830
"google.golang.org/grpc"
2931
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
@@ -178,8 +180,17 @@ func outputYAML(v interface{}) error {
178180
return nil
179181
}
180182

181-
func outputStatusInfo(status interface{}, info map[string]string, format string) error {
182-
statusByte, err := json.Marshal(status)
183+
func protobufObjectToJSON(obj proto.Message) (string, error) {
184+
jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "}
185+
marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj)
186+
if err != nil {
187+
return "", err
188+
}
189+
return marshaledJSON, nil
190+
}
191+
192+
func outputStatusInfo(status proto.Message, info map[string]string, format string) error {
193+
statusByte, err := protobufObjectToJSON(status)
183194
if err != nil {
184195
return err
185196
}

0 commit comments

Comments
 (0)