9
9
"io"
10
10
"net/http"
11
11
"os"
12
- "reflect"
13
12
"time"
14
13
15
14
"code.gitea.io/gitea/modules/private"
@@ -269,12 +268,7 @@ func determineOutput(c *cli.Context, defaultFilename string) (io.WriteCloser, er
269
268
return out , nil
270
269
}
271
270
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 {
271
+ func wrapManagerPrivateFunc (c * cli.Context , defaultOutput string , fn func (ctx context.Context , out io.Writer ) (int , string )) error {
278
272
ctx , cancel := setupManager (c )
279
273
defer cancel ()
280
274
@@ -284,17 +278,7 @@ func runManagerPrivateFunc(c *cli.Context, defaultOutput string, fn interface{},
284
278
}
285
279
defer out .Close ()
286
280
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 )
281
+ statusCode , msg := fn (ctx , out )
298
282
switch statusCode {
299
283
case http .StatusInternalServerError :
300
284
return fail ("InternalServerError" , msg )
@@ -304,25 +288,37 @@ func runManagerPrivateFunc(c *cli.Context, defaultOutput string, fn interface{},
304
288
}
305
289
306
290
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" ))
291
+ return wrapManagerPrivateFunc (c , "-" , func (ctx context.Context , out io.Writer ) (int , string ) {
292
+ return private .Processes (ctx , out , c .Bool ("flat" ), c .Bool ("no-system" ), c .Bool ("stacktraces" ), c .Bool ("json" ), c .String ("cancel" ))
293
+ })
308
294
}
309
295
310
296
func runCPUProfile (c * cli.Context ) error {
311
- return runManagerPrivateFunc (c , "cpu-profile" , private .CPUProfile , c .Duration ("duration" ))
297
+ return wrapManagerPrivateFunc (c , "cpu-profile" , func (ctx context.Context , out io.Writer ) (int , string ) {
298
+ return private .CPUProfile (ctx , out , c .Duration ("duration" ))
299
+ })
312
300
}
313
301
314
302
func runFGProfile (c * cli.Context ) error {
315
- return runManagerPrivateFunc (c , "fg-profile" , private .FGProfile , c .Duration ("duration" ), c .String ("format" ))
303
+ return wrapManagerPrivateFunc (c , "fg-profile" , func (ctx context.Context , out io.Writer ) (int , string ) {
304
+ return private .FGProfile (ctx , out , c .Duration ("duration" ), c .String ("format" ))
305
+ })
316
306
}
317
307
318
308
func runNamedProfile (c * cli.Context ) error {
319
- return runManagerPrivateFunc (c , c .String ("name" )+ "-profile" , private .NamedProfile , c .String ("name" ), c .Int ("debug-level" ))
309
+ return wrapManagerPrivateFunc (c , c .String ("name" )+ "-profile" , func (ctx context.Context , out io.Writer ) (int , string ) {
310
+ return private .NamedProfile (ctx , out , c .String ("name" ), c .Int ("debug-level" ))
311
+ })
320
312
}
321
313
322
314
func runListNamedProfile (c * cli.Context ) error {
323
- return runManagerPrivateFunc (c , "-" , private .ListNamedProfiles , c .Bool ("json" ))
315
+ return wrapManagerPrivateFunc (c , "-" , func (ctx context.Context , out io.Writer ) (int , string ) {
316
+ return private .ListNamedProfiles (ctx , out , c .Bool ("json" ))
317
+ })
324
318
}
325
319
326
320
func runTrace (c * cli.Context ) error {
327
- return runManagerPrivateFunc (c , "trace" , private .Trace , c .Duration ("duration" ))
321
+ return wrapManagerPrivateFunc (c , "trace" , func (ctx context.Context , out io.Writer ) (int , string ) {
322
+ return private .Trace (ctx , out , c .Duration ("duration" ))
323
+ })
328
324
}
0 commit comments