Skip to content

Commit 7220b77

Browse files
f0rmigaalexeagle
authored andcommitted
refactor: reuse hook call logic (#780)
Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com> GitOrigin-RevId: 6a7c3eceaebdf79acc5771324f0e72d30c6cfe37
1 parent ed4349c commit 7220b77

File tree

1 file changed

+42
-68
lines changed
  • pkg/plugin/sdk/v1alpha3/plugin

1 file changed

+42
-68
lines changed

pkg/plugin/sdk/v1alpha3/plugin/grpc.go

Lines changed: 42 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,6 @@ func (m *GRPCServer) Setup(
8383
return &proto.SetupRes{}, m.Impl.Setup(config)
8484
}
8585

86-
// PostBuildHook translates the gRPC call to the Plugin PostBuildHook
87-
// implementation. It starts a prompt runner that is passed to the Plugin
88-
// instance to be able to perform prompt actions to the CLI user.
89-
func (m *GRPCServer) PostBuildHook(
90-
ctx context.Context,
91-
req *proto.PostBuildHookReq,
92-
) (*proto.PostBuildHookRes, error) {
93-
conn, err := m.broker.Dial(req.BrokerId)
94-
if err != nil {
95-
return nil, err
96-
}
97-
defer conn.Close()
98-
99-
client := proto.NewPrompterClient(conn)
100-
prompter := &PrompterGRPCClient{client: client}
101-
return &proto.PostBuildHookRes{},
102-
m.Impl.PostBuildHook(req.IsInteractiveMode, prompter)
103-
}
104-
10586
// CustomCommands translates the gRPC call to the Plugin CustomCommands
10687
// implementation. It returns a list of commands that the plugin implements.
10788
func (m *GRPCServer) CustomCommands(
@@ -140,6 +121,25 @@ func (m *GRPCServer) ExecuteCustomCommand(
140121
m.commandManager.Execute(req.CustomCommand, ctx, req.Args)
141122
}
142123

124+
// PostBuildHook translates the gRPC call to the Plugin PostBuildHook
125+
// implementation. It starts a prompt runner that is passed to the Plugin
126+
// instance to be able to perform prompt actions to the CLI user.
127+
func (m *GRPCServer) PostBuildHook(
128+
ctx context.Context,
129+
req *proto.PostBuildHookReq,
130+
) (*proto.PostBuildHookRes, error) {
131+
conn, err := m.broker.Dial(req.BrokerId)
132+
if err != nil {
133+
return nil, err
134+
}
135+
defer conn.Close()
136+
137+
client := proto.NewPrompterClient(conn)
138+
prompter := &PrompterGRPCClient{client: client}
139+
return &proto.PostBuildHookRes{},
140+
m.Impl.PostBuildHook(req.IsInteractiveMode, prompter)
141+
}
142+
143143
// PostTestHook translates the gRPC call to the Plugin PostTestHook
144144
// implementation. It starts a prompt runner that is passed to the Plugin
145145
// instance to be able to perform prompt actions to the CLI user.
@@ -206,31 +206,6 @@ func (m *GRPCClient) Setup(config *SetupConfig) error {
206206
return err
207207
}
208208

209-
// PostBuildHook is called from the Core to execute the Plugin PostBuildHook. It
210-
// starts the prompt runner server with the provided PromptRunner.
211-
func (m *GRPCClient) PostBuildHook(isInteractiveMode bool, promptRunner ioutils.PromptRunner) error {
212-
prompterServer := &PrompterGRPCServer{promptRunner: promptRunner}
213-
var s *grpc.Server
214-
var wg sync.WaitGroup
215-
wg.Add(1)
216-
serverFunc := func(opts []grpc.ServerOption) *grpc.Server {
217-
s = grpc.NewServer(opts...)
218-
proto.RegisterPrompterServer(s, prompterServer)
219-
defer wg.Done()
220-
return s
221-
}
222-
brokerID := m.broker.NextId()
223-
go m.broker.AcceptAndServe(brokerID, serverFunc)
224-
req := &proto.PostBuildHookReq{
225-
BrokerId: brokerID,
226-
IsInteractiveMode: isInteractiveMode,
227-
}
228-
wg.Wait()
229-
_, err := m.client.PostBuildHook(context.Background(), req)
230-
s.Stop()
231-
return err
232-
}
233-
234209
// CustomCommands is called from the Core to execute the Plugin CustomCommands.
235210
// It returns a list of commands that the plugin implements.
236211
func (m *GRPCClient) CustomCommands() ([]*Command, error) {
@@ -259,34 +234,33 @@ func (m *GRPCClient) ExecuteCustomCommand(customCommand string, ctx context.Cont
259234
return err
260235
}
261236

237+
// PostBuildHook is called from the Core to execute the Plugin PostBuildHook. It
238+
// starts the prompt runner server with the provided PromptRunner.
239+
func (m *GRPCClient) PostBuildHook(isInteractiveMode bool, promptRunner ioutils.PromptRunner) error {
240+
return callClientHook(m.broker, m.client.PostBuildHook, isInteractiveMode, promptRunner)
241+
}
242+
262243
// PostTestHook is called from the Core to execute the Plugin PostTestHook. It
263244
// starts the prompt runner server with the provided PromptRunner.
264245
func (m *GRPCClient) PostTestHook(isInteractiveMode bool, promptRunner ioutils.PromptRunner) error {
265-
prompterServer := &PrompterGRPCServer{promptRunner: promptRunner}
266-
var s *grpc.Server
267-
var wg sync.WaitGroup
268-
wg.Add(1)
269-
serverFunc := func(opts []grpc.ServerOption) *grpc.Server {
270-
s = grpc.NewServer(opts...)
271-
proto.RegisterPrompterServer(s, prompterServer)
272-
defer wg.Done()
273-
return s
274-
}
275-
brokerID := m.broker.NextId()
276-
go m.broker.AcceptAndServe(brokerID, serverFunc)
277-
req := &proto.PostTestHookReq{
278-
BrokerId: brokerID,
279-
IsInteractiveMode: isInteractiveMode,
280-
}
281-
wg.Wait()
282-
_, err := m.client.PostTestHook(context.Background(), req)
283-
s.Stop()
284-
return err
246+
return callClientHook(m.broker, m.client.PostTestHook, isInteractiveMode, promptRunner)
285247
}
286248

287249
// PostRunHook is called from the Core to execute the Plugin PostRunHook. It
288250
// starts the prompt runner server with the provided PromptRunner.
289251
func (m *GRPCClient) PostRunHook(isInteractiveMode bool, promptRunner ioutils.PromptRunner) error {
252+
return callClientHook(m.broker, m.client.PostRunHook, isInteractiveMode, promptRunner)
253+
}
254+
255+
func callClientHook[
256+
ReqT proto.PostBuildHookReq | proto.PostTestHookReq | proto.PostRunHookReq,
257+
ResT proto.PostBuildHookRes | proto.PostTestHookRes | proto.PostRunHookRes,
258+
](
259+
broker *goplugin.GRPCBroker,
260+
callFn func(context.Context, *ReqT, ...grpc.CallOption) (*ResT, error),
261+
isInteractiveMode bool,
262+
promptRunner ioutils.PromptRunner,
263+
) error {
290264
prompterServer := &PrompterGRPCServer{promptRunner: promptRunner}
291265
var s *grpc.Server
292266
var wg sync.WaitGroup
@@ -297,14 +271,14 @@ func (m *GRPCClient) PostRunHook(isInteractiveMode bool, promptRunner ioutils.Pr
297271
defer wg.Done()
298272
return s
299273
}
300-
brokerID := m.broker.NextId()
301-
go m.broker.AcceptAndServe(brokerID, serverFunc)
302-
req := &proto.PostRunHookReq{
274+
brokerID := broker.NextId()
275+
go broker.AcceptAndServe(brokerID, serverFunc)
276+
req := &ReqT{
303277
BrokerId: brokerID,
304278
IsInteractiveMode: isInteractiveMode,
305279
}
306280
wg.Wait()
307-
_, err := m.client.PostRunHook(context.Background(), req)
281+
_, err := callFn(context.Background(), req)
308282
s.Stop()
309283
return err
310284
}

0 commit comments

Comments
 (0)