Skip to content

Commit

Permalink
feat(cmd): add non-interactively mode to rename command (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 authored Aug 1, 2024
1 parent 62cdb68 commit 90d08f1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ jobs:
export KUBECONFIG="$PWD/multi.config"
bin/kubecm s kind-2nd-kind
echo "********************************************************************************"
echo "Running kubecm rename..."
echo "********************************************************************************"
bin/kubecm rename kind-2nd-kind kind-2nd-kind-new
echo "********************************************************************************"
echo "Running kubecm create..."
echo "********************************************************************************"
bin/kubecm create --user e2e --namespace default --cluster-role view --context-name kind-2nd-kind
Expand Down
44 changes: 35 additions & 9 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,27 @@ func (rc *RenameCommand) runRename(command *cobra.Command, args []string) error
kubeItems = append([]Needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...)
}
}
// exit option
kubeItems, err = ExitOption(kubeItems)
if err != nil {
return err
var kubeName string
var rename string
// args option
if len(args) > 0 {
err = checkRenameArgs(args, kubeItems)
if err != nil {
return err
}
kubeName = args[0]
rename = args[1]
} else {
// exit option
kubeItems, err = ExitOption(kubeItems)
if err != nil {
return err
}
num := SelectUI(kubeItems, "Select The Rename Kube Context")
kubeName = kubeItems[num].Name
rename = PromptUI("Rename", kubeName)
}
num := SelectUI(kubeItems, "Select The Rename Kube Context")
kubeName := kubeItems[num].Name
rename := PromptUI("Rename", kubeName)
config, err = renameComplet(rename, kubeName, config)
config, err = renameComplete(rename, kubeName, config)
if err != nil {
return err
}
Expand All @@ -61,7 +73,7 @@ func (rc *RenameCommand) runRename(command *cobra.Command, args []string) error
return MacNotifier(fmt.Sprintf("Rename [%s] to [%s]\n", kubeName, rename))
}

func renameComplet(rename, kubeName string, config *clientcmdapi.Config) (*clientcmdapi.Config, error) {
func renameComplete(rename, kubeName string, config *clientcmdapi.Config) (*clientcmdapi.Config, error) {
if _, ok := config.Contexts[rename]; ok || rename == kubeName {
return nil, errors.New("Name: " + rename + " already exists")
}
Expand All @@ -75,9 +87,23 @@ func renameComplet(rename, kubeName string, config *clientcmdapi.Config) (*clien
return config, nil
}

func checkRenameArgs(args []string, kubeItems []Needle) error {
if len(args) != 2 {
return errors.New("requires exactly 2 args")
}
for _, item := range kubeItems {
if item.Name == args[0] {
return nil
}
}
return errors.New("Can not find cluster: " + args[0])
}

func renameExample() string {
return `
# Renamed the context interactively
kubecm rename
# Renamed the context non-interactively
kubecm rename <kube-context-name> <new-kube-context-name>
`
}
49 changes: 45 additions & 4 deletions cmd/rename_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -49,7 +50,7 @@ var (
}
)

func Test_renameComplet(t *testing.T) {
func Test_renameComplete(t *testing.T) {
type args struct {
rename string
kubeName string
Expand All @@ -68,17 +69,57 @@ func Test_renameComplet(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := renameComplet(tt.args.rename, tt.args.kubeName, tt.args.config)
got, err := renameComplete(tt.args.rename, tt.args.kubeName, tt.args.config)
if (err != nil) != tt.wantErr {
t.Errorf("renameComplet() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("renameComplete() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("renameComplet() got = %v, want %v", got, tt.want)
t.Errorf("renameComplete() got = %v, want %v", got, tt.want)
}
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
})
}
}

func Test_checkRenameArgs(t *testing.T) {
tests := []struct {
name string
args []string
kubeItems []Needle
wantErr error
}{
{
name: "valid args",
args: []string{"context1", "new-context"},
kubeItems: []Needle{{Name: "context1"}},
wantErr: nil,
},
{
name: "invalid args length",
args: []string{"context1"},
kubeItems: []Needle{{Name: "context1"}},
wantErr: errors.New("requires exactly 2 args"),
},
{
name: "context not found",
args: []string{"context2", "new-context"},
kubeItems: []Needle{{Name: "context1"}},
wantErr: errors.New("Can not find cluster: context2"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkRenameArgs(tt.args, tt.kubeItems)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("checkRenameArgs() error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil && tt.wantErr != nil {
t.Errorf("checkRenameArgs() error = nil, wantErr %v", tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions docs/en-us/cli/kubecm_rename.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ kubecm rename [flags]
# Renamed the context interactively
kubecm rename
# Renamed the context non-interactively
kubecm rename <kube-context-name> <new-kube-context-name>
```

Expand Down

0 comments on commit 90d08f1

Please sign in to comment.