-
Notifications
You must be signed in to change notification settings - Fork 1
/
namespace.go
126 lines (112 loc) · 3.07 KB
/
namespace.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
package cmd
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/radiusmethod/kxd/src/utils"
"github.com/spf13/cobra"
"log"
)
var namespaceCmd = &cobra.Command{
Use: "namespace",
Short: "Kubeconfig namespace command",
Aliases: []string{"ns"},
Long: "This is the default namespace command.",
}
var currentNamespaceCmd = &cobra.Command{
Use: "current",
Short: "Shows currently set k8s namespace",
Aliases: []string{"c"},
Long: "This shows the current set k8s namespace.",
Run: func(cmd *cobra.Command, args []string) {
err := runListNamespace()
if err != nil {
log.Fatal(err)
}
},
}
var switchNamespaceCmd = &cobra.Command{
Use: "switch",
Short: "Switch k8s namespaces",
Aliases: []string{"s"},
Long: "This allows for switching of your k8s namespace.",
Run: func(cmd *cobra.Command, args []string) {
err := runNamespaceSwitcher()
if err != nil {
log.Fatal(err)
}
},
}
var listNamespacesCmd = &cobra.Command{
Use: "list",
Short: "List kubeconfig namespaces",
Aliases: []string{"l"},
Long: "This displays a simple list of your k8s namespaces.",
Run: func(cmd *cobra.Command, args []string) {
err := runNamespaceLister()
if err != nil {
log.Fatal(err)
}
},
}
func init() {
namespaceCmd.AddCommand(switchNamespaceCmd, currentNamespaceCmd, listNamespacesCmd)
rootCmd.AddCommand(namespaceCmd)
}
func runNamespaceSwitcher() error {
kubeconfigPath := utils.GetCurrentConfigFile()
namespaces := utils.ListNamespaces(kubeconfigPath)
fmt.Printf(utils.NoticeColor, "Kubeconfig Namespace Switcher\n")
prompt := promptui.Select{
Label: fmt.Sprintf(utils.PromptColor, "Choose a namespace"),
Items: namespaces,
HideHelp: true,
HideSelected: true,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: fmt.Sprintf("%s {{ . | magenta }}", promptui.IconSelect),
Inactive: " {{.}}",
Selected: " {{ . | magenta }}",
},
Searcher: utils.NewPromptUISearcher(namespaces),
StartInSearchMode: true,
Stdout: &utils.BellSkipper{},
}
_, result, err := prompt.Run()
if err != nil {
utils.CheckError(err)
}
fmt.Printf(utils.PromptColor, "Choose a namespace")
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.MagentaColor, result)
fmt.Println("")
err = utils.SwitchNamespace(result, kubeconfigPath)
if err != nil {
log.Fatalf("Error switching namespace: %v\n", err)
}
return nil
}
func runListNamespace() error {
kubeconfigPath := utils.GetCurrentConfigFile()
config, err := utils.InitializeKubeconfig(kubeconfigPath)
if err != nil {
log.Fatalf("Error initializing kubeconfig: %v\n", err)
}
currentContext := config.CurrentContext
context, exists := config.Contexts[currentContext]
if !exists {
log.Fatal("Current context not found in kubeconfig")
}
namespace := context.Namespace
if namespace == "" {
namespace = "default"
}
fmt.Printf(namespace + "\n")
return nil
}
func runNamespaceLister() error {
namespaces := utils.ListNamespaces(utils.GetCurrentConfigFile())
for _, c := range namespaces {
fmt.Println(c)
}
return nil
}