From 9f9c28fc54ef340c38d7da81d4f38b6ff9c08be2 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 19 Sep 2024 23:36:52 +1000 Subject: [PATCH] fix: `--help` in the interactive console wasn't working (#2733) It calls Kong's Exit() hook, so we now override it. --- frontend/cli/cmd_interactive.go | 34 ++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/frontend/cli/cmd_interactive.go b/frontend/cli/cmd_interactive.go index df5debf97a..65aac5c15a 100644 --- a/frontend/cli/cmd_interactive.go +++ b/frontend/cli/cmd_interactive.go @@ -17,6 +17,7 @@ import ( ) var _ readline.AutoCompleter = &FTLCompletion{} +var errExitTrap = errors.New("exit trap") type interactiveCmd struct { } @@ -32,6 +33,8 @@ func (i *interactiveCmd) Run(ctx context.Context, k *kong.Kong, projectConfig pr return fmt.Errorf("init readline: %w", err) } l.CaptureExitSignal() + // Overload the exit function to avoid exiting the process + k.Exit = func(i int) { panic(errExitTrap) } for { line, err := l.Readline() if errors.Is(err, readline.ErrInterrupt) { @@ -51,18 +54,27 @@ func (i *interactiveCmd) Run(ctx context.Context, k *kong.Kong, projectConfig pr errorf("%s", err) continue } - kctx, err := k.Parse(args) - if err != nil { - errorf("%s", err) - continue - } - subctx := bindContext(ctx, kctx, projectConfig, app) + defer func() { + // Catch Exit() and continue the loop + if r := recover(); r != nil { + if r == errExitTrap { //nolint:errorlint + return + } + panic(r) + } + kctx, err := k.Parse(args) + if err != nil { + errorf("%s", err) + return + } + subctx := bindContext(ctx, kctx, projectConfig, app) - err = kctx.Run(subctx) - if err != nil { - errorf("error: %s", err) - continue - } + err = kctx.Run(subctx) + if err != nil { + errorf("error: %s", err) + return + } + }() } return nil }