From 26a69b3908c9f7573ac914bd1caff317a3ba8691 Mon Sep 17 00:00:00 2001 From: Yanqiang Miao Date: Thu, 2 Nov 2017 18:57:28 +0800 Subject: [PATCH] Extract the output codes of status as a util funtion Signed-off-by: Yanqiang Miao --- cmd/crictl/info.go | 34 ++-------------------------------- cmd/crictl/util.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/cmd/crictl/info.go b/cmd/crictl/info.go index 290929ce59..7db4ea6784 100644 --- a/cmd/crictl/info.go +++ b/cmd/crictl/info.go @@ -17,12 +17,9 @@ limitations under the License. package main import ( - "bytes" - "encoding/json" "fmt" "github.com/Sirupsen/logrus" - "github.com/ghodss/yaml" "github.com/urfave/cli" "golang.org/x/net/context" pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" @@ -52,7 +49,7 @@ var runtimeStatusCommand = cli.Command{ // Info sends a StatusRequest to the server, and parses the returned StatusResponse. func Info(cliContext *cli.Context, client pb.RuntimeServiceClient) error { - request := &pb.StatusRequest{} + request := &pb.StatusRequest{Verbose: true} logrus.Debugf("StatusRequest: %v", request) r, err := client.Status(context.Background(), request) logrus.Debugf("StatusResponse: %v", r) @@ -60,32 +57,5 @@ func Info(cliContext *cli.Context, client pb.RuntimeServiceClient) error { return err } - statusByte, err := json.Marshal(r.Status) - if err != nil { - return err - } - jsonInfo := "{" + "\"status\":" + string(statusByte) + "," - for k, v := range r.Info { - jsonInfo += "\"" + k + "\"" + v + "," - } - jsonInfo = jsonInfo[:len(jsonInfo)-1] - jsonInfo += "}" - - switch cliContext.String("output") { - case "yaml": - yamlInfo, err := yaml.JSONToYAML([]byte(jsonInfo)) - if err != nil { - return err - } - fmt.Println(string(yamlInfo)) - case "json": - var output bytes.Buffer - if err := json.Indent(&output, []byte(jsonInfo), "", " "); err != nil { - return err - } - fmt.Println(output.String()) - default: - fmt.Printf("Don't support %q format\n", cliContext.String("output")) - } - return nil + return outputStatusInfo(r.Status, r.Info, cliContext.String("output")) } diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index d89b546a12..1c7f9cf97b 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "bytes" "encoding/json" "fmt" "os" @@ -176,3 +177,34 @@ func outputYAML(v interface{}) error { fmt.Println(string(marshaledYAML)) return nil } + +func outputStatusInfo(status interface{}, info map[string]string, format string) error { + statusByte, err := json.Marshal(status) + if err != nil { + return err + } + jsonInfo := "{" + "\"status\":" + string(statusByte) + "," + for k, v := range info { + jsonInfo += "\"" + k + "\"" + ":" + v + "," + } + jsonInfo = jsonInfo[:len(jsonInfo)-1] + jsonInfo += "}" + + switch format { + case "yaml": + yamlInfo, err := yaml.JSONToYAML([]byte(jsonInfo)) + if err != nil { + return err + } + fmt.Println(string(yamlInfo)) + case "json": + var output bytes.Buffer + if err := json.Indent(&output, []byte(jsonInfo), "", " "); err != nil { + return err + } + fmt.Println(output.String()) + default: + fmt.Printf("Don't support %q format\n", format) + } + return nil +}