Skip to content

Commit

Permalink
add interactively cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 2, 2019
1 parent 1b716ff commit 839ee78
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 81 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ kubecm delete my-context
### Delete context
```bash
# Delete the context interactively
kubecm delete
# Delete the context
kubecm delete my-context
```
### Rename context
```bash
# Renamed the context interactively
kubecm rename
# Renamed dev to test
kubecm rename -o dev -n test
# Renamed current-context name to dev
Expand Down
24 changes: 21 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ var deleteCmd = &cobra.Command{
Short: "Delete the specified context from the kubeconfig",
Long: `Delete the specified context from the kubeconfig`,
Example: `
# Delete the context
kubecm delete my-context
# Delete the context interactively
kubecm delete
# Delete the context
kubecm delete my-context
`,
Run: func(cmd *cobra.Command, args []string) {
config, err := LoadClientConfig(cfgFile)
if len(args) != 0 {
config, err := LoadClientConfig(cfgFile)
if err != nil {
fmt.Println(err)
os.Exit(-1)
Expand All @@ -44,6 +46,22 @@ var deleteCmd = &cobra.Command{
Error.Println(err)
os.Exit(-1)
}
} else if len(args) == 0 {
var kubeItems []needle
for key, obj := range config.Contexts {
if key != config.CurrentContext {
kubeItems = append(kubeItems, needle{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo})
} else {
kubeItems = append([]needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...)
}
}
num := SelectUI(kubeItems, "Select The Delete Kube Context")
kubeName := kubeItems[num].Name
err = deleteContext([]string{kubeName}, config)
if err != nil {
Error.Println(err)
os.Exit(-1)
}
} else {
fmt.Println("Please enter the context you want to delete.")
}
Expand Down
130 changes: 91 additions & 39 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"log"
"os"
Expand All @@ -29,60 +32,89 @@ var renameCmd = &cobra.Command{
Use: "rename",
Short: "Rename the contexts of kubeconfig",
Long: `
# Renamed the context interactively
kubecm rename
# Renamed dev to test
kubecm rename -o dev -n test
# Renamed current-context name to dev
kubecm rename -n dev -c
`,
Run: func(cmd *cobra.Command, args []string) {
cover, _ = cmd.Flags().GetBool("cover")
if cover && oldName != "" {
log.Println("parameter `-c` and `-n` cannot be set at the same time")
os.Exit(1)
} else {
config, err := LoadClientConfig(cfgFile)
config, err := LoadClientConfig(cfgFile)
if newName == "" && oldName == "" {
var kubeItems []needle
for key, obj := range config.Contexts {
if key != config.CurrentContext {
kubeItems = append(kubeItems, needle{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo})
} else {
kubeItems = append([]needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...)
}
}
num := SelectUI(kubeItems, "Select The Rename Kube Context")
kubeName := kubeItems[num].Name
rename := InputStr(kubeName)
fmt.Println(rename)
if obj, ok := config.Contexts[kubeName]; ok {
config.Contexts[rename] = obj
delete(config.Contexts, kubeName)
if config.CurrentContext == kubeName {
config.CurrentContext = rename
}
}
err = ModifyKubeConfig(config)
if err != nil {
Error.Println(err)
os.Exit(-1)
}
if _, ok := config.Contexts[newName]; ok {
Error.Printf("the name:%s is exit.",newName)
os.Exit(-1)
os.Exit(1)
}
if cover {
for key, obj := range config.Contexts {
if current := config.CurrentContext; key == current {
config.Contexts[newName] = obj
delete(config.Contexts, key)
config.CurrentContext = newName
log.Printf("Rename %s to %s", key, newName)
break
}
}
} else {
cover, _ = cmd.Flags().GetBool("cover")
if cover && oldName != "" {
log.Println("parameter `-c` and `-n` cannot be set at the same time")
os.Exit(1)
} else {
if obj, ok := config.Contexts[oldName]; ok {
config.Contexts[newName] = obj
delete(config.Contexts, oldName)
if config.CurrentContext == oldName {
config.CurrentContext = newName
if err != nil {
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 {
for key, obj := range config.Contexts {
if current := config.CurrentContext; key == current {
config.Contexts[newName] = obj
delete(config.Contexts, key)
config.CurrentContext = newName
log.Printf("Rename %s to %s", key, newName)
break
}
}
} else {
log.Printf("Can not find context: %s", oldName)
err := Formatable(nil)
if err != nil {
Error.Println(err)
os.Exit(1)
if obj, ok := config.Contexts[oldName]; ok {
config.Contexts[newName] = obj
delete(config.Contexts, oldName)
if config.CurrentContext == oldName {
config.CurrentContext = newName
}
} else {
log.Printf("Can not find context: %s", oldName)
err := Formatable(nil)
if err != nil {
Error.Println(err)
os.Exit(1)
}
os.Exit(-1)
}
os.Exit(-1)
}
}
err = ModifyKubeConfig(config)
if err != nil {
Error.Println(err)
os.Exit(1)
err = ModifyKubeConfig(config)
if err != nil {
Error.Println(err)
os.Exit(1)
}
}
}
err := Formatable(nil)
err = Formatable(nil)
if err != nil {
Error.Println(err)
os.Exit(1)
Expand All @@ -95,5 +127,25 @@ func init() {
renameCmd.Flags().StringVarP(&oldName, "old", "o", "", "Old context name")
renameCmd.Flags().StringVarP(&newName, "new", "n", "", "New context name")
renameCmd.Flags().BoolP("cover", "c", false, "")
renameCmd.MarkFlagRequired("new")
}

func InputStr(name string) string {
validate := func(input string) error {
if len(input) < 3 {
return errors.New("Context name must have more than 3 characters")
}
return nil
}
prompt := promptui.Prompt{
Label: "Rename",
Validate: validate,
Default: name,
}
result, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
os.Exit(-1)
}
return result
}
77 changes: 38 additions & 39 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type needle struct {
Name string
Cluster string
User string
Center string
}

// switchCmd represents the switch command
Expand All @@ -52,59 +53,24 @@ Switch Kube Context interactively.
Error.Println(err)
os.Exit(-1)
}

var kubeItems []needle
current := config.CurrentContext
for key, obj := range config.Contexts {
if key != current {
kubeItems = append(kubeItems, needle{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo})
} else {
kubeItems = append([]needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo}}, kubeItems...)
kubeItems = append([]needle{{Name: key, Cluster: obj.Cluster, User: obj.AuthInfo, Center: "(*)"}}, kubeItems...)
}
}

templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F63C {{ .Name | red }}",
Inactive: " {{ .Name | cyan }}",
Selected: "\U0001F638 {{ .Name | green }}",
Details: `
--------- Info ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Cluster:" | faint }} {{ .Cluster }}
{{ "User:" | faint }} {{ .User }}`,
}

searcher := func(input string, index int) bool {
pepper := kubeItems[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
}

prompt := promptui.Select{
Label: "Select Kube Context",
Items: kubeItems,
Templates: templates,
Size: 4,
Searcher: searcher,
}

i, _, err := prompt.Run()
kubeName := kubeItems[i].Name
if err != nil {
log.Printf("Prompt failed %v\n", err)
return
}
num := SelectUI(kubeItems, "Select Kube Context")
kubeName := kubeItems[num].Name
config.CurrentContext = kubeName

err = ModifyKubeConfig(config)
if err != nil {
Error.Println(err)
os.Exit(1)
}
log.Printf("Switched to context %s\n", kubeName)
log.Printf("Switched to context 「%s」\n", config.CurrentContext)
err = Formatable(nil)
if err != nil {
Error.Println(err)
Expand Down Expand Up @@ -157,3 +123,36 @@ func ClusterStatus() error {
log.Printf("Cluster check succeeded!\nContains components: %v \n", names)
return nil
}

func SelectUI(kubeItems []needle, label string) int {
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F63C {{ .Name | red }}{{ .Center | red}}",
Inactive: " {{ .Name | cyan }}{{ .Center | red}}",
Selected: "\U0001F638 {{ .Name | green }}",
Details: `
--------- Info ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Cluster:" | faint }} {{ .Cluster }}
{{ "User:" | faint }} {{ .User }}`,
}
searcher := func(input string, index int) bool {
pepper := kubeItems[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: label,
Items: kubeItems,
Templates: templates,
Size: 4,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
Error.Printf("Prompt failed %v\n", err)
os.Exit(-1)
}
return i
}

0 comments on commit 839ee78

Please sign in to comment.