@@ -10,10 +10,11 @@ import (
1010var contextManager = kubeconfig .NewContextManager (manager )
1111
1212var 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{
6162func 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+ }
0 commit comments