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 protobuf/jsonpb instead of golang json in some cases #178

Merged
merged 1 commit into from
Nov 8, 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
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