char/pkg/register/plugin_client.go

53 lines
941 B
Go
Raw Normal View History

2022-11-01 14:26:54 +01:00
package register
import (
2022-11-01 21:15:32 +01:00
"context"
"encoding/json"
2022-11-01 14:26:54 +01:00
"net/rpc"
)
type PluginClient struct {
client *rpc.Client
}
2022-11-05 23:05:41 +01:00
type DoRequest struct {
CommandName string `json:"commandName"`
Args map[string]string `json:"args"`
}
// Do implements Plugin
func (pc *PluginClient) Do(ctx context.Context, commandName string, args map[string]string) error {
req := &DoRequest{
CommandName: commandName,
Args: args,
}
doReq, err := json.Marshal(req)
if err != nil {
return err
}
err = pc.client.Call("Plugin.Do", doReq, new(any))
if err != nil {
return err
}
return nil
}
2022-11-01 14:26:54 +01:00
var _ Plugin = &PluginClient{}
2022-11-01 21:15:32 +01:00
func (pc *PluginClient) About(ctx context.Context) (*About, error) {
2022-11-01 14:26:54 +01:00
var resp string
err := pc.client.Call("Plugin.About", new(any), &resp)
if err != nil {
2022-11-01 21:15:32 +01:00
return nil, err
2022-11-01 14:26:54 +01:00
}
2022-11-01 21:15:32 +01:00
var about About
err = json.Unmarshal([]byte(resp), &about)
if err != nil {
return nil, err
}
return &about, nil
2022-11-01 14:26:54 +01:00
}