mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-11 15:31:37 +08:00
feat: support switch hashicorp plugin with environment HRP_PLUGIN_TYPE
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package pluginInternal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-plugin"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -19,7 +21,14 @@ type HashicorpPlugin struct {
|
||||
}
|
||||
|
||||
func (p *HashicorpPlugin) Init(path string) error {
|
||||
pluginName := GRPCPluginName
|
||||
var pluginName string
|
||||
if IsRPCPluginType() {
|
||||
pluginName = RPCPluginName
|
||||
} else {
|
||||
pluginName = GRPCPluginName
|
||||
}
|
||||
|
||||
// logger
|
||||
loggerOptions := &hclog.LoggerOptions{
|
||||
Name: pluginName,
|
||||
Output: os.Stdout,
|
||||
@@ -29,6 +38,11 @@ func (p *HashicorpPlugin) Init(path string) error {
|
||||
} else {
|
||||
loggerOptions.Level = hclog.Info
|
||||
}
|
||||
|
||||
// cmd
|
||||
cmd := exec.Command(path)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", hrpPluginTypeEnvName, hrpPluginType))
|
||||
|
||||
// launch the plugin process
|
||||
client = plugin.NewClient(&plugin.ClientConfig{
|
||||
HandshakeConfig: HandshakeConfig,
|
||||
@@ -36,7 +50,7 @@ func (p *HashicorpPlugin) Init(path string) error {
|
||||
RPCPluginName: &RPCPlugin{},
|
||||
GRPCPluginName: &GRPCPlugin{},
|
||||
},
|
||||
Cmd: exec.Command(path),
|
||||
Cmd: cmd,
|
||||
Logger: hclog.New(loggerOptions),
|
||||
AllowedProtocols: []plugin.Protocol{
|
||||
plugin.ProtocolNetRPC,
|
||||
@@ -44,18 +58,16 @@ func (p *HashicorpPlugin) Init(path string) error {
|
||||
},
|
||||
})
|
||||
|
||||
// Connect via RPC
|
||||
// Connect via RPC/gRPC
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("connect plugin via RPC failed")
|
||||
return err
|
||||
return errors.Wrap(err, fmt.Sprintf("connect %s plugin failed", hrpPluginType))
|
||||
}
|
||||
|
||||
// Request the plugin
|
||||
raw, err := rpcClient.Dispense(pluginName)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("request plugin failed")
|
||||
return err
|
||||
return errors.Wrap(err, fmt.Sprintf("request %s plugin failed", hrpPluginType))
|
||||
}
|
||||
|
||||
// We should have a Function now! This feels like a normal interface
|
||||
|
||||
Reference in New Issue
Block a user