Use getter function for plugin subcommand resolution

Plugin subcommand resolution is relatively less used than the
builtin subcommands. That's why, instead always initializing a
hash map on memory, it would be better to use a getter function only
serves as needed.

In addition to that this function will be exported that external
libraries can use it.
This commit is contained in:
Arda Güçlü 2023-05-10 10:41:43 +03:00
parent af1bf43067
commit 4634073d0b

View File

@ -84,10 +84,6 @@ import (
const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS" const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS"
var (
allowedCmdsSubcommandPlugin = map[string]struct{}{"create": {}}
)
type KubectlOptions struct { type KubectlOptions struct {
PluginHandler PluginHandler PluginHandler PluginHandler
Arguments []string Arguments []string
@ -146,7 +142,7 @@ func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command {
if cmdutil.CmdPluginAsSubcommand.IsEnabled() { if cmdutil.CmdPluginAsSubcommand.IsEnabled() {
// Command exists(e.g. kubectl create), but it is not certain that // Command exists(e.g. kubectl create), but it is not certain that
// subcommand also exists (e.g. kubectl create networkpolicy) // subcommand also exists (e.g. kubectl create networkpolicy)
if _, ok := allowedCmdsSubcommandPlugin[foundCmd.Name()]; ok { if _, ok := GetAllowedCmdsAsSubcommandPlugins()[foundCmd.Name()]; ok {
var subcommand string var subcommand string
for _, arg := range foundArgs { // first "non-flag" argument as subcommand for _, arg := range foundArgs { // first "non-flag" argument as subcommand
if !strings.HasPrefix(arg, "-") { if !strings.HasPrefix(arg, "-") {
@ -176,6 +172,13 @@ func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command {
return cmd return cmd
} }
// GetAllowedCmdsAsSubcommandPlugins returns the list of builtin commands
// that plugins are allowed to be used as subcommands only if the passed subcommand
// does not exist as builtin.
func GetAllowedCmdsAsSubcommandPlugins() map[string]struct{} {
return map[string]struct{}{"create": {}}
}
// PluginHandler is capable of parsing command line arguments // PluginHandler is capable of parsing command line arguments
// and performing executable filename lookups to search // and performing executable filename lookups to search
// for valid plugin files, and execute found plugins. // for valid plugin files, and execute found plugins.