增强插件系统:添加插件执行功能
- 在插件接口和基本插件实现中新增 Execute 方法,支持插件功能的动态执行。 - 更新各个插件(如日志插件、统计插件、存储插件等)以实现 Execute 方法,允许通过操作名称和参数执行特定功能。 - 在插件管理器中添加 ExecutePlugin、ExecutePluginsByType 和 ExecuteAllPlugins 方法,便于批量执行插件操作。 - 示例程序中更新插件调用方式,展示如何使用 Execute 方法进行操作。 此更新提升了插件系统的灵活性和可扩展性,便于开发者动态管理和执行插件功能。
This commit is contained in:
@@ -35,6 +35,11 @@ type Plugin interface {
|
||||
IsEnabled() bool
|
||||
// SetEnabled 设置插件启用状态
|
||||
SetEnabled(enabled bool)
|
||||
// Execute 执行插件功能
|
||||
// action: 要执行的操作名称
|
||||
// params: 操作所需的参数
|
||||
// 返回操作结果和可能的错误
|
||||
Execute(ctx context.Context, action string, params map[string]interface{}) (interface{}, error)
|
||||
}
|
||||
|
||||
// BasePluginImpl 提供插件接口的基本实现,用于适配Plugin接口
|
||||
@@ -72,6 +77,11 @@ func (p *BasePluginImpl) Stop(ctx context.Context) error {
|
||||
return p.BasePlugin.Stop()
|
||||
}
|
||||
|
||||
// Execute 适配Execute方法以支持context参数
|
||||
func (p *BasePluginImpl) Execute(ctx context.Context, action string, params map[string]interface{}) (interface{}, error) {
|
||||
return p.BasePlugin.Execute(action, params)
|
||||
}
|
||||
|
||||
// PluginInfo 插件信息
|
||||
type PluginInfo struct {
|
||||
Name string `json:"name"`
|
||||
@@ -577,3 +587,66 @@ func (pm *PluginManager) GetPluginConfig(name string) (map[string]interface{}, e
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ExecutePlugin 执行指定插件的操作
|
||||
func (pm *PluginManager) ExecutePlugin(ctx context.Context, name string, action string, params map[string]interface{}) (interface{}, error) {
|
||||
pm.mu.RLock()
|
||||
defer pm.mu.RUnlock()
|
||||
|
||||
plugin, exists := pm.plugins[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("插件 %s 不存在", name)
|
||||
}
|
||||
|
||||
if !plugin.IsEnabled() {
|
||||
return nil, fmt.Errorf("插件 %s 已禁用", name)
|
||||
}
|
||||
|
||||
return plugin.Execute(ctx, action, params)
|
||||
}
|
||||
|
||||
// ExecutePluginsByType 对指定类型的所有插件执行操作
|
||||
func (pm *PluginManager) ExecutePluginsByType(ctx context.Context, pluginType PluginType, action string, params map[string]interface{}) map[string]interface{} {
|
||||
pm.mu.RLock()
|
||||
defer pm.mu.RUnlock()
|
||||
|
||||
results := make(map[string]interface{})
|
||||
|
||||
if typePlugins, exists := pm.pluginsByType[pluginType]; exists {
|
||||
for name, plugin := range typePlugins {
|
||||
if !plugin.IsEnabled() {
|
||||
continue
|
||||
}
|
||||
|
||||
result, err := plugin.Execute(ctx, action, params)
|
||||
results[name] = map[string]interface{}{
|
||||
"result": result,
|
||||
"error": err,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// ExecuteAllPlugins 对所有插件执行操作
|
||||
func (pm *PluginManager) ExecuteAllPlugins(ctx context.Context, action string, params map[string]interface{}) map[string]interface{} {
|
||||
pm.mu.RLock()
|
||||
defer pm.mu.RUnlock()
|
||||
|
||||
results := make(map[string]interface{})
|
||||
|
||||
for name, plugin := range pm.plugins {
|
||||
if !plugin.IsEnabled() {
|
||||
continue
|
||||
}
|
||||
|
||||
result, err := plugin.Execute(ctx, action, params)
|
||||
results[name] = map[string]interface{}{
|
||||
"result": result,
|
||||
"error": err,
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
Reference in New Issue
Block a user