From d2a6ba3ff629587646eeb7741ded30f78a52f446 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 19 Jul 2025 17:40:13 -0400 Subject: Replace envconfig with envelope --- internal/configloader/configloader.go | 33 ++++++++++++++++++------------ internal/configloader/configloader_test.go | 4 ++-- 2 files changed, 22 insertions(+), 15 deletions(-) (limited to 'internal/configloader') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index 0f724a6..842dcfb 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -13,10 +13,11 @@ import ( "fmt" "os" "path/filepath" + "reflect" "runtime" + "git.sr.ht/~eugenetriguba/envelope" "github.com/BurntSushi/toml" - "github.com/kelseyhightower/envconfig" ) type VersionStyle string @@ -41,26 +42,32 @@ var ( // This can come from the TOML file or environment variables, // with environment variables taking precedence. type Config struct { - Migrations MigrationsConfig `toml:"migrations"` + Migrations MigrationsConfig `toml:"migrations" env:"BOLT_MIGRATIONS"` // Information related to how to connect to the database // that is desired to run migrations against. - Connection ConnectionConfig `toml:"database"` + Connection ConnectionConfig `toml:"database" env:"BOLT_DB"` } type MigrationsConfig struct { - DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` - VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` + DirectoryPath string `toml:"directory_path" env:"DIR_PATH"` + VersionStyle VersionStyle `toml:"version_style" env:"VERSION_STYLE"` } type ConnectionConfig struct { - Host string `toml:"host" envconfig:"BOLT_DB_HOST"` - Port string `toml:"port" envconfig:"BOLT_DB_PORT"` - User string `toml:"user" envconfig:"BOLT_DB_USER"` - Password string `toml:"password" envconfig:"BOLT_DB_PASSWORD"` - DBName string `toml:"dbname" envconfig:"BOLT_DB_NAME"` - Driver string `toml:"driver" envconfig:"BOLT_DB_DRIVER"` - MigrationsTable string `toml:"migrations_table" envconfig:"BOLT_DB_MIGRATIONS_TABLE"` + Host string `toml:"host" env:"HOST"` + Port string `toml:"port" env:"PORT"` + User string `toml:"user" env:"USER"` + Password string `toml:"password" env:"PASSWORD"` + DBName string `toml:"dbname" env:"NAME"` + Driver string `toml:"driver" env:"DRIVER"` + MigrationsTable string `toml:"migrations_table" env:"MIGRATIONS_TABLE"` +} + +func init() { + envelope.RegisterTypeParser(reflect.TypeOf(VersionStyleSequential), envelope.TypeParserFunc(func(value string) (any, error) { + return VersionStyle(value), nil + })) } func NewConfig() (*Config, error) { @@ -85,7 +92,7 @@ func NewConfig() (*Config, error) { } } - err = envconfig.Process("", &cfg) + err = envelope.LoadFromEnv(&cfg) if err != nil { return nil, err } diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 352f243..f988afb 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -5,10 +5,10 @@ import ( "path/filepath" "testing" + "git.sr.ht/~eugenetriguba/checkmate/assert" + "git.sr.ht/~eugenetriguba/checkmate/check" "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate/assert" - "github.com/eugenetriguba/checkmate/check" ) func TestNewConfigDefaults(t *testing.T) { -- cgit v1.2.3