Skip to content

Commit

Permalink
fix log out
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 2, 2019
1 parent 396ac3a commit 3e8c194
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 141 deletions.
25 changes: 17 additions & 8 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"log"
"os"
syaml "sigs.k8s.io/yaml"
"strings"
Expand Down Expand Up @@ -50,21 +51,28 @@ kubecm add -f example.yaml -c
if fileExists(file) {
err := configCheck(file)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(1)
}
cover, _ = cmd.Flags().GetBool("cover")
config, err := getAddConfig(file)
if err != nil {
fmt.Println(err)
Error.Println(err)
}
output := merge2Master(config)
err = WriteConfig(output)
if err != nil {
fmt.Println(err.Error())
Error.Println(err.Error())
} else {
log.Printf("「%s」 add successful!", file)
err = Formatable(nil)
if err != nil {
log.Println(err)
os.Exit(1)
}
}
} else {
fmt.Printf("%s file does not exist", file)
Error.Printf("%s file does not exist", file)
os.Exit(1)
}
},
Expand All @@ -86,14 +94,15 @@ func getAddConfig(kubeconfig string) (*clientcmdapi.Config, error) {
}

if len(config.AuthInfos) != 1 {
fmt.Println("Only support add 1 context.")
//fmt.Println("Only support add 1 context.")
Error.Println("Only support add 1 context.")
os.Exit(-1)
}

name := getName()
err = nameCheck(name)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(-1)
}
suffix := HashSuf(config)
Expand Down Expand Up @@ -192,11 +201,11 @@ func merge2Master(config *clientcmdapi.Config) []byte {

json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
Error.Printf("Unexpected error: %v", err)
}
output, err := syaml.JSONToYAML(json)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
Error.Printf("Unexpected error: %v", err)
}

return output
Expand Down
18 changes: 9 additions & 9 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"github.com/spf13/cobra"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"log"
"os"
)

Expand All @@ -40,7 +41,7 @@ var deleteCmd = &cobra.Command{
}
err = deleteContext(args, config)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(-1)
}
} else {
Expand All @@ -55,19 +56,18 @@ func init() {
}

func deleteContext(ctxs []string, config *clientcmdapi.Config) error {
for key, _ := range config.Contexts {
for _, ctx := range ctxs {
if ctx == key {
delete(config.Contexts, key)
break
}
for _, ctx := range ctxs {
if _, ok := config.Contexts[ctx]; ok {
delete(config.Contexts, ctx)
log.Printf("Context Delete: %s \n", ctx)
} else {
Error.Printf("「%s」do not exit.", ctx)
}
}
err := ModifyKubeConfig(config)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(1)
}
fmt.Printf("Context delete succeeded!\nDelete: %v \n", ctxs)
return nil
}
100 changes: 50 additions & 50 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ package cmd

import (
"fmt"
"github.com/bndr/gotabulate"
"github.com/spf13/cobra"
"os"
)

// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "Displays one or many contexts from the kubeconfig file",
Long: `Displays one or many contexts from the kubeconfig file`,
Short: "Displays one or many contexts from the kubeconfig file(Will be removed in new version, move to kubecm)",
Long: `Displays one or many contexts from the kubeconfig file(Will be removed in new version, move to kubecm)`,
Example: `
# List all the contexts in your kubeconfig file
kubecm get
Expand All @@ -35,6 +34,7 @@ var getCmd = &cobra.Command{
kubecm get my-context
`,
Run: func(cmd *cobra.Command, args []string) {
Warning.Println("This command Will be removed in new version, move to kubecm.")
if len(args) == 0 {
err := Formatable(nil)
if err != nil {
Expand All @@ -60,50 +60,50 @@ func init() {
getCmd.SetArgs([]string{""})
}

func Formatable(args []string) error {
config, err := LoadClientConfig(cfgFile)
if err != nil {
return err
}
var table [][]string
if args == nil {
for key, obj := range config.Contexts {
var tmp []string
if config.CurrentContext == key {
tmp = append(tmp, "*")
} else {
tmp = append(tmp, "")
}
tmp = append(tmp, key)
tmp = append(tmp, obj.Cluster)
tmp = append(tmp, obj.AuthInfo)
tmp = append(tmp, obj.Namespace)
table = append(table, tmp)
}
} else {
for key, obj := range config.Contexts {
var tmp []string
if config.CurrentContext == key {
tmp = append(tmp, "*")
tmp = append(tmp, key)
tmp = append(tmp, obj.Cluster)
tmp = append(tmp, obj.AuthInfo)
tmp = append(tmp, obj.Namespace)
table = append(table, tmp)
}
}
}

if table != nil {
tabulate := gotabulate.Create(table)
tabulate.SetHeaders([]string{"CURRENT", "NAME", "CLUSTER", "USER", "Namespace"})
// Turn On String Wrapping
tabulate.SetWrapStrings(true)
// Render the table
tabulate.SetAlign("center")
fmt.Println(tabulate.Render("grid", "left"))
} else {
return fmt.Errorf("context %v not found", args)
}
return nil
}
//func Formatable(args []string) error {
// config, err := LoadClientConfig(cfgFile)
// if err != nil {
// return err
// }
// var table [][]string
// if args == nil {
// for key, obj := range config.Contexts {
// var tmp []string
// if config.CurrentContext == key {
// tmp = append(tmp, "*")
// } else {
// tmp = append(tmp, "")
// }
// tmp = append(tmp, key)
// tmp = append(tmp, obj.Cluster)
// tmp = append(tmp, obj.AuthInfo)
// tmp = append(tmp, obj.Namespace)
// table = append(table, tmp)
// }
// } else {
// for key, obj := range config.Contexts {
// var tmp []string
// if config.CurrentContext == key {
// tmp = append(tmp, "*")
// tmp = append(tmp, key)
// tmp = append(tmp, obj.Cluster)
// tmp = append(tmp, obj.AuthInfo)
// tmp = append(tmp, obj.Namespace)
// table = append(table, tmp)
// }
// }
// }
//
// if table != nil {
// tabulate := gotabulate.Create(table)
// tabulate.SetHeaders([]string{"CURRENT", "NAME", "CLUSTER", "USER", "Namespace"})
// // Turn On String Wrapping
// tabulate.SetWrapStrings(true)
// // Render the table
// tabulate.SetAlign("center")
// fmt.Println(tabulate.Render("grid", "left"))
// } else {
// return fmt.Errorf("context %v not found", args)
// }
// return nil
//}
15 changes: 11 additions & 4 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"log"
"os"
"sigs.k8s.io/yaml"
"strings"
Expand All @@ -46,8 +47,9 @@ kubecm merge -f test -c
Run: func(cmd *cobra.Command, args []string) {
cover, _ = cmd.Flags().GetBool("cover")
files := listFile(folder)
fmt.Printf("Loading kubeconfig file: %v \n", files)
log.Printf("Loading kubeconfig file: %v \n", files)
var loop []string
var defaultName string
for _, yaml := range files {
config, err := LoadClientConfig(yaml)
if err != nil {
Expand Down Expand Up @@ -85,18 +87,23 @@ kubecm merge -f test -c
}
_ = clientcmd.WriteToFile(configType, commandLineFile.Name())
loop = append(loop, commandLineFile.Name())
log.Printf("Context Add: %s \n", name)
defaultName = name
}
loadingRules := &clientcmd.ClientConfigLoadingRules{
Precedence: loop,
}
mergedConfig, err := loadingRules.Load()
if mergedConfig != nil {
mergedConfig.CurrentContext = defaultName
}
json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
Error.Printf("Unexpected error: %v", err)
}
output, err := yaml.JSONToYAML(json)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
Error.Printf("Unexpected error: %v", err)
}

for _, name := range loop {
Expand All @@ -105,7 +112,7 @@ kubecm merge -f test -c

err = WriteConfig(output)
if err != nil {
fmt.Println(err.Error())
Error.Println(err.Error())
}
},
}
Expand Down
20 changes: 12 additions & 8 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License.
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)

Expand All @@ -37,12 +37,16 @@ kubecm rename -n dev -c
Run: func(cmd *cobra.Command, args []string) {
cover, _ = cmd.Flags().GetBool("cover")
if cover && oldName != "" {
fmt.Println("parameter `-c` and `-n` cannot be set at the same time")
log.Println("parameter `-c` and `-n` cannot be set at the same time")
os.Exit(1)
} else {
config, err := LoadClientConfig(cfgFile)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(-1)
}
if _, ok := config.Contexts[newName]; ok {
Error.Printf("the name:%s is exit.",newName)
os.Exit(-1)
}
if cover {
Expand All @@ -51,7 +55,7 @@ kubecm rename -n dev -c
config.Contexts[newName] = obj
delete(config.Contexts, key)
config.CurrentContext = newName
fmt.Println(fmt.Sprintf("Rename %s to %s", key, newName))
log.Printf("Rename %s to %s", key, newName)
break
}
}
Expand All @@ -63,24 +67,24 @@ kubecm rename -n dev -c
config.CurrentContext = newName
}
} else {
fmt.Println(fmt.Sprintf("Can not find context: %s", oldName))
log.Printf("Can not find context: %s", oldName)
err := Formatable(nil)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(1)
}
os.Exit(-1)
}
}
err = ModifyKubeConfig(config)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(1)
}
}
err := Formatable(nil)
if err != nil {
fmt.Println(err)
Error.Println(err)
os.Exit(1)
}
},
Expand Down
Loading

0 comments on commit 3e8c194

Please sign in to comment.