Skip to content

Commit

Permalink
Merge pull request #175 from miaoyq/extract-help-function
Browse files Browse the repository at this point in the history
Extract the output codes of status as a util function
  • Loading branch information
Random-Liu authored Nov 3, 2017
2 parents 044a560 + 26a69b3 commit 4e3c997
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
34 changes: 2 additions & 32 deletions cmd/crictl/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,40 +49,13 @@ 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)
if err != nil {
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"))
}
32 changes: 32 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"bytes"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -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
}

0 comments on commit 4e3c997

Please sign in to comment.