Skip to content

Commit

Permalink
feat: create specified config files that do not exist (#1618)
Browse files Browse the repository at this point in the history
Fixes: #1561

```
$ rm ftl-project-idk.toml
$ ftl config set yo --inline value -C ftl-project-idk.toml
warn: Creating a new project config file at "ftl-project-idk.toml" because the file does not already exist
$ ftl config set yo --inline value -C ftl-project-idk.toml
$ stat ftl-project-idk.toml                         
16777232 3291117 -rw------- 1 dli staff 0 125 "Jun  3 22:44:16 2024" "Jun  3 22:44:15 2024" "Jun  3 22:44:15 2024" "Jun  3 22:44:15 2024" 4096 8 0 ftl-project-idk.toml
$
```
  • Loading branch information
deniseli authored Jun 4, 2024
1 parent d94495c commit d4c9753
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/ftl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"net/url"
"os"
"os/signal"
Expand Down Expand Up @@ -83,7 +84,7 @@ func main() {
ctx = log.ContextWithLogger(ctx, logger)

config, err := projectconfig.LoadConfig(ctx, cli.ConfigFlag)
if err != nil {
if err != nil && !errors.Is(err, os.ErrNotExist) {
kctx.Fatalf(err.Error())
}
kctx.Bind(config)
Expand Down
17 changes: 17 additions & 0 deletions common/projectconfig/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build integration

package projectconfig

import (
"testing"

in "github.com/TBD54566975/ftl/integration"
)

func TestCmdsCreateProjectTomlFilesIfNonexistent(t *testing.T) {
in.Run(t, "",
in.CopyModule("echo"),
in.Exec("ftl", "config", "set", "key", "--inline", "value", "--config", "ftl-project-nonexistent.toml"),
in.FileExists("ftl-project-nonexistent.toml"),
)
}
11 changes: 11 additions & 0 deletions common/projectconfig/projectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package projectconfig

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -75,6 +76,16 @@ func LoadWritableConfig(ctx context.Context, input []string) (Config, error) {
return Config{}, nil
}
target := configPaths[len(configPaths)-1]

logger := log.FromContext(ctx)
if _, err := os.Stat(target); errors.Is(err, os.ErrNotExist) {
logger.Warnf("Creating a new project config file at %q because the file does not already exist", target)
err = Save(target, Config{})
if err != nil {
return Config{}, err
}
}

log.FromContext(ctx).Tracef("Loading config from %s", target)
return loadFile(target)
}
Expand Down
21 changes: 21 additions & 0 deletions common/projectconfig/testdata/go/echo/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package echo

import (
"context"
"fmt"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
)

type EchoRequest struct {
Name ftl.Option[string] `json:"name"`
}

type EchoResponse struct {
Message string `json:"message"`
}

//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
2 changes: 2 additions & 0 deletions common/projectconfig/testdata/go/echo/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "echo"
language = "go"
45 changes: 45 additions & 0 deletions common/projectconfig/testdata/go/echo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module ftl/echo

go 1.22.2

toolchain go1.22.3

require github.com/TBD54566975/ftl v0.234.0

require (
connectrpc.com/connect v1.16.1 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.16.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)

replace github.com/TBD54566975/ftl => ../../../../..
136 changes: 136 additions & 0 deletions common/projectconfig/testdata/go/echo/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4c9753

Please sign in to comment.