Skip to content

Commit b08a4b7

Browse files
authored
Merge pull request #178 from miaoyq/using-jsonpb
Use `protobuf/jsonpb` instead of `golang json` in some cases
2 parents d6db0e4 + e5f5781 commit b08a4b7

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
@@ -207,12 +207,9 @@ var imageStatusCommand = cli.Command{
207207
return fmt.Errorf("no such image present")
208208
}
209209

210-
switch context.String("output") {
211-
case "json":
212-
return outputJSON(r.Image)
213-
214-
case "yaml":
215-
return outputYAML(r.Image)
210+
output := context.String("output")
211+
if output == "json" || output == "yaml" {
212+
return outputStatusInfo(r.Image, r.Info, output)
216213
}
217214

218215
// output in table format by default.

cmd/crictl/sandbox.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,8 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string) error {
287287
return err
288288
}
289289

290-
switch output {
291-
case "json":
292-
return outputJSON(r.Status)
293-
294-
case "yaml":
295-
return outputYAML(r.Status)
290+
if output == "json" || output == "yaml" {
291+
return outputStatusInfo(r.Status, r.Info, output)
296292
}
297293

298294
// 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"
@@ -183,8 +185,17 @@ func outputYAML(v interface{}) error {
183185
return nil
184186
}
185187

186-
func outputStatusInfo(status interface{}, info map[string]string, format string) error {
187-
statusByte, err := json.Marshal(status)
188+
func protobufObjectToJSON(obj proto.Message) (string, error) {
189+
jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "}
190+
marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj)
191+
if err != nil {
192+
return "", err
193+
}
194+
return marshaledJSON, nil
195+
}
196+
197+
func outputStatusInfo(status proto.Message, info map[string]string, format string) error {
198+
statusByte, err := protobufObjectToJSON(status)
188199
if err != nil {
189200
return err
190201
}

0 commit comments

Comments
 (0)