Skip to content

Commit

Permalink
Use protobuf/jsonpb instead of golang json in some cases
Browse files Browse the repository at this point in the history
Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
  • Loading branch information
Yanqiang Miao committed Nov 7, 2017
1 parent 4e3c997 commit e5f5781
Show file tree
Hide file tree
Showing 5 changed files with 849 additions and 21 deletions.
9 changes: 2 additions & 7 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,8 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID, output string) error {
return err
}

switch output {
case "json":
return outputJSON(r.Status)

case "yaml":
return outputYAML(r.Status)
if output == "json" || output == "yaml" {
return outputStatusInfo(r.Status, r.Info, output)
}

// output in table format by default.
Expand Down Expand Up @@ -538,7 +534,6 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
switch opts.output {
case "json":
return outputJSON(r.Containers)

case "yaml":
return outputYAML(r.Containers)
}
Expand Down
9 changes: 3 additions & 6 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,9 @@ var imageStatusCommand = cli.Command{
return fmt.Errorf("no such image present")
}

switch context.String("output") {
case "json":
return outputJSON(r.Image)

case "yaml":
return outputYAML(r.Image)
output := context.String("output")
if output == "json" || output == "yaml" {
return outputStatusInfo(r.Image, r.Info, output)
}

// output in table format by default.
Expand Down
8 changes: 2 additions & 6 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,8 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string) error {
return err
}

switch output {
case "json":
return outputJSON(r.Status)

case "yaml":
return outputYAML(r.Status)
if output == "json" || output == "yaml" {
return outputStatusInfo(r.Status, r.Info, output)
}

// output in table format by default.
Expand Down
15 changes: 13 additions & 2 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"sort"

"github.com/ghodss/yaml"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/urfave/cli"
"google.golang.org/grpc"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -178,8 +180,17 @@ func outputYAML(v interface{}) error {
return nil
}

func outputStatusInfo(status interface{}, info map[string]string, format string) error {
statusByte, err := json.Marshal(status)
func protobufObjectToJSON(obj proto.Message) (string, error) {
jsonpbMarshaler := jsonpb.Marshaler{EmitDefaults: true, Indent: " "}
marshaledJSON, err := jsonpbMarshaler.MarshalToString(obj)
if err != nil {
return "", err
}
return marshaledJSON, nil
}

func outputStatusInfo(status proto.Message, info map[string]string, format string) error {
statusByte, err := protobufObjectToJSON(status)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit e5f5781

Please sign in to comment.