Skip to content

Commit

Permalink
fix: add commands section to ftl-project (#1208)
Browse files Browse the repository at this point in the history
Fixes #1179 1179
  • Loading branch information
wesbillman authored Apr 9, 2024
1 parent 2424f2c commit 82ecace
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
15 changes: 14 additions & 1 deletion buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/common/projectconfig"
"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
)
Expand All @@ -43,6 +45,7 @@ type Engine struct {
projects map[ProjectKey]Project
moduleDirs []string
externalDirs []string
commands map[string]string
controllerSchema *xsync.MapOf[string, *schema.Module]
schemaChanges *pubsub.Topic[schemaChange]
cancel func()
Expand Down Expand Up @@ -206,7 +209,17 @@ func (e *Engine) Deploy(ctx context.Context, replicas int32, waitForDeployOnline
}

// Dev builds and deploys all local modules and watches for changes, redeploying as necessary.
func (e *Engine) Dev(ctx context.Context, period time.Duration) error {
func (e *Engine) Dev(ctx context.Context, period time.Duration, commands projectconfig.Commands) error {
logger := log.FromContext(ctx)
if len(commands.Startup) > 0 {
for _, cmd := range commands.Startup {
logger.Debugf("Executing startup command: %s", cmd)
if err := exec.Command(ctx, log.Info, ".", "bash", "-c", cmd).Run(); err != nil {
return fmt.Errorf("startup command failed: %w", err)
}
}
}

return e.watchForModuleChanges(ctx, period)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error
if err != nil {
return err
}
return engine.Dev(ctx, d.Watch)
return engine.Dev(ctx, d.Watch, projConfig.Commands)
})

return g.Wait()
Expand Down
10 changes: 8 additions & 2 deletions common/projectconfig/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ func mergeConfigAndSecrets(a, b ConfigAndSecrets) ConfigAndSecrets {
}

func mergeRootKeys(a, b Config) Config {
if a.ModuleDirs == nil {
if b.ModuleDirs != nil {
a.ModuleDirs = b.ModuleDirs
}
if a.ExternalDirs == nil {
if b.ExternalDirs != nil {
a.ExternalDirs = b.ExternalDirs
}
if b.Executables.FTL != "" {
a.Executables.FTL = b.Executables.FTL
}
if len(b.Commands.Startup) > 0 {
a.Commands.Startup = b.Commands.Startup
}
return a
}
24 changes: 24 additions & 0 deletions common/projectconfig/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func TestMerge(t *testing.T) {
},
},
},
ModuleDirs: []string{"a/b/c"},
ExternalDirs: []string{"e/f"},
Executables: Executables{
FTL: "ftl",
},
Commands: Commands{
Startup: []string{"echo 'Before'"},
},
}
b := Config{
Global: ConfigAndSecrets{
Expand All @@ -51,6 +59,14 @@ func TestMerge(t *testing.T) {
},
},
},
ModuleDirs: []string{"d"},
ExternalDirs: []string{"g/h"},
Executables: Executables{
FTL: "./bin/ftl",
},
Commands: Commands{
Startup: []string{"echo 'After'"},
},
}
a = merge(a, b)
expected := Config{
Expand All @@ -77,6 +93,14 @@ func TestMerge(t *testing.T) {
},
},
},
ModuleDirs: []string{"d"},
ExternalDirs: []string{"g/h"},
Executables: Executables{
FTL: "./bin/ftl",
},
Commands: Commands{
Startup: []string{"echo 'After'"},
},
}
assert.Equal(t, expected, a)
}
10 changes: 10 additions & 0 deletions common/projectconfig/projectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/TBD54566975/ftl/internal/log"
)

type Executables struct {
FTL string `toml:"ftl"`
}

type Commands struct {
Startup []string `toml:"startup"`
}

type ConfigAndSecrets struct {
Config map[string]*URL `toml:"configuration"`
Secrets map[string]*URL `toml:"secrets"`
Expand All @@ -23,6 +31,8 @@ type Config struct {
Modules map[string]ConfigAndSecrets `toml:"modules"`
ModuleDirs []string `toml:"module-dirs"`
ExternalDirs []string `toml:"external-dirs"`
Executables Executables `toml:"executables"`
Commands Commands `toml:"commands"`
}

// ConfigPaths returns the computed list of configuration paths to load.
Expand Down
6 changes: 6 additions & 0 deletions common/projectconfig/projectconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestProjectConfig(t *testing.T) {
},
ModuleDirs: []string{"a/b/c", "d"},
ExternalDirs: []string{"e/f", "g/h"},
Executables: Executables{
FTL: "ftl",
},
Commands: Commands{
Startup: []string{"echo 'Executing global pre-build command'"},
},
}

assert.Equal(t, expected, actual)
Expand Down
12 changes: 9 additions & 3 deletions common/projectconfig/testdata/ftl-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ module-dirs = ["a/b/c", "d"]
external-dirs = ["e/f", "g/h"]

[modules.module.configuration]
githubAccessToken = "keychain://githubAccessToken"
githubAccessToken = "keychain://githubAccessToken"

[modules.module.secrets]
encryptionKey = "inline://notASensitiveSecret"
companyApiKey = "op://devel/yj3jfj2vzsbiwqabprflnl27lm/companyApiKey"
encryptionKey = "inline://notASensitiveSecret"
companyApiKey = "op://devel/yj3jfj2vzsbiwqabprflnl27lm/companyApiKey"

[commands]
startup = ["echo 'Executing global pre-build command'"]

[executables]
ftl = "ftl"
3 changes: 3 additions & 0 deletions ftl-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ external-dirs = []
[modules.echo]
[modules.echo.configuration]
default = "inline://ImFub255bW91cyI"

[commands]
startup = ["echo 'FTL startup command ⚡️'"]

0 comments on commit 82ecace

Please sign in to comment.