aboutsummaryrefslogtreecommitdiffstats
path: root/internal/configloader
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-13 21:47:26 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-13 21:47:26 -0500
commit34702d3802c8eb08cf4440c0cbb939b1db45168d (patch)
treea9d601c583d431092f74d3af7a98f6b84379ab20 /internal/configloader
parentTweak doc text (diff)
downloadcli-34702d3802c8eb08cf4440c0cbb939b1db45168d.zip
Update configuration format and env names
Diffstat (limited to 'internal/configloader')
-rw-r--r--internal/configloader/configloader.go25
-rw-r--r--internal/configloader/configloader_test.go24
-rw-r--r--internal/configloader/configloader_unix_test.go2
-rw-r--r--internal/configloader/configloader_windows_test.go2
4 files changed, 27 insertions, 26 deletions
diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go
index d85548a..281ed1f 100644
--- a/internal/configloader/configloader.go
+++ b/internal/configloader/configloader.go
@@ -27,23 +27,24 @@ var errConfigFileNotFound = errors.New(
// This can come from the TOML file or environment variables,
// with environment variables taking precedence.
type Config struct {
- // Relative file path from the bolt configuration file.
- // This is optional and will default to "migrations" if
- // not specified.
- MigrationsDir string `toml:"migrations_dir" envconfig:"BOLT_MIGRATIONS_DIR"`
+ Migrations MigrationsConfig `toml:"bolt.migrations"`
// Information related to how to connect to the database
// that is desired to run migrations against.
- Connection ConnectionConfig `toml:"connection"`
+ Connection ConnectionConfig `toml:"bolt.db.connection"`
+}
+
+type MigrationsConfig struct {
+ DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"`
}
type ConnectionConfig struct {
- Host string `toml:"host" envconfig:"BOLT_CONNECTION_HOST"`
- Port int `toml:"port" envconfig:"BOLT_CONNECTION_PORT"`
- User string `toml:"user" envconfig:"BOLT_CONNECTION_USER"`
- Password string `toml:"password" envconfig:"BOLT_CONNECTION_PASSWORD"`
- DBName string `toml:"dbname" envconfig:"BOLT_CONNECTION_DBNAME"`
- Driver string `toml:"driver" envconfig:"BOLT_CONNECTION_DRIVER"`
+ Host string `toml:"host" envconfig:"BOLT_DB_CONN_HOST"`
+ Port int `toml:"port" envconfig:"BOLT_DB_CONN_PORT"`
+ User string `toml:"user" envconfig:"BOLT_DB_CONN_USER"`
+ Password string `toml:"password" envconfig:"BOLT_DB_CONN_PASSWORD"`
+ DBName string `toml:"dbname" envconfig:"BOLT_DB_CONN_DBNAME"`
+ Driver string `toml:"driver" envconfig:"BOLT_DB_CONN_DRIVER"`
}
func NewConfig() (*Config, error) {
@@ -52,7 +53,7 @@ func NewConfig() (*Config, error) {
return nil, err
}
- cfg := Config{MigrationsDir: "migrations"}
+ cfg := Config{Migrations: MigrationsConfig{DirectoryPath: "migrations"}}
if !errors.Is(err, errConfigFileNotFound) {
_, err = toml.DecodeFile(filePath, &cfg)
if err != nil {
diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go
index 88c7f94..a41c2be 100644
--- a/internal/configloader/configloader_test.go
+++ b/internal/configloader/configloader_test.go
@@ -10,18 +10,18 @@ import (
"gotest.tools/v3/assert"
)
-func TestNewConfigMigrationsDirDefault(t *testing.T) {
+func TestNewConfigMigrationsDirPathDefault(t *testing.T) {
bolttest.ChangeCwd(t, os.TempDir())
cfg, err := configloader.NewConfig()
assert.NilError(t, err)
- assert.Equal(t, cfg.MigrationsDir, "migrations")
+ assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations")
}
func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
expectedCfg := configloader.Config{
- MigrationsDir: "myfancymigrations",
+ Migrations: configloader.MigrationsConfig{DirectoryPath: "myfancymigrations"},
Connection: configloader.ConnectionConfig{
Host: "testhost",
Port: 1234,
@@ -40,7 +40,7 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
fileCfg := configloader.Config{
- MigrationsDir: "cfgmigrations",
+ Migrations: configloader.MigrationsConfig{DirectoryPath: "cfgmigrations"},
Connection: configloader.ConnectionConfig{
Host: "testhost",
Port: 1234,
@@ -53,7 +53,7 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")
envCfg := configloader.Config{
- MigrationsDir: "envmigrations",
+ Migrations: configloader.MigrationsConfig{DirectoryPath: "envmigrations"},
Connection: configloader.ConnectionConfig{
Host: "envtesthost",
Port: 4321,
@@ -63,13 +63,13 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
Driver: "postgres",
},
}
- t.Setenv("BOLT_MIGRATIONS_DIR", envCfg.MigrationsDir)
- t.Setenv("BOLT_CONNECTION_HOST", envCfg.Connection.Host)
- t.Setenv("BOLT_CONNECTION_PORT", fmt.Sprintf("%d", envCfg.Connection.Port))
- t.Setenv("BOLT_CONNECTION_USER", envCfg.Connection.User)
- t.Setenv("BOLT_CONNECTION_PASSWORD", envCfg.Connection.Password)
- t.Setenv("BOLT_CONNECTION_DBNAME", envCfg.Connection.DBName)
- t.Setenv("BOLT_CONNECTION_DRIVER", envCfg.Connection.Driver)
+ t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath)
+ t.Setenv("BOLT_DB_CONN_HOST", envCfg.Connection.Host)
+ t.Setenv("BOLT_DB_CONN_PORT", fmt.Sprintf("%d", envCfg.Connection.Port))
+ t.Setenv("BOLT_DB_CONN_USER", envCfg.Connection.User)
+ t.Setenv("BOLT_DB_CONN_PASSWORD", envCfg.Connection.Password)
+ t.Setenv("BOLT_DB_CONN_DBNAME", envCfg.Connection.DBName)
+ t.Setenv("BOLT_DB_CONN_DRIVER", envCfg.Connection.Driver)
cfg, err := configloader.NewConfig()
assert.NilError(t, err)
diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go
index 412ef47..dcd8ebd 100644
--- a/internal/configloader/configloader_unix_test.go
+++ b/internal/configloader/configloader_unix_test.go
@@ -13,7 +13,7 @@ import (
func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) {
expectedCfg := configloader.Config{
- MigrationsDir: "differentmigrationsdir",
+ Migrations: configloader.MigrationsConfig{DirectoryPath: "differentmigrationsdir"},
}
bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml")
diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go
index 5c8cee9..42f2978 100644
--- a/internal/configloader/configloader_windows_test.go
+++ b/internal/configloader/configloader_windows_test.go
@@ -14,7 +14,7 @@ import (
func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) {
expectedCfg := configloader.Config{
- MigrationsDir: "differentmigrationsdir",
+ Migrations: configloader.MigrationsConfig{DirectoryPath: "differentmigrationsdir"},
}
configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml")
bolttest.CreateConfigFile(t, &expectedCfg, configPath)