From 5bbc976ef4df0769ea8a9ab99461d003fc79309f Mon Sep 17 00:00:00 2001 From: Claire Lee Date: Thu, 14 Nov 2024 22:42:28 -0800 Subject: [PATCH] add exception handling in load plugins command Signed-off-by: Claire Lee --- cmd/helm/load_plugins.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/helm/load_plugins.go b/cmd/helm/load_plugins.go index 5ae638124..7b64ae9a6 100644 --- a/cmd/helm/load_plugins.go +++ b/cmd/helm/load_plugins.go @@ -70,6 +70,15 @@ func loadPlugins(baseCmd *cobra.Command, out io.Writer) { md.Usage = fmt.Sprintf("the %q plugin", md.Name) } + // Check if a command with this name already exists in baseCmd. + if baseCmd.HasSubCommands() { + existingCmd, _, err := baseCmd.Find([]string{md.Name}) + if err == nil && existingCmd != nil { + fmt.Fprintf(os.Stderr, "plugin %q not loaded: command with this name already exists\n", md.Name) + continue + } + } + c := &cobra.Command{ Use: md.Name, Short: md.Usage, @@ -95,8 +104,7 @@ func loadPlugins(baseCmd *cobra.Command, out io.Writer) { // This passes all the flags to the subcommand. DisableFlagParsing: true, } - - // TODO: Make sure a command with this name does not already exist. + // Add the plugin command if the name does not already exist. baseCmd.AddCommand(c) // For completion, we try to load more details about the plugins so as to allow for command and