4
4
package cmd
5
5
6
6
import (
7
+ "context"
7
8
"fmt"
8
9
"io"
9
10
"net/http"
10
11
"os"
12
+ "reflect"
11
13
"time"
12
14
13
15
"code.gitea.io/gitea/modules/private"
@@ -201,11 +203,17 @@ var (
201
203
}
202
204
)
203
205
204
- func runShutdown (c * cli.Context ) error {
206
+ func setupManager (c * cli.Context ) (context. Context , context. CancelFunc ) {
205
207
ctx , cancel := installSignals ()
206
- defer cancel ()
207
208
208
209
setup ("manager" , c .Bool ("debug" ))
210
+ return ctx , cancel
211
+ }
212
+
213
+ func runShutdown (c * cli.Context ) error {
214
+ ctx , cancel := setupManager (c )
215
+ defer cancel ()
216
+
209
217
statusCode , msg := private .Shutdown (ctx )
210
218
switch statusCode {
211
219
case http .StatusInternalServerError :
@@ -217,10 +225,9 @@ func runShutdown(c *cli.Context) error {
217
225
}
218
226
219
227
func runRestart (c * cli.Context ) error {
220
- ctx , cancel := installSignals ( )
228
+ ctx , cancel := setupManager ( c )
221
229
defer cancel ()
222
230
223
- setup ("manager" , c .Bool ("debug" ))
224
231
statusCode , msg := private .Restart (ctx )
225
232
switch statusCode {
226
233
case http .StatusInternalServerError :
@@ -232,10 +239,9 @@ func runRestart(c *cli.Context) error {
232
239
}
233
240
234
241
func runFlushQueues (c * cli.Context ) error {
235
- ctx , cancel := installSignals ( )
242
+ ctx , cancel := setupManager ( c )
236
243
defer cancel ()
237
244
238
- setup ("manager" , c .Bool ("debug" ))
239
245
statusCode , msg := private .FlushQueues (ctx , c .Duration ("timeout" ), c .Bool ("non-blocking" ))
240
246
switch statusCode {
241
247
case http .StatusInternalServerError :
@@ -263,18 +269,32 @@ func determineOutput(c *cli.Context, defaultFilename string) (io.WriteCloser, er
263
269
return out , nil
264
270
}
265
271
266
- func runProcesses (c * cli.Context ) error {
267
- ctx , cancel := installSignals ()
272
+ // runManagerPrivateFunc will requires that a provided fn has an interface:
273
+ //
274
+ // func(context.Context, io.Writer, ...argsTypes) (int, string) {
275
+ //
276
+ // but this cann't quite easily be expressed as a generic type
277
+ func runManagerPrivateFunc (c * cli.Context , defaultOutput string , fn interface {}, args ... any ) error {
278
+ ctx , cancel := setupManager (c )
268
279
defer cancel ()
269
280
270
- setup ("manager" , c .Bool ("debug" ))
271
- out , err := determineOutput (c , "-" )
281
+ out , err := determineOutput (c , defaultOutput )
272
282
if err != nil {
273
283
return err
274
284
}
275
285
defer out .Close ()
276
286
277
- statusCode , msg := private .Processes (ctx , out , c .Bool ("flat" ), c .Bool ("no-system" ), c .Bool ("stacktraces" ), c .Bool ("json" ), c .String ("cancel" ))
287
+ valFn := reflect .ValueOf (fn )
288
+ callArgs := []reflect.Value {
289
+ reflect .ValueOf (ctx ),
290
+ reflect .ValueOf (out ),
291
+ }
292
+ for _ , arg := range args {
293
+ callArgs = append (callArgs , reflect .ValueOf (arg ))
294
+ }
295
+ outArgs := valFn .Call (callArgs )
296
+
297
+ statusCode , msg := outArgs [0 ].Interface ().(int ), outArgs [1 ].Interface ().(string )
278
298
switch statusCode {
279
299
case http .StatusInternalServerError :
280
300
return fail ("InternalServerError" , msg )
@@ -283,94 +303,26 @@ func runProcesses(c *cli.Context) error {
283
303
return nil
284
304
}
285
305
286
- func runCPUProfile (c * cli.Context ) error {
287
- ctx , cancel := installSignals ()
288
- defer cancel ()
289
- setup ("manager" , c .Bool ("debug" ))
290
-
291
- out , err := determineOutput (c , "cpu-profile" )
292
- if err != nil {
293
- return err
294
- }
295
- defer out .Close ()
306
+ func runProcesses (c * cli.Context ) error {
307
+ return runManagerPrivateFunc (c , "-" , private .Processes , c .Bool ("flat" ), c .Bool ("no-system" ), c .Bool ("stacktraces" ), c .Bool ("json" ), c .String ("cancel" ))
308
+ }
296
309
297
- statusCode , msg := private .CPUProfile (ctx , out , c .Duration ("duration" ))
298
- switch statusCode {
299
- case http .StatusInternalServerError :
300
- return fail ("InternalServerError" , msg )
301
- }
302
- return nil
310
+ func runCPUProfile (c * cli.Context ) error {
311
+ return runManagerPrivateFunc (c , "cpu-profile" , private .CPUProfile , c .Duration ("duration" ))
303
312
}
304
313
305
314
func runFGProfile (c * cli.Context ) error {
306
- ctx , cancel := installSignals ()
307
- defer cancel ()
308
- setup ("manager" , c .Bool ("debug" ))
309
- out , err := determineOutput (c , "fg-profile" )
310
- if err != nil {
311
- return err
312
- }
313
- defer out .Close ()
314
-
315
- statusCode , msg := private .FGProfile (ctx , out , c .Duration ("duration" ), c .String ("format" ))
316
- switch statusCode {
317
- case http .StatusInternalServerError :
318
- return fail ("InternalServerError" , msg )
319
- }
320
- return nil
315
+ return runManagerPrivateFunc (c , "fg-profile" , private .FGProfile , c .Duration ("duration" ), c .String ("format" ))
321
316
}
322
317
323
318
func runNamedProfile (c * cli.Context ) error {
324
- ctx , cancel := installSignals ()
325
- defer cancel ()
326
- setup ("manager" , c .Bool ("debug" ))
327
- out , err := determineOutput (c , c .String ("name" )+ "-profile" )
328
- if err != nil {
329
- return err
330
- }
331
- defer out .Close ()
332
- statusCode , msg := private .NamedProfile (ctx , out , c .String ("name" ), c .Int ("debug-level" ))
333
- switch statusCode {
334
- case http .StatusInternalServerError :
335
- return fail ("InternalServerError" , msg )
336
- }
337
- return nil
319
+ return runManagerPrivateFunc (c , c .String ("name" )+ "-profile" , private .NamedProfile , c .String ("name" ), c .Int ("debug-level" ))
338
320
}
339
321
340
322
func runListNamedProfile (c * cli.Context ) error {
341
- ctx , cancel := installSignals ()
342
- defer cancel ()
343
- setup ("manager" , c .Bool ("debug" ))
344
-
345
- out , err := determineOutput (c , "-" )
346
- if err != nil {
347
- return err
348
- }
349
- defer out .Close ()
350
-
351
- statusCode , msg := private .ListNamedProfiles (ctx , out , c .Bool ("json" ))
352
- switch statusCode {
353
- case http .StatusInternalServerError :
354
- return fail ("InternalServerError" , msg )
355
- }
356
- return nil
323
+ return runManagerPrivateFunc (c , "-" , private .ListNamedProfiles , c .Bool ("json" ))
357
324
}
358
325
359
326
func runTrace (c * cli.Context ) error {
360
- ctx , cancel := installSignals ()
361
- defer cancel ()
362
- setup ("manager" , c .Bool ("debug" ))
363
-
364
- out , err := determineOutput (c , "trace" )
365
- if err != nil {
366
- return err
367
- }
368
- defer out .Close ()
369
-
370
- statusCode , msg := private .Trace (ctx , out , c .Duration ("duration" ))
371
- switch statusCode {
372
- case http .StatusInternalServerError :
373
- return fail ("InternalServerError" , msg )
374
- }
375
- return nil
327
+ return runManagerPrivateFunc (c , "trace" , private .Trace , c .Duration ("duration" ))
376
328
}
0 commit comments