Skip to content

Commit

Permalink
Shell completion enhancements (#586)
Browse files Browse the repository at this point in the history
* Disable file completion for command args

* Add completion for `get-token --login`

* Add completion for kubecontexts

* Remove Cobra's default file completions

* Re-add explicit file and directory completions
  • Loading branch information
albers authored Jan 7, 2025
1 parent 15edb67 commit 2cd42fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/removetokencache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func newRemoveTokenCacheCmd() *cobra.Command {
}
return nil
},
ValidArgsFunction: cobra.NoFileCompletions,
}

cmd.Flags().StringVar(&tokenCacheDir, "token-cache-dir", token.DefaultTokenCacheDir, "directory to cache token")
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ func newTokenCmd() *cobra.Command {
}
return nil
},
ValidArgsFunction: cobra.NoFileCompletions,
}

o.AddFlags(cmd.Flags())
o.AddCompletions(cmd)

return cmd
}
28 changes: 28 additions & 0 deletions pkg/internal/converter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,33 @@ func (o *Options) isSet(name string) bool {
}

func (o *Options) AddCompletions(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc(flagContext, completeContexts(o))
_ = cmd.MarkFlagDirname(flagAzureConfigDir)
_ = cmd.MarkFlagFilename("kubeconfig", "")

o.TokenOptions.AddCompletions(cmd)

cmd.Flags().VisitAll(func(flag *pflag.Flag) {
// Set a default completion function if none was set. We don't look
// up if it does already have one set, because Cobra does this for
// us, and returns an error (which we ignore for this reason).
_ = cmd.RegisterFlagCompletionFunc(flag.Name, cobra.NoFileCompletions)
})
}

func completeContexts(o *Options) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
clientConfig := o.configFlags.ToRawKubeConfigLoader()
config, err := clientConfig.RawConfig()
if err != nil {
cobra.CompDebugln(fmt.Sprintf("unable to load kubeconfig: %s", err), false)
}

contexts := make([]string, 0, len(config.Contexts))
for name, _ := range config.Contexts {
contexts = append(contexts, name)
}

return contexts, cobra.ShellCompDirectiveNoFileComp
}
}
10 changes: 10 additions & 0 deletions pkg/internal/token/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,14 @@ func (o *Options) AddCompletions(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc("login", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return supportedLogin, cobra.ShellCompDirectiveNoFileComp
})
_ = cmd.MarkFlagFilename("client-certificate", "pfx", "cert")
_ = cmd.MarkFlagFilename("federated-token-file", "")
_ = cmd.MarkFlagDirname("token-cache-dir")

cmd.Flags().VisitAll(func(flag *pflag.Flag) {
// Set a default completion function if none was set. We don't look
// up if it does already have one set, because Cobra does this for
// us, and returns an error (which we ignore for this reason).
_ = cmd.RegisterFlagCompletionFunc(flag.Name, cobra.NoFileCompletions)
})
}

0 comments on commit 2cd42fb

Please sign in to comment.