Skip to content

Commit

Permalink
update cmd delete
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 6, 2019
1 parent 570be57 commit 9033726
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
37 changes: 32 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"log"
Expand Down Expand Up @@ -57,10 +58,15 @@ kubecm delete my-context
}
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)
confirm := BoolUI(fmt.Sprintf("Are you sure you want to delete「%s」?", kubeName))
if confirm == "True" {
err = deleteContext([]string{kubeName}, config)
if err != nil {
Error.Println(err)
os.Exit(-1)
}
} else {
log.Println("Nothing deleted!")
}
} else {
fmt.Println("Please enter the context you want to delete.")
Expand All @@ -77,7 +83,7 @@ func deleteContext(ctxs []string, config *clientcmdapi.Config) error {
for _, ctx := range ctxs {
if _, ok := config.Contexts[ctx]; ok {
delete(config.Contexts, ctx)
log.Printf("Context Delete: %s \n", ctx)
log.Printf("Context Delete:「%s」\n", ctx)
} else {
Error.Printf("「%s」do not exit.", ctx)
}
Expand All @@ -89,3 +95,24 @@ func deleteContext(ctxs []string, config *clientcmdapi.Config) error {
}
return nil
}

func BoolUI(label string) string {
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F37A {{ . | red }}",
Inactive: " {{ . | cyan }}",
Selected: "\U0001F47B {{ . | green }}",
}
prompt := promptui.Select{
Label: label,
Items: []string{"True", "False"},
Templates: templates,
Size: 2,
}
_, obj, err := prompt.Run()
if err != nil {
Error.Printf("Prompt failed %v\n", err)
os.Exit(-1)
}
return obj
}
2 changes: 1 addition & 1 deletion cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func SelectUI(kubeItems []needle, label string) int {
Label: "{{ . }}",
Active: "\U0001F63C {{ .Name | red }}{{ .Center | red}}",
Inactive: " {{ .Name | cyan }}{{ .Center | red}}",
Selected: "\U0001F638 {{ .Name | green }}",
Selected: "\U0001F638 Select:{{ .Name | green }}",
Details: `
--------- Info ----------
{{ "Name:" | faint }} {{ .Name }}
Expand Down

0 comments on commit 9033726

Please sign in to comment.