Skip to content

Commit cdcf4ba

Browse files
feat: add shell completions for context and namespace names (#175)
* feat: Add shell completions for context and namespace names * Trigger CI * fix
1 parent 7aaaa4b commit cdcf4ba

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

cmd/context.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
var contextManager = kubeconfig.NewContextManager(manager)
1111

1212
var contextCmd = &cobra.Command{
13-
Use: "context",
14-
Aliases: []string{"ctx"},
15-
Short: "Switch the active Kubernetes context",
16-
Args: cobra.MaximumNArgs(1),
13+
Use: "context",
14+
Aliases: []string{"ctx"},
15+
Short: "Switch the active Kubernetes context",
16+
ValidArgsFunction: getContextCompletions,
17+
Args: cobra.MaximumNArgs(1),
1718
Run: func(cmd *cobra.Command, args []string) {
1819
// Validate and get the config directory
1920
validConfigDir, err := contextManager.ValidateConfigDir(configDir)
@@ -61,3 +62,19 @@ var contextCmd = &cobra.Command{
6162
func init() {
6263
rootCmd.AddCommand(contextCmd)
6364
}
65+
66+
// getContextCompletions provides bash completion for available contexts
67+
func getContextCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
68+
configDirFlag, _ := cmd.Flags().GetString("kubeconfig-dir")
69+
validConfigDir, err := contextManager.ValidateConfigDir(configDirFlag)
70+
if err != nil {
71+
return nil, cobra.ShellCompDirectiveNoFileComp
72+
}
73+
74+
_, contextNames, err := contextManager.GetContextsFromDir(validConfigDir)
75+
if err != nil {
76+
return nil, cobra.ShellCompDirectiveNoFileComp
77+
}
78+
79+
return contextNames, cobra.ShellCompDirectiveNoFileComp
80+
}

cmd/namespace.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
var namespaceManager = kubeconfig.NewNamespaceManager(manager)
1111

1212
var namespaceCmd = &cobra.Command{
13-
Use: "namespace",
14-
Aliases: []string{"ns"},
15-
Short: "Switch the active Kubernetes namespace",
16-
Args: cobra.MaximumNArgs(1),
13+
Use: "namespace",
14+
Aliases: []string{"ns"},
15+
Short: "Switch the active Kubernetes namespace",
16+
Args: cobra.MaximumNArgs(1),
17+
ValidArgsFunction: getNamespaceCompletions,
1718
Run: func(cmd *cobra.Command, args []string) {
1819
// Get all namespaces from the current context
1920
namespaceNames, err := namespaceManager.GetNamespaces()
@@ -48,3 +49,13 @@ var namespaceCmd = &cobra.Command{
4849
func init() {
4950
rootCmd.AddCommand(namespaceCmd)
5051
}
52+
53+
// getNamespaceCompletions provides bash completion for available namespaces
54+
func getNamespaceCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
55+
namespaceNames, err := namespaceManager.GetNamespaces()
56+
if err != nil {
57+
return nil, cobra.ShellCompDirectiveNoFileComp
58+
}
59+
60+
return namespaceNames, cobra.ShellCompDirectiveNoFileComp
61+
}

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ var (
1515
)
1616

1717
var rootCmd = &cobra.Command{
18-
Use: "kubectl-switch",
18+
Use: "kubectl-switch",
19+
Annotations: map[string]string{
20+
cobra.CommandDisplayNameAnnotation: "kubectl switch",
21+
},
1922
Short: "A tool to switch Kubernetes contexts",
2023
Long: `kubectl-switch is a CLI tool to switch Kubernetes contexts from multiple kubeconfig files.`,
2124
Version: version,

0 commit comments

Comments
 (0)