- 在插件接口中新增 GetOperationInfo 和 GetAllOperations 方法,支持获取特定操作及所有操作的参数信息。 - 更新 PluginHelper 以实现获取操作参数信息的功能,提升插件的可用性和灵活性。 - 在日志插件中实现操作信息获取,展示如何使用新方法获取操作的详细参数信息。 - 示例程序中添加新的选项,展示如何获取插件操作的参数信息。 此更新提升了插件系统的可扩展性和易用性,便于开发者更好地管理和使用插件功能。
183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/darkit/goproxy/examples/plugin"
|
|
)
|
|
|
|
// 格式化输出JSON
|
|
func prettyPrint(data interface{}) {
|
|
bytes, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
fmt.Printf("错误: %v\n", err)
|
|
return
|
|
}
|
|
fmt.Println(string(bytes))
|
|
}
|
|
|
|
// 演示插件操作信息获取功能
|
|
func OperationInfoExample() {
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 获取插件目录
|
|
pluginsDir := filepath.Join("..", "plugins", "dist")
|
|
if _, err := os.Stat(pluginsDir); os.IsNotExist(err) {
|
|
fmt.Printf("插件目录不存在: %s\n", pluginsDir)
|
|
return
|
|
}
|
|
|
|
// 创建插件管理器
|
|
pm := plugin.NewPluginManager(pluginsDir)
|
|
|
|
// 加载插件
|
|
fmt.Println("正在加载插件...")
|
|
if err := pm.LoadPlugins(); err != nil {
|
|
fmt.Printf("加载插件失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// 初始化所有插件
|
|
if err := pm.InitPlugins(ctx); err != nil {
|
|
fmt.Printf("初始化插件失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// 启动所有插件
|
|
if err := pm.StartPlugins(ctx); err != nil {
|
|
fmt.Printf("启动插件失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// 打印所有插件信息
|
|
fmt.Println("\n=== 已加载的插件 ===")
|
|
for _, info := range pm.GetPluginInfos() {
|
|
fmt.Printf("插件: %s (版本: %s, 类型: %s)\n", info.Name, info.Version, info.Type)
|
|
}
|
|
fmt.Println()
|
|
|
|
// 示例1: 获取StatsPlugin的所有操作信息
|
|
fmt.Println("=== 示例1: 获取StatsPlugin的所有操作信息 ===")
|
|
operations, err := pm.GetPluginAllOperations("StatsPlugin")
|
|
if err != nil {
|
|
fmt.Printf("获取操作信息失败: %v\n", err)
|
|
} else {
|
|
fmt.Printf("StatsPlugin支持 %d 个操作:\n", len(operations))
|
|
for opName := range operations {
|
|
fmt.Printf(" - %s\n", opName)
|
|
}
|
|
|
|
// 选择一个操作详细展示
|
|
if len(operations) > 0 {
|
|
selectedOp := ""
|
|
for opName := range operations {
|
|
if strings.Contains(strings.ToLower(opName), "record") {
|
|
selectedOp = opName
|
|
break
|
|
}
|
|
}
|
|
|
|
if selectedOp != "" {
|
|
fmt.Printf("\n%s 操作的详细信息:\n", selectedOp)
|
|
prettyPrint(operations[selectedOp])
|
|
}
|
|
}
|
|
}
|
|
|
|
// 示例2: 获取特定操作的参数信息
|
|
fmt.Println("\n=== 示例2: 获取特定操作的参数信息 ===")
|
|
recordInfo, err := pm.GetPluginOperationInfo("StatsPlugin", "recordrequest")
|
|
if err != nil {
|
|
fmt.Printf("获取recordrequest参数信息失败: %v\n", err)
|
|
} else {
|
|
fmt.Println("recordrequest操作的参数信息:")
|
|
prettyPrint(recordInfo)
|
|
|
|
// 基于参数信息构建调用参数
|
|
if paramsInfo, ok := recordInfo["parameters"].(map[string]interface{}); ok {
|
|
fmt.Println("\n基于参数信息自动构建调用参数:")
|
|
|
|
callParams := make(map[string]interface{})
|
|
for paramName, paramInfo := range paramsInfo {
|
|
if paramInfoMap, ok := paramInfo.(map[string]interface{}); ok {
|
|
if paramInfoMap["type"] == "struct" {
|
|
// 处理结构体参数
|
|
if fields, ok := paramInfoMap["fields"].(map[string]interface{}); ok {
|
|
for fieldName, fieldInfo := range fields {
|
|
if fieldInfoMap, ok := fieldInfo.(map[string]interface{}); ok {
|
|
// 根据字段类型设置默认值
|
|
fieldType, _ := fieldInfoMap["type"].(string)
|
|
switch fieldType {
|
|
case "integer":
|
|
callParams[fieldName] = int64(1000)
|
|
case "boolean":
|
|
callParams[fieldName] = false
|
|
case "string":
|
|
callParams[fieldName] = "自动生成的值"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// 处理基本类型参数
|
|
paramType, _ := paramInfoMap["type"].(string)
|
|
switch paramType {
|
|
case "integer":
|
|
callParams[paramName] = int64(42)
|
|
case "boolean":
|
|
callParams[paramName] = true
|
|
case "string":
|
|
callParams[paramName] = "自动生成的值"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 打印构建的参数
|
|
prettyPrint(callParams)
|
|
|
|
// 使用构建的参数调用操作
|
|
fmt.Println("\n使用自动构建的参数调用操作:")
|
|
result, err := pm.ExecutePlugin(ctx, "StatsPlugin", "recordrequest", callParams)
|
|
fmt.Printf("调用结果: %v, 错误: %v\n", result, err)
|
|
}
|
|
}
|
|
|
|
// 示例3: 获取所有插件的所有操作信息
|
|
fmt.Println("\n=== 示例3: 获取所有插件的所有操作信息 ===")
|
|
allOperations := pm.GetAllPluginsOperations()
|
|
fmt.Printf("找到 %d 个插件的操作信息:\n", len(allOperations))
|
|
|
|
var totalOps int
|
|
for pluginName, ops := range allOperations {
|
|
fmt.Printf(" 插件 %s: %d 个操作\n", pluginName, len(ops))
|
|
totalOps += len(ops)
|
|
}
|
|
fmt.Printf("总计 %d 个可用操作\n", totalOps)
|
|
|
|
// 示例4: 按插件类型获取操作信息
|
|
fmt.Println("\n=== 示例4: 按插件类型获取操作信息 ===")
|
|
utilsOps := pm.GetOperationsByType(plugin.PluginTypeUtils)
|
|
fmt.Printf("工具类插件操作:\n")
|
|
for pluginName, ops := range utilsOps {
|
|
fmt.Printf(" 插件 %s: %d 个操作\n", pluginName, len(ops))
|
|
for opName := range ops {
|
|
fmt.Printf(" - %s\n", opName)
|
|
}
|
|
}
|
|
|
|
// 停止所有插件
|
|
if err := pm.StopPlugins(ctx); err != nil {
|
|
fmt.Printf("停止插件失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("\n示例结束")
|
|
}
|