feat(cli): add a --context flag alias of --kube-context

For parity with kubectl. Adds --context as an alias for the existing
--kube-context flag, so users switching between kubectl and helm don't
have to remember two different flag names for the same thing.

Also registers shell completion for --context (reusing the existing
--kube-context completion function) and keeps the flag visible in
--help, so the alias is actually discoverable rather than hidden.

Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
pull/32625/head
Sylvain Rabot 4 days ago
parent fa11636b01
commit 2fc6775f7e
No known key found for this signature in database
GPG Key ID: 13D27DFB503A8D91

@ -152,6 +152,7 @@ func New() *EnvSettings {
func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&s.namespace, "namespace", "n", s.namespace, "namespace scope for this request")
fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file")
fs.StringVar(&s.KubeContext, "context", s.KubeContext, "name of the kubeconfig context to use (alias for --kube-context)")
fs.StringVar(&s.KubeContext, "kube-context", s.KubeContext, "name of the kubeconfig context to use")
fs.StringVar(&s.KubeToken, "kube-token", s.KubeToken, "bearer token used for authentication")
fs.StringVar(&s.KubeAsUser, "kube-as-user", s.KubeAsUser, "username to impersonate for the operation")

@ -233,8 +233,8 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
log.Fatal(err)
}
// Setup shell completion for the kube-context flag
err = cmd.RegisterFlagCompletionFunc("kube-context", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
// Setup shell completion for the kube-context flag (and its --context alias)
kubeContextCompletionFunc := func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
cobra.CompDebugln("About to get the different kube-contexts", settings.Debug)
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
@ -251,7 +251,12 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
return comps, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
})
}
err = cmd.RegisterFlagCompletionFunc("kube-context", kubeContextCompletionFunc)
if err != nil {
log.Fatal(err)
}
err = cmd.RegisterFlagCompletionFunc("context", kubeContextCompletionFunc)
if err != nil {
log.Fatal(err)
}

Loading…
Cancel
Save