Skip to content

Commit

Permalink
fix: --help in the interactive console wasn't working (#2733)
Browse files Browse the repository at this point in the history
It calls Kong's Exit() hook, so we now override it.
  • Loading branch information
alecthomas authored Sep 19, 2024
1 parent 5d1f5f7 commit 9f9c28f
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions frontend/cli/cmd_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

var _ readline.AutoCompleter = &FTLCompletion{}
var errExitTrap = errors.New("exit trap")

type interactiveCmd struct {
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit 9f9c28f

Please sign in to comment.