增强插件系统:添加插件执行功能

- 在插件接口和基本插件实现中新增 Execute 方法,支持插件功能的动态执行。
- 更新各个插件(如日志插件、统计插件、存储插件等)以实现 Execute 方法,允许通过操作名称和参数执行特定功能。
- 在插件管理器中添加 ExecutePlugin、ExecutePluginsByType 和 ExecuteAllPlugins 方法,便于批量执行插件操作。
- 示例程序中更新插件调用方式,展示如何使用 Execute 方法进行操作。

此更新提升了插件系统的灵活性和可扩展性,便于开发者动态管理和执行插件功能。
This commit is contained in:
2025-03-14 11:31:32 +08:00
parent 0990e8c42c
commit 9f78cb483b
9 changed files with 559 additions and 7 deletions

View File

@@ -1,5 +1,9 @@
package plugin
import (
"fmt"
)
// PluginType 插件类型
type PluginType string
@@ -8,10 +12,10 @@ const (
PluginTypeGeneral PluginType = "general" // 通用插件
PluginTypeStorage PluginType = "storage" // 存储插件
PluginTypeSecurity PluginType = "security" // 安全插件
PluginTypeUI PluginType = "ui" // 用户界面插件
PluginTypeNetwork PluginType = "network" // 网络插件
PluginTypeUtils PluginType = "utils" // 工具插件
PluginTypeHardware PluginType = "hardware" // 硬件插件
PluginTypeUI PluginType = "ui" // 用户界面插件
// 可以根据需求添加更多插件类型
)
@@ -39,6 +43,11 @@ type IPlugin interface {
IsEnabled() bool
// SetEnabled 设置插件启用状态
SetEnabled(enabled bool)
// Execute 执行插件功能
// action: 要执行的操作名称
// params: 操作所需的参数
// 返回操作结果和可能的错误
Execute(action string, params map[string]interface{}) (interface{}, error)
}
// BasePlugin 提供插件接口的基本实现
@@ -119,3 +128,8 @@ func (p *BasePlugin) Start() error {
func (p *BasePlugin) Stop() error {
return nil
}
// Execute 执行插件功能,子类需要重写此方法
func (p *BasePlugin) Execute(action string, params map[string]interface{}) (interface{}, error) {
return nil, fmt.Errorf("插件 %s 不支持 %s 操作", p.name, action)
}