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

此更新提升了插件系统的灵活性和可扩展性,便于开发者动态管理和执行插件功能。
2025-03-14 11:31:32 +08:00

136 lines
3.6 KiB
Go

package plugin
import (
"fmt"
)
// PluginType 插件类型
type PluginType string
// 预定义的插件类型
const (
PluginTypeGeneral PluginType = "general" // 通用插件
PluginTypeStorage PluginType = "storage" // 存储插件
PluginTypeSecurity PluginType = "security" // 安全插件
PluginTypeNetwork PluginType = "network" // 网络插件
PluginTypeUtils PluginType = "utils" // 工具插件
PluginTypeHardware PluginType = "hardware" // 硬件插件
PluginTypeUI PluginType = "ui" // 用户界面插件
// 可以根据需求添加更多插件类型
)
// IPlugin 插件接口
// 这个文件定义了所有插件必须实现的接口
// 注意:这个文件应该与实际插件代码一起编译
type IPlugin interface {
// Name 插件名称
Name() string
// Version 插件版本
Version() string
// Description 插件描述
Description() string
// Author 插件作者
Author() string
// Type 插件类型
Type() PluginType
// Init 初始化插件
Init(config map[string]interface{}) error
// Start 启动插件
Start() error
// Stop 停止插件
Stop() error
// IsEnabled 插件是否启用
IsEnabled() bool
// SetEnabled 设置插件启用状态
SetEnabled(enabled bool)
// Execute 执行插件功能
// action: 要执行的操作名称
// params: 操作所需的参数
// 返回操作结果和可能的错误
Execute(action string, params map[string]interface{}) (interface{}, error)
}
// BasePlugin 提供插件接口的基本实现
// 插件开发者可以嵌入此结构体,以减少需要实现的方法数量
type BasePlugin struct {
name string
version string
description string
author string
pluginType PluginType
enabled bool
}
// NewBasePlugin 创建一个基本插件
func NewBasePlugin(name, version, description, author string, pluginType PluginType) *BasePlugin {
return &BasePlugin{
name: name,
version: version,
description: description,
author: author,
pluginType: pluginType,
enabled: true,
}
}
// NewBasePluginWithDefaultType 创建一个基本插件,使用默认的通用插件类型
// 这是一个便捷的构造函数,适用于不需要指定特殊类型的场景
func NewBasePluginWithDefaultType(name, version, description, author string) *BasePlugin {
return NewBasePlugin(name, version, description, author, PluginTypeGeneral)
}
// Name 获取插件名称
func (p *BasePlugin) Name() string {
return p.name
}
// Version 获取插件版本
func (p *BasePlugin) Version() string {
return p.version
}
// Description 获取插件描述
func (p *BasePlugin) Description() string {
return p.description
}
// Author 获取插件作者
func (p *BasePlugin) Author() string {
return p.author
}
// Type 获取插件类型
func (p *BasePlugin) Type() PluginType {
return p.pluginType
}
// IsEnabled 插件是否启用
func (p *BasePlugin) IsEnabled() bool {
return p.enabled
}
// SetEnabled 设置插件启用状态
func (p *BasePlugin) SetEnabled(enabled bool) {
p.enabled = enabled
}
// Init 初始化插件,子类需要重写此方法
func (p *BasePlugin) Init(config map[string]interface{}) error {
return nil
}
// Start 启动插件,子类需要重写此方法
func (p *BasePlugin) Start() error {
return nil
}
// Stop 停止插件,子类需要重写此方法
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)
}