From d73cc47b96110080377f5e0e51025fd789e68943 Mon Sep 17 00:00:00 2001 From: guoxudong Date: Wed, 30 Dec 2020 18:04:09 +0800 Subject: [PATCH 1/2] feature of #18 --- cmd/merge.go | 1 + cmd/switch.go | 56 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/cmd/merge.go b/cmd/merge.go index 4549b81b..e7c68b80 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -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 { diff --git a/cmd/switch.go b/cmd/switch.go index 27380ba8..f4fb862a 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -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" ) @@ -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) }, @@ -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 { @@ -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 ` } From 30b09422a40bef84af98b0b33105282da7ce995f Mon Sep 17 00:00:00 2001 From: guoxudong Date: Wed, 30 Dec 2020 18:59:09 +0800 Subject: [PATCH 2/2] add unit test --- cmd/switch.go | 5 ++-- cmd/switch_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 cmd/switch_test.go diff --git a/cmd/switch.go b/cmd/switch.go index f4fb862a..bb0d6b94 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -64,7 +64,7 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error 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 + "」") + return config, errors.New("cannot find context named 「" + name + "」") } config.CurrentContext = name return config, nil @@ -83,7 +83,7 @@ func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error) // exit option kubeItems, err := ExitOption(kubeItems) if err != nil { - return nil, err + return config, err } num := SelectUI(kubeItems, "Select Kube Context") kubeName := kubeItems[num].Name @@ -91,7 +91,6 @@ func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error) return config, nil } -//TODO need add test //TODO need update docs func switchExample() string { diff --git a/cmd/switch_test.go b/cmd/switch_test.go new file mode 100644 index 00000000..59bea107 --- /dev/null +++ b/cmd/switch_test.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "reflect" + "testing" + + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" +) + +var ( + switchConfig = clientcmdapi.Config{ + AuthInfos: map[string]*clientcmdapi.AuthInfo{ + "black-user": {Token: "black-token"}}, + Clusters: map[string]*clientcmdapi.Cluster{ + "pig-cluster": {Server: "http://pig.org:8080"}}, + Contexts: map[string]*clientcmdapi.Context{ + "root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}}, + } + currentContextSwitchConfig = clientcmdapi.Config{ + AuthInfos: map[string]*clientcmdapi.AuthInfo{ + "black-user": {Token: "black-token"}}, + Clusters: map[string]*clientcmdapi.Cluster{ + "pig-cluster": {Server: "http://pig.org:8080"}}, + Contexts: map[string]*clientcmdapi.Context{ + "root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}}, + CurrentContext: "root-context", + } +) + +func Test_handleQuickSwitch(t *testing.T) { + type args struct { + config *clientcmdapi.Config + name string + } + tests := []struct { + name string + args args + want *clientcmdapi.Config + wantErr bool + }{ + // TODO: Add test cases. + {"exist-context", args{&switchConfig, "root-context"}, ¤tContextSwitchConfig, false}, + {"no-exist-context", args{&switchConfig, "test"}, ¤tContextSwitchConfig, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := handleQuickSwitch(tt.args.config, tt.args.name) + if (err != nil) != tt.wantErr { + t.Errorf("handleQuickSwitch() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("handleQuickSwitch() got = %v, want %v", got, tt.want) + } + }) + } +}