/
bacotell_plugin.go
176 lines (141 loc) · 4.89 KB
/
bacotell_plugin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package codec
import (
"context"
"github.com/EliasStar/BacoTell/internal/proto/bacotellpb"
common "github.com/EliasStar/BacoTell/pkg/bacotell_common"
"github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
)
// The server implementation of bacotell_common.Plugin.
type pluginServer struct {
bacotellpb.UnimplementedPluginServer
impl common.Plugin
broker *plugin.GRPCBroker
}
// pluginServer implements bacotellpb.PluginServer.
var _ bacotellpb.PluginServer = pluginServer{}
// Id implements bacotellpb.PluginServer.
func (s pluginServer) Id(context.Context, *bacotellpb.PluginIdRequest) (*bacotellpb.PluginIdResponse, error) {
id, err := s.impl.ID()
if err != nil {
return nil, err
}
return &bacotellpb.PluginIdResponse{Id: id}, nil
}
// ApplicationCommands implements bacotellpb.PluginServer.
func (s pluginServer) ApplicationCommands(context.Context, *bacotellpb.PluginApplicationCommandsRequest) (*bacotellpb.PluginApplicationCommandsResponse, error) {
commands, err := s.impl.ApplicationCommands()
if err != nil {
return nil, err
}
ids := make([]uint32, len(commands))
for i, command := range commands {
ids[i] = s.broker.NextId()
srv := commandServer{impl: command, broker: s.broker}
go s.broker.AcceptAndServe(ids[i], func(opts []grpc.ServerOption) *grpc.Server {
server := grpc.NewServer(opts...)
bacotellpb.RegisterCommandServer(server, srv)
return server
})
}
return &bacotellpb.PluginApplicationCommandsResponse{CommandIds: ids}, nil
}
// MessageComponents implements bacotellpb.PluginServer.
func (s pluginServer) MessageComponents(context.Context, *bacotellpb.PluginMessageComponentsRequest) (*bacotellpb.PluginMessageComponentsResponse, error) {
components, err := s.impl.MessageComponents()
if err != nil {
return nil, err
}
ids := make([]uint32, len(components))
for i, component := range components {
ids[i] = s.broker.NextId()
srv := componentServer{impl: component, broker: s.broker}
go s.broker.AcceptAndServe(ids[i], func(opts []grpc.ServerOption) *grpc.Server {
server := grpc.NewServer(opts...)
bacotellpb.RegisterComponentServer(server, srv)
return server
})
}
return &bacotellpb.PluginMessageComponentsResponse{ComponentIds: ids}, nil
}
// Modals implements bacotellpb.PluginServer.
func (s pluginServer) Modals(context.Context, *bacotellpb.PluginModalsRequest) (*bacotellpb.PluginModalsResponse, error) {
modals, err := s.impl.Modals()
if err != nil {
return nil, err
}
ids := make([]uint32, len(modals))
for i, modal := range modals {
ids[i] = s.broker.NextId()
srv := modalServer{impl: modal, broker: s.broker}
go s.broker.AcceptAndServe(ids[i], func(opts []grpc.ServerOption) *grpc.Server {
server := grpc.NewServer(opts...)
bacotellpb.RegisterModalServer(server, srv)
return server
})
}
return &bacotellpb.PluginModalsResponse{ModalIds: ids}, nil
}
// The client implementation of bacotell_common.Plugin.
type pluginClient struct {
client bacotellpb.PluginClient
broker *plugin.GRPCBroker
}
// pluginClient implements bacotell_common.Plugin.
var _ common.Plugin = pluginClient{}
// ID implements bacotell_common.Plugin.
func (c pluginClient) ID() (string, error) {
res, err := c.client.Id(context.Background(), &bacotellpb.PluginIdRequest{})
if err != nil {
return "", err
}
return res.Id, nil
}
// ApplicationCommands implements bacotell_common.Plugin.
func (c pluginClient) ApplicationCommands() ([]common.Command, error) {
res, err := c.client.ApplicationCommands(context.Background(), &bacotellpb.PluginApplicationCommandsRequest{})
if err != nil {
return nil, err
}
commands := make([]common.Command, len(res.CommandIds))
for i, id := range res.CommandIds {
conn, err := c.broker.Dial(id)
if err != nil {
return nil, err
}
commands[i] = &commandClient{client: bacotellpb.NewCommandClient(conn), broker: c.broker}
}
return commands, nil
}
// MessageComponents implements bacotell_common.Plugin.
func (c pluginClient) MessageComponents() ([]common.Component, error) {
res, err := c.client.MessageComponents(context.Background(), &bacotellpb.PluginMessageComponentsRequest{})
if err != nil {
return nil, err
}
components := make([]common.Component, len(res.ComponentIds))
for i, id := range res.ComponentIds {
conn, err := c.broker.Dial(id)
if err != nil {
return nil, err
}
components[i] = &componentClient{client: bacotellpb.NewComponentClient(conn), broker: c.broker}
}
return components, nil
}
// Modals implements bacotell_common.Plugin.
func (c pluginClient) Modals() ([]common.Modal, error) {
res, err := c.client.Modals(context.Background(), &bacotellpb.PluginModalsRequest{})
if err != nil {
return nil, err
}
modals := make([]common.Modal, len(res.ModalIds))
for i, id := range res.ModalIds {
conn, err := c.broker.Dial(id)
if err != nil {
return nil, err
}
modals[i] = &modalClient{client: bacotellpb.NewModalClient(conn), broker: c.broker}
}
return modals, nil
}