Skip to content

Commit

Permalink
refactor: simplify terminal.KongContextBinder (#2819)
Browse files Browse the repository at this point in the history
Instead of passing around all the parameters required manually, we
capture them in a closure once at startup.
  • Loading branch information
alecthomas authored Sep 25, 2024
1 parent 57eea5b commit 4b74557
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions frontend/cli/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type devCmd struct {
Build buildCmd `embed:""`
}

func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig.Config, cancel context.CancelFunc) error {
func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig.Config, bindContext terminal.KongContextBinder) error {
startTime := time.Now()
if len(d.Build.Dirs) == 0 {
d.Build.Dirs = projConfig.AbsModuleDirs()
Expand All @@ -39,7 +39,7 @@ func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig
}

client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
terminal.LaunchEmbeddedConsole(ctx, k, projConfig, bindContext, cancel, client)
terminal.LaunchEmbeddedConsole(ctx, k, bindContext, client)

g, ctx := errgroup.WithContext(ctx)

Expand Down
2 changes: 1 addition & 1 deletion frontend/cli/cmd_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type interactiveCmd struct {
}

func (i *interactiveCmd) Run(ctx context.Context, k *kong.Kong, projectConfig projectconfig.Config, binder terminal.KongContextBinder, cancel context.CancelFunc, client ftlv1connect.ControllerServiceClient) error {
err := terminal.RunInteractiveConsole(ctx, k, projectConfig, binder, cancel, client)
err := terminal.RunInteractiveConsole(ctx, k, binder, client)
if err != nil {
return fmt.Errorf("interactive console: %w", err)
}
Expand Down
39 changes: 20 additions & 19 deletions frontend/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func main() {
if err != nil && !errors.Is(err, os.ErrNotExist) {
kctx.FatalIfErrorf(err)
}
ctx = bindContext(ctx, kctx, config, createKongApplication(&InteractiveCLI{}), cancel)
bindContext := makeBindContext(config, cancel)
ctx = bindContext(ctx, kctx)

err = kctx.Run(ctx)
kctx.FatalIfErrorf(err)
Expand Down Expand Up @@ -150,26 +151,26 @@ func createKongApplication(cli any) *kong.Kong {
return app
}

var _ terminal.KongContextBinder = bindContext
func makeBindContext(projectConfig projectconfig.Config, cancel context.CancelFunc) terminal.KongContextBinder {
var bindContext terminal.KongContextBinder
bindContext = func(ctx context.Context, kctx *kong.Context) context.Context {
kctx.Bind(projectConfig)

func bindContext(ctx context.Context, kctx *kong.Context, projectConfig projectconfig.Config, app *kong.Kong, cancel context.CancelFunc) context.Context {
controllerServiceClient := rpc.Dial(ftlv1connect.NewControllerServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, controllerServiceClient)
kctx.BindTo(controllerServiceClient, (*ftlv1connect.ControllerServiceClient)(nil))

kctx.Bind(projectConfig)
kctx.Bind(app)
kongcompletion.Register(kctx.Kong, kongcompletion.WithPredictors(terminal.Predictors(ctx, controllerServiceClient)))

controllerServiceClient := rpc.Dial(ftlv1connect.NewControllerServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, controllerServiceClient)
kctx.BindTo(controllerServiceClient, (*ftlv1connect.ControllerServiceClient)(nil))
verbServiceClient := rpc.Dial(ftlv1connect.NewVerbServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, verbServiceClient)
kctx.BindTo(verbServiceClient, (*ftlv1connect.VerbServiceClient)(nil))

kongcompletion.Register(app, kongcompletion.WithPredictors(terminal.Predictors(ctx, controllerServiceClient)))

verbServiceClient := rpc.Dial(ftlv1connect.NewVerbServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, verbServiceClient)
kctx.BindTo(verbServiceClient, (*ftlv1connect.VerbServiceClient)(nil))

kctx.Bind(cli.Endpoint)
kctx.BindTo(ctx, (*context.Context)(nil))
kctx.BindTo(bindContext, (*terminal.KongContextBinder)(nil))
kctx.BindTo(cancel, (*context.CancelFunc)(nil))
return ctx
kctx.Bind(cli.Endpoint)
kctx.BindTo(ctx, (*context.Context)(nil))
kctx.Bind(bindContext)
kctx.BindTo(cancel, (*context.CancelFunc)(nil))
return ctx
}
return bindContext
}
7 changes: 3 additions & 4 deletions internal/terminal/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import (
"github.com/posener/complete"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/internal/projectconfig"
)

const interactivePrompt = "\033[32m>\033[0m "

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

type KongContextBinder func(ctx context.Context, kctx *kong.Context, projectConfig projectconfig.Config, app *kong.Kong, cancel context.CancelFunc) context.Context
type KongContextBinder func(ctx context.Context, kctx *kong.Context) context.Context

func RunInteractiveConsole(ctx context.Context, k *kong.Kong, projectConfig projectconfig.Config, binder KongContextBinder, cancelContext context.CancelFunc, client ftlv1connect.ControllerServiceClient) error {
func RunInteractiveConsole(ctx context.Context, k *kong.Kong, binder KongContextBinder, client ftlv1connect.ControllerServiceClient) error {

l, err := readline.NewEx(&readline.Config{
Prompt: interactivePrompt,
Expand Down Expand Up @@ -90,7 +89,7 @@ func RunInteractiveConsole(ctx context.Context, k *kong.Kong, projectConfig proj
errorf("%s", err)
return
}
subctx := binder(ctx, kctx, projectConfig, k, cancelContext)
subctx := binder(ctx, kctx)

err = kctx.Run(subctx)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/terminal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/projectconfig"
)

type BuildState string
Expand Down Expand Up @@ -489,11 +488,11 @@ func (r *terminalStatusLine) SetMessage(message string) {
r.manager.recalculateLines()
}

func LaunchEmbeddedConsole(ctx context.Context, k *kong.Kong, projectConfig projectconfig.Config, binder KongContextBinder, cancel context.CancelFunc, client ftlv1connect.ControllerServiceClient) {
func LaunchEmbeddedConsole(ctx context.Context, k *kong.Kong, binder KongContextBinder, client ftlv1connect.ControllerServiceClient) {
sm := FromContext(ctx)
if _, ok := sm.(*terminalStatusManager); ok {
go func() {
err := RunInteractiveConsole(ctx, k, projectConfig, binder, cancel, client)
err := RunInteractiveConsole(ctx, k, binder, client)
if err != nil {
fmt.Printf("\033[31mError: %s\033[0m\n", err)
return
Expand Down

0 comments on commit 4b74557

Please sign in to comment.