Skip to content

Commit

Permalink
Allow rename equals to target config filename on kubecm add (#1060)
Browse files Browse the repository at this point in the history
* fix: cluster and user name conflict on add kubeconfig with same context cluster and user

* fix: cluster and user name conflict on add kubeconfig with same context cluster and user

* fix: allow rename equals to filename and loop check
  • Loading branch information
poneding authored Feb 5, 2025
1 parent 06a00fe commit cae1f84
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -171,17 +170,20 @@ func (kc *KubeConfigOption) handleContexts(oldConfig *clientcmdapi.Config, conte
}
}

if checkContextName(newName, oldConfig) {
var quitNewName bool
for checkContextName(newName, oldConfig) {
nameConfirm := BoolUI(fmt.Sprintf("「%s」 Name already exists, do you want to rename it? (If you select `False`, this context will not be merged)", newName))
if nameConfirm == "True" {
newName = PromptUI("Rename", newName)
if newName == kc.fileName {
return nil, errors.New("need to rename")
}
} else {
continue
} else {
quitNewName = true
break
}
}
if quitNewName {
continue
}
itemConfig := kc.handleContext(oldConfig, newName, ctx)
newConfig = appendConfig(newConfig, itemConfig)
fmt.Printf("Add Context: %s \n", newName)
Expand Down Expand Up @@ -229,7 +231,6 @@ func checkClusterAndUserName(oldConfig *clientcmdapi.Config, newClusterName, new
for _, ctx := range oldConfig.Contexts {
if ctx.Cluster == newClusterName && ctx.AuthInfo == newUserName {
clusterAndUserNameExistInSameContext = true
break
}
if ctx.Cluster == newClusterName {
justClusterNameExist = true
Expand Down

0 comments on commit cae1f84

Please sign in to comment.