Skip to content

Commit

Permalink
feat(config): Added app description config key
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Jul 26, 2024
1 parent c61f160 commit 0cba1f6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
21 changes: 12 additions & 9 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
1 change: 1 addition & 0 deletions config/testdata/config/valid/config.custom.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
app:
name: custom-app
description: custom-description
env: custom
debug: true
config:
Expand Down
1 change: 1 addition & 0 deletions config/testdata/config/valid/config.test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
app:
name: test-app
description: test-description
env: test
debug: true
config:
Expand Down
1 change: 1 addition & 0 deletions config/testdata/config/valid/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
app:
name: default-app
description: default-description
env: dev
version: 0.1.0
debug: false
Expand Down

0 comments on commit 0cba1f6

Please sign in to comment.