Skip to content

Commit

Permalink
fix (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 31, 2020
1 parent 077e7db commit 85972a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"strings"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand All @@ -19,8 +20,8 @@ type ListCommand struct {
func (lc *ListCommand) Init() {
lc.command = &cobra.Command{
Use: "ls",
Short: "List kubeconfig",
Long: "List kubeconfig",
Short: "List KubeConfig",
Long: "List KubeConfig",
Aliases: []string{"l"},
RunE: func(cmd *cobra.Command, args []string) error {
return lc.runList(cmd, args)
Expand All @@ -46,7 +47,8 @@ func (lc *ListCommand) runList(command *cobra.Command, args []string) error {
}
err = ClusterStatus()
if err != nil {
return fmt.Errorf("Cluster check failure!\n%v", err)
printWarning(os.Stdout, "Cluster check failure!\n")
return err
}
return nil
}
Expand Down Expand Up @@ -76,7 +78,7 @@ func filterArgs(args []string, config *clientcmdapi.Config) (*clientcmdapi.Confi

func listExample() string {
return `
# List all the contexts in your kubeconfig file
# List all the contexts in your KubeConfig file
kubecm ls
# Aliases
kubecm l
Expand Down
18 changes: 12 additions & 6 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func PrintTable(config *clientcmdapi.Config) error {
tabulate.SetAlign("center")
fmt.Println(tabulate.Render("grid", "left"))
} else {
return fmt.Errorf("context not found")
return errors.New("context not found")
}
return nil
}
Expand Down Expand Up @@ -195,19 +195,19 @@ func BoolUI(label string) string {
func ClusterStatus() error {
config, err := clientcmd.BuildConfigFromFlags("", cfgFile)
if err != nil {
return fmt.Errorf(err.Error())
return err
}

clientset, err := kubernetes.NewForConfig(config)
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf(err.Error())
return err
}

timeout := int64(5)
ctx := context.TODO()
cus, err := clientset.CoreV1().ComponentStatuses().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
cus, err := clientSet.CoreV1().ComponentStatuses().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
if err != nil {
return fmt.Errorf(err.Error())
return err
}
var names []string
for _, k := range cus.Items {
Expand Down Expand Up @@ -316,6 +316,12 @@ func printString(out io.Writer, name string) {
ct.ResetColor()
}

func printWarning(out io.Writer, name string) {
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Fprint(out, name)
ct.ResetColor()
}

func printComponents(out io.Writer, name string, list []string) {
ct.ChangeColor(ct.Green, false, ct.None, false)
fmt.Fprint(out, name)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module github.com/sunny0826/kubecm

go 1.13
go 1.15

require (
github.com/bndr/gotabulate v1.1.3-0.20170315142410-bc555436bfd5
github.com/daviddengcn/go-colortext v1.0.0
github.com/imdario/mergo v0.3.7
github.com/manifoldco/promptui v0.3.2
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pkg/errors v0.9.1
github.com/rsteube/cobra-zsh-gen v1.1.0
github.com/spf13/cobra v1.0.0
k8s.io/apimachinery v0.19.3
Expand Down

0 comments on commit 85972a3

Please sign in to comment.