-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.go
109 lines (87 loc) · 2.41 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package probe
import (
"context"
"os"
"os/exec"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/linyows/probe/pb"
"google.golang.org/grpc"
)
var (
BuiltinCmd = "builtin-actions"
Handshake = plugin.HandshakeConfig{ProtocolVersion: 1, MagicCookieKey: "probe", MagicCookieValue: "actions"}
PluginMap = map[string]plugin.Plugin{"actions": &ActionsPlugin{}}
)
type ActionsArgs []string
type ActionsParams map[string]string
type Actions interface {
Run(args []string, with map[string]string) (map[string]string, error)
}
type ActionsPlugin struct {
plugin.Plugin
Impl Actions
}
func (p *ActionsPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
pb.RegisterActionsServer(s, &ActionsServer{Impl: p.Impl})
return nil
}
func (p *ActionsPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (any, error) {
return &ActionsClient{client: pb.NewActionsClient(c)}, nil
}
type ActionsClient struct {
client pb.ActionsClient
}
func (m *ActionsClient) Run(args []string, with map[string]string) (map[string]string, error) {
res := map[string]string{}
runRes, err := m.client.Run(context.Background(), &pb.RunRequest{
Args: args,
With: with,
})
if err != nil {
return res, err
}
return runRes.Result, err
}
type ActionsServer struct {
Impl Actions
}
func (m *ActionsServer) Run(ctx context.Context, req *pb.RunRequest) (*pb.RunResponse, error) {
v, err := m.Impl.Run(req.Args, req.With)
return &pb.RunResponse{Result: v}, err
}
func RunActions(name string, args []string, with map[string]any, verbose bool) (map[string]any, error) {
loglevel := hclog.Warn
if verbose {
loglevel = hclog.Debug
}
log := hclog.New(&hclog.LoggerOptions{
Name: "actions",
Output: os.Stdout,
Level: loglevel,
})
cl := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: Handshake,
Plugins: PluginMap,
Cmd: exec.Command(os.Args[0], BuiltinCmd, name),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
Logger: log,
})
defer cl.Kill()
protocol, err := cl.Client()
if err != nil {
return nil, err
}
raw, err := protocol.Dispense("actions")
if err != nil {
return nil, err
}
actions := raw.(Actions)
flatW := FlattenInterface(with)
result, err := actions.Run(args, flatW)
if err != nil {
return nil, err
}
unflatR := UnflattenInterface(result)
return unflatR, nil
}