-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
127 lines (113 loc) · 3.08 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cmd
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/radiusmethod/kxd/src/utils"
"github.com/spf13/cobra"
"log"
"os"
)
var contextCmd = &cobra.Command{
Use: "context",
Short: "Kubeconfig context command",
Aliases: []string{"ctx"},
Long: "This is the default context command.",
}
var currentContextCmd = &cobra.Command{
Use: "current",
Short: "Shows currently set kubeconfig context",
Aliases: []string{"c"},
Long: "This shows the current set kubeconfig context.",
Run: func(cmd *cobra.Command, args []string) {
err := runListContext()
if err != nil {
log.Fatal(err)
}
},
}
var switchContextCmd = &cobra.Command{
Use: "switch",
Short: "Switch kubeconfig contexts",
Aliases: []string{"s"},
Long: "This allows for switching of your kubeconfig context.",
Run: func(cmd *cobra.Command, args []string) {
err := runContextSwitcher()
if err != nil {
log.Fatal(err)
}
},
}
var listContextsCmd = &cobra.Command{
Use: "list",
Short: "List kubeconfig contexts",
Aliases: []string{"l"},
Long: "This displays a simple list of your kubeconfig contexts.",
Run: func(cmd *cobra.Command, args []string) {
err := runContextLister()
if err != nil {
log.Fatal(err)
}
},
}
func init() {
contextCmd.AddCommand(switchContextCmd, currentContextCmd, listContextsCmd)
rootCmd.AddCommand(contextCmd)
}
func runContextSwitcher() error {
kubeconfigPath := utils.GetCurrentConfigFile()
config, err := utils.InitializeKubeconfig(kubeconfigPath)
if err != nil {
log.Fatalf("Error initializing kubeconfig: %v\n", err)
}
contexts := utils.ListContexts(kubeconfigPath)
fmt.Printf(utils.NoticeColor, "Kubeconfig Context Switcher\n")
prompt := promptui.Select{
Label: fmt.Sprintf(utils.PromptColor, "Choose a context"),
Items: contexts,
HideHelp: true,
HideSelected: true,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: fmt.Sprintf("%s {{ . | magenta }}", promptui.IconSelect),
Inactive: " {{.}}",
Selected: " {{ . | magenta }}",
},
Searcher: utils.NewPromptUISearcher(contexts),
StartInSearchMode: true,
Stdout: &utils.BellSkipper{},
}
_, result, err := prompt.Run()
if err != nil {
utils.CheckError(err)
}
fmt.Printf(utils.PromptColor, "Choose a context")
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.MagentaColor, result)
fmt.Println("")
err = utils.SwitchContext(config, result, kubeconfigPath)
if err != nil {
log.Fatalf("Error switching context: %v\n", err)
}
return nil
}
func runListContext() error {
kubeconfigPath := utils.GetCurrentConfigFile()
config, err := utils.InitializeKubeconfig(kubeconfigPath)
if err != nil {
log.Fatalf("Error initializing kubeconfig: %v\n", err)
}
currentContext := config.CurrentContext
if currentContext == "" {
fmt.Println("No current context found in kubeconfig.")
os.Exit(1)
}
fmt.Printf(currentContext + "\n")
return nil
}
func runContextLister() error {
contexts := utils.ListContexts(utils.GetCurrentConfigFile())
for _, c := range contexts {
fmt.Println(c)
}
return nil
}