Skip to content

Commit

Permalink
Interactive cmd add exit option
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Jan 20, 2020
1 parent 41892fb commit 1428ef0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 355 deletions.
7 changes: 6 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (dc *DeleteCommand) runDelete(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
}
num := SelectUI(kubeItems, "Select The Delete Kube Context")
kubeName := kubeItems[num].Name
confirm := BoolUI(fmt.Sprintf("Are you sure you want to delete「%s」?", kubeName))
Expand All @@ -76,7 +81,7 @@ func (dc *DeleteCommand) runDelete(command *cobra.Command, args []string) error
return nil
}

func (dc *DeleteCommand)deleteContext(ctxs []string, config *clientcmdapi.Config) error {
func (dc *DeleteCommand) deleteContext(ctxs []string, config *clientcmdapi.Config) error {
for _, ctx := range ctxs {
if _, ok := config.Contexts[ctx]; ok {
delete(config.Contexts, ctx)
Expand Down
5 changes: 5 additions & 0 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ 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
}
num := SelectUI(kubeItems, "Select The Rename Kube Context")
kubeName := kubeItems[num].Name
rename := PromptUI("Rename", kubeName)
Expand Down
12 changes: 5 additions & 7 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ type SwitchCommand struct {
baseCommand
}

type needle struct {
Name string
Cluster string
User string
Center string
}

func (sc *SwitchCommand) Init() {
sc.command = &cobra.Command{
Use: "switch",
Expand Down Expand Up @@ -60,6 +53,11 @@ func (sc *SwitchCommand) runSwitch(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
}
num := SelectUI(kubeItems, "Select Kube Context")
kubeName := kubeItems[num].Name
config.CurrentContext = kubeName
Expand Down
27 changes: 26 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ import (
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"log"
"os"
"os/user"
"strings"
)

type needle struct {
Name string
Cluster string
User string
Center string
}

// ModifyKubeConfig modify kubeconfig
func ModifyKubeConfig(config *clientcmdapi.Config) error {
commandLineFile, _ := ioutil.TempFile("", "")
Expand Down Expand Up @@ -158,6 +166,9 @@ func SelectUI(kubeItems []needle, label string) int {
pepper := kubeItems[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
if input == "q" && name == "<exit>" {
return true
}
return strings.Contains(name, input)
}
prompt := promptui.Select{
Expand All @@ -171,6 +182,10 @@ func SelectUI(kubeItems []needle, label string) int {
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
if kubeItems[i].Name == "<exit>" {
fmt.Println("Exiting.")
os.Exit(1)
}
return i
}

Expand Down Expand Up @@ -254,4 +269,14 @@ func WriteConfig(config []byte) error {
}
}
return nil
}
}

// ExitOption exit option of SelectUI
func ExitOption(kubeItems []needle) ([]needle, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
kubeItems = append(kubeItems, needle{Name: "<exit>", Cluster: "exit the kubecm", User: u.Username})
return kubeItems, nil
}
Loading

0 comments on commit 1428ef0

Please sign in to comment.