-
Notifications
You must be signed in to change notification settings - Fork 11
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: If not already existant, create ftl-project.toml
and/or module config or secret
#1273
Changes from 7 commits
1528684
e63b5c9
dbad3fc
9dd3bc8
2de255a
2a1266c
c5a7dc7
b83926d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package configuration | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
"github.com/alecthomas/types/optional" | ||
|
||
"github.com/TBD54566975/ftl/common/projectconfig" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
) | ||
|
||
func TestSet(t *testing.T) { | ||
defaultPath := projectconfig.GetDefaultConfigPath() | ||
origConfigBytes, err := os.ReadFile(defaultPath) | ||
assert.NoError(t, err) | ||
|
||
config := filepath.Join(t.TempDir(), "ftl-project.toml") | ||
existing, err := os.ReadFile("testdata/ftl-project.toml") | ||
assert.NoError(t, err) | ||
err = os.WriteFile(config, existing, 0600) | ||
assert.NoError(t, err) | ||
|
||
t.Run("ExistingModule", func(t *testing.T) { | ||
setAndAssert(t, "echo", []string{config}) | ||
}) | ||
t.Run("NewModule", func(t *testing.T) { | ||
setAndAssert(t, "echooo", []string{config}) | ||
}) | ||
t.Run("MissingTOMLFile", func(t *testing.T) { | ||
err := os.Remove(config) | ||
assert.NoError(t, err) | ||
setAndAssert(t, "echooooo", []string{}) | ||
err = os.WriteFile(defaultPath, origConfigBytes, 0600) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func setAndAssert(t *testing.T, module string, config []string) { | ||
ctx := log.ContextWithNewDefaultLogger(context.Background()) | ||
|
||
cf, err := New(ctx, | ||
ProjectConfigResolver[Configuration]{Config: config}, | ||
[]Provider[Configuration]{ | ||
EnvarProvider[Configuration]{}, | ||
InlineProvider[Configuration]{Inline: true}, // Writer | ||
}) | ||
assert.NoError(t, err) | ||
|
||
var got *url.URL | ||
want := URL("inline://asdfasdf") | ||
cf.Set(ctx, Ref{Module: optional.Some[string](module), Name: "default"}, want) | ||
err = cf.Get(ctx, Ref{Module: optional.Some[string](module), Name: "default"}, &got) | ||
assert.NoError(t, err) | ||
assert.Equal(t, want, got) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package projectconfig | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
@@ -42,14 +43,18 @@ func ConfigPaths(input []string) []string { | |
if len(input) > 0 { | ||
return input | ||
} | ||
path := filepath.Join(internal.GitRoot(""), "ftl-project.toml") | ||
path := GetDefaultConfigPath() | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return []string{path} | ||
} | ||
return []string{} | ||
} | ||
|
||
func GetDefaultConfigPath() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. |
||
return filepath.Join(internal.GitRoot(""), "ftl-project.toml") | ||
} | ||
|
||
func LoadConfig(ctx context.Context, input []string) (Config, error) { | ||
logger := log.FromContext(ctx) | ||
configPaths := ConfigPaths(input) | ||
|
@@ -92,6 +97,21 @@ func loadFile(path string) (Config, error) { | |
return config, nil | ||
} | ||
|
||
func CreateAndSave(config Config) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what this adds over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh thanks for catching that - I only made this to call |
||
path := GetDefaultConfigPath() | ||
_, err := os.Stat(path) | ||
// Only create a new file if there isn't one already defined at this location | ||
if err != nil && errors.Is(err, os.ErrNotExist) { | ||
if err = os.WriteFile(path, []byte{}, 0600); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per above, I think this function is redundant, but just FTR it's always better to write to a temp file and rename it, rather than overwrite a file directly, because if the write fails halfway through it will result in a corrupted config. With the rename temp file approach, the rename is atomic, so the file is either old or new, never "half written". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's very good to know. Thanks! |
||
return fmt.Errorf("failed to create file at path %q due to error: %w", path, err) | ||
} | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to create file at path %q due to error: %w", path, err) | ||
} | ||
return Save(path, config) | ||
} | ||
|
||
// Save project config atomically to a file. | ||
func Save(path string, config Config) error { | ||
w, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually unnecessary -
pc.ConfigPaths()
will returnpc.GetDefaultConfigPath()
iflen(p.Config) == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that! #1276