From 0cba1f6bde4892e34be55eb5f6de231c4d030cd3 Mon Sep 17 00:00:00 2001 From: Jonathan Vuillemin Date: Fri, 26 Jul 2024 10:03:51 +0200 Subject: [PATCH] feat(config): Added app description config key --- config/README.md | 21 +++++++++------- config/config.go | 5 ++++ config/config_test.go | 25 +++++++++++++++++++ .../testdata/config/valid/config.custom.yaml | 1 + config/testdata/config/valid/config.test.yaml | 1 + config/testdata/config/valid/config.yaml | 1 + 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/config/README.md b/config/README.md index d6aa8112..c5de4e96 100644 --- a/config/README.md +++ b/config/README.md @@ -51,6 +51,7 @@ For the following examples, we will be considering those configuration files: # ./configs/config.yaml app: name: app + description: app description env: dev version: 0.1.0 debug: false @@ -109,10 +110,11 @@ func main() { cfg, _ := config.NewDefaultConfigFactory().Create() // helpers - fmt.Printf("name: %s", cfg.AppName()) // name: app - fmt.Printf("env: %s", cfg.AppEnv()) // env: dev - fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0 - fmt.Printf("debug: %v", cfg.AppDebug()) // debug: false + fmt.Printf("name: %s", cfg.AppName()) // name: app + fmt.Printf("description: %s", cfg.AppDescription()) // description: app description + fmt.Printf("env: %s", cfg.AppEnv()) // env: dev + fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0 + fmt.Printf("debug: %v", cfg.AppDebug()) // debug: false // others fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: default @@ -146,11 +148,12 @@ func main() { cfg, _ := config.NewDefaultConfigFactory().Create() // helpers - fmt.Printf("var: %s", cfg.GetEnvVar("APP_ENV")) // var: test - fmt.Printf("name: %s", cfg.AppName()) // name: app - fmt.Printf("env: %s", cfg.AppEnv()) // env: test - fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0 - fmt.Printf("debug: %v", cfg.AppDebug()) // debug: true + fmt.Printf("var: %s", cfg.GetEnvVar("APP_ENV")) // var: test + fmt.Printf("name: %s", cfg.AppName()) // name: app + fmt.Printf("description: %s", cfg.AppDescription()) // description: app description + fmt.Printf("env: %s", cfg.AppEnv()) // env: test + fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0 + fmt.Printf("debug: %v", cfg.AppDebug()) // debug: true // others fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: test diff --git a/config/config.go b/config/config.go index 97bbc7a5..22bec450 100644 --- a/config/config.go +++ b/config/config.go @@ -32,6 +32,11 @@ func (c *Config) AppName() string { return c.GetString("app.name") } +// AppDescription returns the configured application description (from config field app.description or env var APP_DESCRIPTION). +func (c *Config) AppDescription() string { + return c.GetString("app.description") +} + // AppEnv returns the configured application environment (from config field app.env or env var APP_ENV). func (c *Config) AppEnv() string { return c.GetString("app.env") diff --git a/config/config_test.go b/config/config_test.go index ac9f8d05..77134494 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -41,6 +41,31 @@ func TestAppNameOverrideFromCustomEnvConfig(t *testing.T) { assert.Equal(t, "custom-app", cfg.AppName()) } +func TestAppDescriptionFromDefaultConfig(t *testing.T) { + cfg, err := createTestConfig() + + assert.Nil(t, err) + assert.Equal(t, "default-description", cfg.AppDescription()) +} + +func TestAppDescriptionOverrideFromTestEnvConfig(t *testing.T) { + t.Setenv("APP_ENV", "test") + + cfg, err := createTestConfig() + + assert.NoError(t, err) + assert.Equal(t, "test-description", cfg.AppDescription()) +} + +func TestAppDescriptionOverrideFromCustomEnvConfig(t *testing.T) { + t.Setenv("APP_ENV", "custom") + + cfg, err := createTestConfig() + + assert.NoError(t, err) + assert.Equal(t, "custom-description", cfg.AppDescription()) +} + func TestAppNameOverrideFromInvalidEnvConfig(t *testing.T) { t.Setenv("APP_ENV", "invalid") diff --git a/config/testdata/config/valid/config.custom.yaml b/config/testdata/config/valid/config.custom.yaml index 6a123fc9..95316241 100644 --- a/config/testdata/config/valid/config.custom.yaml +++ b/config/testdata/config/valid/config.custom.yaml @@ -1,5 +1,6 @@ app: name: custom-app + description: custom-description env: custom debug: true config: diff --git a/config/testdata/config/valid/config.test.yaml b/config/testdata/config/valid/config.test.yaml index c6550c9f..d156766d 100644 --- a/config/testdata/config/valid/config.test.yaml +++ b/config/testdata/config/valid/config.test.yaml @@ -1,5 +1,6 @@ app: name: test-app + description: test-description env: test debug: true config: diff --git a/config/testdata/config/valid/config.yaml b/config/testdata/config/valid/config.yaml index e52467a7..9d65ce3c 100644 --- a/config/testdata/config/valid/config.yaml +++ b/config/testdata/config/valid/config.yaml @@ -1,5 +1,6 @@ app: name: default-app + description: default-description env: dev version: 0.1.0 debug: false