loadAllPlugins loads all plugins and registers their commands to the root command
(rootCmd *cobra.Command)
| 308 | |
| 309 | // loadAllPlugins loads all plugins and registers their commands to the root command |
| 310 | func loadAllPlugins(rootCmd *cobra.Command) error { |
| 311 | ctx := rootCmd.Context() |
| 312 | |
| 313 | // Load all plugins from the plugins directory |
| 314 | if err := pluginManager.LoadPlugins(ctx); err != nil { |
| 315 | return fmt.Errorf("failed to load plugins: %w", err) |
| 316 | } |
| 317 | |
| 318 | // Get all loaded plugins |
| 319 | allPlugins := pluginManager.GetAllPlugins() |
| 320 | if len(allPlugins) == 0 { |
| 321 | return nil |
| 322 | } |
| 323 | |
| 324 | // Register commands from all plugins, checking for conflicts |
| 325 | for pluginName, plugin := range allPlugins { |
| 326 | for _, cmdInfo := range plugin.Metadata.Commands { |
| 327 | if existingPlugin, exists := registeredCommands[cmdInfo.Name]; exists { |
| 328 | return fmt.Errorf("command conflict: command '%s' is provided by both '%s' and '%s' plugins", |
| 329 | cmdInfo.Name, existingPlugin, pluginName) |
| 330 | } |
| 331 | |
| 332 | pluginCmd := createPluginCommand(rootCmd, plugin, cmdInfo) |
| 333 | |
| 334 | // If ParentCommand is specified, try to find the parent and add as subcommand otherwise add as top-level command |
| 335 | if cmdInfo.ParentCommand != "" { |
| 336 | parentCmd := findCommand(rootCmd, cmdInfo.ParentCommand) |
| 337 | if parentCmd == nil { |
| 338 | return fmt.Errorf("parent command '%s' not found for plugin command '%s' from plugin '%s'", |
| 339 | cmdInfo.ParentCommand, cmdInfo.Name, pluginName) |
| 340 | } |
| 341 | parentCmd.AddCommand(pluginCmd) |
| 342 | } else { |
| 343 | rootCmd.AddCommand(pluginCmd) |
| 344 | } |
| 345 | |
| 346 | registeredCommands[cmdInfo.Name] = pluginName |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return nil |
| 351 | } |
| 352 | |
| 353 | // cleanupPlugins should be called during application shutdown |
| 354 | func cleanupPlugins() { |
no test coverage detected