Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create specified config files that do not exist #1618

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading