@@ -83,25 +83,6 @@ func (m *GRPCServer) Setup(
83
83
return & proto.SetupRes {}, m .Impl .Setup (config )
84
84
}
85
85
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
-
105
86
// CustomCommands translates the gRPC call to the Plugin CustomCommands
106
87
// implementation. It returns a list of commands that the plugin implements.
107
88
func (m * GRPCServer ) CustomCommands (
@@ -140,6 +121,25 @@ func (m *GRPCServer) ExecuteCustomCommand(
140
121
m .commandManager .Execute (req .CustomCommand , ctx , req .Args )
141
122
}
142
123
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
+
143
143
// PostTestHook translates the gRPC call to the Plugin PostTestHook
144
144
// implementation. It starts a prompt runner that is passed to the Plugin
145
145
// instance to be able to perform prompt actions to the CLI user.
@@ -206,31 +206,6 @@ func (m *GRPCClient) Setup(config *SetupConfig) error {
206
206
return err
207
207
}
208
208
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
-
234
209
// CustomCommands is called from the Core to execute the Plugin CustomCommands.
235
210
// It returns a list of commands that the plugin implements.
236
211
func (m * GRPCClient ) CustomCommands () ([]* Command , error ) {
@@ -259,34 +234,33 @@ func (m *GRPCClient) ExecuteCustomCommand(customCommand string, ctx context.Cont
259
234
return err
260
235
}
261
236
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
+
262
243
// PostTestHook is called from the Core to execute the Plugin PostTestHook. It
263
244
// starts the prompt runner server with the provided PromptRunner.
264
245
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 )
285
247
}
286
248
287
249
// PostRunHook is called from the Core to execute the Plugin PostRunHook. It
288
250
// starts the prompt runner server with the provided PromptRunner.
289
251
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 {
290
264
prompterServer := & PrompterGRPCServer {promptRunner : promptRunner }
291
265
var s * grpc.Server
292
266
var wg sync.WaitGroup
@@ -297,14 +271,14 @@ func (m *GRPCClient) PostRunHook(isInteractiveMode bool, promptRunner ioutils.Pr
297
271
defer wg .Done ()
298
272
return s
299
273
}
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 {
303
277
BrokerId : brokerID ,
304
278
IsInteractiveMode : isInteractiveMode ,
305
279
}
306
280
wg .Wait ()
307
- _ , err := m . client . PostRunHook (context .Background (), req )
281
+ _ , err := callFn (context .Background (), req )
308
282
s .Stop ()
309
283
return err
310
284
}
0 commit comments