Skip to content

Commit

Permalink
comments and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Oct 10, 2024
1 parent e6a66db commit 8fc445b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions frontend/cli/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type newCmd struct {
// - help text (ftl new go --help)
// - default values
// - environment variable overrides
//
// Language plugins take time to launch, so we return the one we created so it can be reused in Run().
func prepareNewCmd(ctx context.Context, k *kong.Kong, args []string) (optionalPlugin optional.Option[languageplugin.LanguagePlugin], err error) {
if len(args) < 2 {
return optionalPlugin, nil
Expand Down
6 changes: 4 additions & 2 deletions frontend/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ func main() {
csm := &currentStatusManager{}

app := createKongApplication(&cli, csm)
languagePlugin, err := prepareNewCmd(ctx, app, os.Args[1:])

// Dynamically update the kong app with language specific flags for the "ftl new" command.
languagePlugin, err := prepareNewCmd(log.ContextWithNewDefaultLogger(ctx), app, os.Args[1:])
app.FatalIfErrorf(err)

kctx, err := app.Parse(os.Args[1:])
app.FatalIfErrorf(err)

if plugin, ok := languagePlugin.Get(); ok {
// for "ftl new" command, we only need to create the language plugin once
// Plugins take time to launch, so we bind the "ftl new" plugin to the kong context.
kctx.BindTo(plugin, (*languageplugin.LanguagePlugin)(nil))
}

Expand Down
2 changes: 2 additions & 0 deletions internal/buildengine/languageplugin/external_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (p *externalPlugin) CreateModule(ctx context.Context, projConfig projectcon
Name: moduleConfig.Module,
Path: moduleConfig.Dir,
ProjectConfig: &langpb.ProjectConfig{
Path: projConfig.Path,
Name: projConfig.Name,
NoGit: projConfig.NoGit,
Hermit: projConfig.Hermit,
},
Expand Down
36 changes: 29 additions & 7 deletions internal/buildengine/languageplugin/external_plugin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,42 @@ func (p *externalPluginImpl) ping(ctx context.Context) error {
}

func (p *externalPluginImpl) kill() error {
// TODO: cancel run() ctx
return p.cmd.Kill(syscall.SIGINT)
if err := p.cmd.Kill(syscall.SIGINT); err != nil {
return fmt.Errorf("failed to kill language plugin: %w", err)
}
return nil
}

func (p *externalPluginImpl) getCreateModuleFlags(ctx context.Context, req *connect.Request[langpb.GetCreateModuleFlagsRequest]) (*connect.Response[langpb.GetCreateModuleFlagsResponse], error) {
return p.client.GetCreateModuleFlags(ctx, req)
resp, err := p.client.GetCreateModuleFlags(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get create module flags from plugin: %w", err)
}
return resp, nil
}

func (p *externalPluginImpl) moduleConfigDefaults(ctx context.Context, req *connect.Request[langpb.ModuleConfigDefaultsRequest]) (*connect.Response[langpb.ModuleConfigDefaultsResponse], error) {
return p.client.ModuleConfigDefaults(ctx, req)
resp, err := p.client.ModuleConfigDefaults(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get module config defaults from plugin: %w", err)
}
return resp, nil
}

func (p *externalPluginImpl) createModule(ctx context.Context, req *connect.Request[langpb.CreateModuleRequest]) (*connect.Response[langpb.CreateModuleResponse], error) {
return p.client.CreateModule(ctx, req)
resp, err := p.client.CreateModule(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to create module: %w", err)
}
return resp, nil
}

func (p *externalPluginImpl) getDependencies(ctx context.Context, req *connect.Request[langpb.DependenciesRequest]) (*connect.Response[langpb.DependenciesResponse], error) {
return p.client.GetDependencies(ctx, req)
resp, err := p.client.GetDependencies(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get dependencies from plugin: %w", err)
}
return resp, nil
}

func (p *externalPluginImpl) build(ctx context.Context, req *connect.Request[langpb.BuildRequest]) (chan either.Either[*langpb.BuildEvent, error], streamCancelFunc, error) {
Expand Down Expand Up @@ -152,5 +170,9 @@ func streamToChan(stream *connect.ServerStreamForClient[langpb.BuildEvent], ch c
}

func (p *externalPluginImpl) buildContextUpdated(ctx context.Context, req *connect.Request[langpb.BuildContextUpdatedRequest]) (*connect.Response[langpb.BuildContextUpdatedResponse], error) {
return p.client.BuildContextUpdated(ctx, req)
resp, err := p.client.BuildContextUpdated(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to send updated build context to plugin: %w", err)
}
return resp, nil
}
3 changes: 2 additions & 1 deletion internal/buildengine/languageplugin/external_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *mockExternalPluginClient) getDependencies(context.Context, *connect.Req
func buildContextFromProto(proto *langpb.BuildContext) (BuildContext, error) {
sch, err := schema.FromProto(proto.Schema)
if err != nil {
return BuildContext{}, err
return BuildContext{}, fmt.Errorf("could not load schema from build context proto: %w", err)
}
return BuildContext{
Schema: sch,
Expand Down Expand Up @@ -133,6 +133,7 @@ func setUp() (context.Context, *externalPlugin, *mockExternalPluginClient, Build
}

func TestCreateModuleFlags(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
protoFlags []*langpb.GetCreateModuleFlagsResponse_Flag
expectedFlags []*kong.Flag
Expand Down
5 changes: 3 additions & 2 deletions internal/buildengine/languageplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ type AutoRebuildEndedEvent struct {
func (AutoRebuildEndedEvent) pluginEvent() {}
func (e AutoRebuildEndedEvent) ModuleName() string { return e.Module }

// TODO: docs
// BuildContext contains contextual information needed to build.
//
// Any change to the build context would require a new build.
type BuildContext struct {
Config moduleconfig.ModuleConfig
Schema *schema.Schema
Expand Down Expand Up @@ -94,7 +96,6 @@ type LanguagePlugin interface {
// Build builds the module with the latest config and schema.
// In dev mode, plugin is responsible for automatically rebuilding as relevant files within the module change,
// and publishing these automatic builds updates to Updates().
// TODO: build env needed?
Build(ctx context.Context, projectRoot string, bctx BuildContext, buildEnv []string, rebuildAutomatically bool) (BuildResult, error)

// Kill stops the plugin and cleans up any resources.
Expand Down

0 comments on commit 8fc445b

Please sign in to comment.