Merge pull request #107142 from dimbleby/delete-user-completion

Completions for kubectl config delete-user

Kubernetes-commit: 31dba0a435ecb98722fc71dab6f8180ffc0e0935
This commit is contained in:
Kubernetes Publisher 2022-02-12 06:19:48 -08:00
commit f9b136324e
2 changed files with 11 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
@ -64,6 +65,7 @@ func NewCmdConfigDeleteUser(streams genericclioptions.IOStreams, configAccess cl
Short: i18n.T("Delete the specified user from the kubeconfig"),
Long: i18n.T("Delete the specified user from the kubeconfig."),
Example: deleteUserExample,
ValidArgsFunction: util.UserCompletionFunc,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(cmd, args))
cmdutil.CheckErr(o.Validate())

View File

@ -131,6 +131,15 @@ func ClusterCompletionFunc(cmd *cobra.Command, args []string, toComplete string)
return nil, cobra.ShellCompDirectiveNoFileComp
}
// UserCompletionFunc is a completion function that completes as a first argument the
// user names that match the toComplete prefix
func UserCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return ListUsersInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ListContextsInConfig returns a list of context names which begin with `toComplete`
func ListContextsInConfig(toComplete string) []string {
config, err := factory.ToRawKubeConfigLoader().RawConfig()