From 74f4ea30795bea32e3df795559d44ddde55c3cc9 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Sun, 17 May 2026 18:04:59 +0530 Subject: [PATCH] feat: updates conf to allow setting db name Signed-off-by: Amit Singh --- cmd/app/main.go | 2 +- pkg/conf/conf.go | 4 ++-- pkg/conf/conf_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 1fbb34b..059491f 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -54,7 +54,7 @@ func main() { conf.Cfg.PgHost, conf.AppUserName, conf.Cfg.AppUserPassword, - conf.DbName, + conf.Cfg.DbName, ) dbClient := pgsql.NewClient(context.Background(), dsn, conf.GormConfig) // TODO: wrap this and call it securely diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 01c3cc7..9e203fb 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -10,13 +10,13 @@ import ( const ( AppUserName = "sourcescore" - DbName = "sourcescore" ) type conf struct { AppUserPassword string `env:"APP_USER_PASSWORD" yaml:"APP_USER_PASSWORD" env-required:"true"` + DbName string `env:"DB_NAME" yaml:"DB_NAME" env-default:"sourcescore"` PgHost string `env:"PG_HOST" yaml:"PG_HOST" env-required:"true"` - Port string `env:"PORT" yaml:"PORT"` + Port string `env:"PORT" yaml:"PORT" env-default:"8080"` SuperUserPassword string `env:"SUPER_USER_PASSWORD" yaml:"SUPER_USER_PASSWORD" env-required:"true"` } diff --git a/pkg/conf/conf_test.go b/pkg/conf/conf_test.go index ba33d4d..1ce7874 100644 --- a/pkg/conf/conf_test.go +++ b/pkg/conf/conf_test.go @@ -13,6 +13,7 @@ const ( SamplePwd = "sample-pwd" SampleHost = "sample-host" SampleSUPwd = "super-pwd" + SampleDbName = "test-db" ) var _ = Describe("Conf Tests", func() { @@ -44,4 +45,32 @@ var _ = Describe("Conf Tests", func() { Expect(conf.Cfg.SuperUserPassword).To(BeEquivalentTo("user-pwd")) }) }) + + When("DbName field is read", func() { + It("should use default value when DB_NAME environment variable is not set", func() { + os.Unsetenv("DOTENV_PATH") + os.Unsetenv("DB_NAME") + os.Setenv("APP_USER_PASSWORD", SamplePwd) + os.Setenv("PG_HOST", SampleHost) + os.Setenv("PORT", SamplePort) + os.Setenv("SUPER_USER_PASSWORD", SampleSUPwd) + + conf.LoadConfig() + + Expect(conf.Cfg.DbName).To(BeEquivalentTo("sourcescore")) + }) + + It("should be overwritten when DB_NAME environment variable is set", func() { + os.Unsetenv("DOTENV_PATH") + os.Setenv("DB_NAME", SampleDbName) + os.Setenv("APP_USER_PASSWORD", SamplePwd) + os.Setenv("PG_HOST", SampleHost) + os.Setenv("PORT", SamplePort) + os.Setenv("SUPER_USER_PASSWORD", SampleSUPwd) + + conf.LoadConfig() + + Expect(conf.Cfg.DbName).To(BeEquivalentTo(SampleDbName)) + }) + }) })