Skip to content

Commit

Permalink
fix: end the ftl-project.toml search at git root, require a git repo …
Browse files Browse the repository at this point in the history
…on init
  • Loading branch information
safeer committed Jun 13, 2024
1 parent a7f2484 commit 250dfe0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
6 changes: 5 additions & 1 deletion cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error {
return fmt.Errorf("module name %q is invalid", i.Name)
}

if _, ok := internal.GitRoot(i.Dir).Get(); !ok {
return fmt.Errorf("directory %s is not a git repository", i.Dir)
}

logger := log.FromContext(ctx)
logger.Debugf("Initializing FTL Go module %s in %s", i.Name, i.Dir)
if err := scaffold(parent.Hermit, goruntime.Files(), i.Dir, i, scaffolder.Exclude("^go.mod$")); err != nil {
Expand All @@ -71,7 +75,7 @@ func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error {
if err := updateGitIgnore(i.Dir); err != nil {
return err
}
if err := projectconfig.MaybeCreateDefault(ctx, i.Dir); err != nil {
if err := projectconfig.MaybeCreateDefault(ctx); err != nil {
return err
}
logger.Debugf("Running go mod tidy")
Expand Down
25 changes: 10 additions & 15 deletions common/projectconfig/projectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl"
"github.com/TBD54566975/ftl/internal"
"github.com/TBD54566975/ftl/internal/log"
)

Expand Down Expand Up @@ -79,8 +80,12 @@ func DefaultConfigPath() optional.Option[string] {
if err != nil {
return optional.None[string]()
}
// Find the first ftl-project.toml file in the parent directories.
for {
// Find the first ftl-project.toml file in the parent directories, up until the gitroot.
root, ok := internal.GitRoot(dir).Get()
if !ok {
root = "/"
}
for dir != root && dir != "." {
path := filepath.Join(dir, "ftl-project.toml")
_, err := os.Stat(path)
if err == nil {
Expand All @@ -90,16 +95,13 @@ func DefaultConfigPath() optional.Option[string] {
return optional.None[string]()
}
dir = filepath.Dir(dir)
if dir == "/" || dir == "." {
break
}
}
return optional.Some(filepath.Join(dir, "ftl-project.toml"))
}

// MaybeCreateDefault creates the ftl-project.toml file in the given dir if it
// does not already exist in any parent directory.
func MaybeCreateDefault(ctx context.Context, dir string) error {
// MaybeCreateDefault creates the ftl-project.toml file in the Git root if it
// does not already exist.
func MaybeCreateDefault(ctx context.Context) error {
logger := log.FromContext(ctx)
path, ok := DefaultConfigPath().Get()
if !ok {
Expand All @@ -113,13 +115,6 @@ func MaybeCreateDefault(ctx context.Context, dir string) error {
if !errors.Is(err, os.ErrNotExist) {
return err
}

// No project file exists, create one in the given dir.
absDir, err := filepath.Abs(dir)
if err != nil {
return err
}
path = filepath.Join(absDir, "ftl-project.toml")
logger.Debugf("Creating a new project config file at %q", path)
return Save(Config{Path: path})
}
Expand Down

0 comments on commit 250dfe0

Please sign in to comment.