Skip to content

Commit

Permalink
feature of #18
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 30, 2020
1 parent 917191c commit d73cc47
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
files := listFile(folder)
mc.command.Printf("Loading kubeconfig file: %v \n", files)
configs := clientcmdapi.NewConfig()
// TODO 还原合并逻辑,使其与 add 相同
for _, yaml := range files {
config, err := clientcmd.LoadFromFile(yaml)
if err != nil {
Expand Down
56 changes: 48 additions & 8 deletions cmd/switch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cmd

import (
"errors"
"fmt"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -19,6 +24,12 @@ func (sc *SwitchCommand) Init() {
Switch Kube Context interactively
`,
Aliases: []string{"s"},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("no support for more than 1 parameter")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return sc.runSwitch(cmd, args)
},
Expand All @@ -31,6 +42,35 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error
if err != nil {
return err
}
switch len(args) {
case 0:
config, err = handleOperation(config)
if err != nil {
return err
}
case 1:
config, err = handleQuickSwitch(config, args[0])
if err != nil {
return err
}
}
err = WriteConfig(true, cfgFile, config)
if err != nil {
return err
}
fmt.Printf("Switched to context 「%s」\n", config.CurrentContext)
return nil
}

func handleQuickSwitch(config *clientcmdapi.Config, name string) (*clientcmdapi.Config, error) {
if _, ok := config.Contexts[name]; !ok {
return nil, errors.New("cannot find context named 「" + name + "」")
}
config.CurrentContext = name
return config, nil
}

func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error) {
var kubeItems []Needle
current := config.CurrentContext
for key, obj := range config.Contexts {
Expand All @@ -41,24 +81,24 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error
}
}
// exit option
kubeItems, err = ExitOption(kubeItems)
kubeItems, err := ExitOption(kubeItems)
if err != nil {
return err
return nil, err
}
num := SelectUI(kubeItems, "Select Kube Context")
kubeName := kubeItems[num].Name
config.CurrentContext = kubeName
err = WriteConfig(true, cfgFile, config)
if err != nil {
return err
}
sc.command.Printf("Switched to context 「%s」\n", config.CurrentContext)
return nil
return config, nil
}

//TODO need add test
//TODO need update docs

func switchExample() string {
return `
# Switch Kube Context interactively
kubecm switch
# Quick switch Kube Context
kubecm switch dev
`
}

0 comments on commit d73cc47

Please sign in to comment.