Completions for kubectl config delete-user

This commit is contained in:
David Hotham 2021-12-20 14:40:56 +00:00 committed by David Hotham
parent 49dc226381
commit 62eb74880c
No known key found for this signature in database
GPG Key ID: FC427B0279945911
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()