Skip to content

Commit 1400ab2

Browse files
committed
as per delvh
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 61755a7 commit 1400ab2

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

cmd/manager.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"net/http"
1111
"os"
12-
"reflect"
1312
"time"
1413

1514
"code.gitea.io/gitea/modules/private"
@@ -269,12 +268,7 @@ func determineOutput(c *cli.Context, defaultFilename string) (io.WriteCloser, er
269268
return out, nil
270269
}
271270

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 {
278272
ctx, cancel := setupManager(c)
279273
defer cancel()
280274

@@ -284,17 +278,7 @@ func runManagerPrivateFunc(c *cli.Context, defaultOutput string, fn interface{},
284278
}
285279
defer out.Close()
286280

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)
298282
switch statusCode {
299283
case http.StatusInternalServerError:
300284
return fail("InternalServerError", msg)
@@ -304,25 +288,37 @@ func runManagerPrivateFunc(c *cli.Context, defaultOutput string, fn interface{},
304288
}
305289

306290
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+
})
308294
}
309295

310296
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+
})
312300
}
313301

314302
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+
})
316306
}
317307

318308
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+
})
320312
}
321313

322314
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+
})
324318
}
325319

326320
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+
})
328324
}

0 commit comments

Comments
 (0)