Skip to content

Commit

Permalink
feat: improve config setup (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop authored Sep 23, 2024
1 parent 9eead42 commit 3e23247
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin

# Setup go-make version to use desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].97
GOMAKE_DEP ?= github.com/tkrop/[email protected].98
INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The [`go-config`][go-config] framework supports to set up a [logrus][logrus]
`Logger`_out-of-the-box using the following two approaches:

```go
config := config.SetupLogger(logger)
logger := config.SetupLogger(log.New())

logger := config.Log.Setup(log.New())
```
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.4
0.0.5
10 changes: 4 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ type Config struct {
}

// SetupLogger is a convenience method to setup the logger.
func (c *Config) SetupLogger(logger *log.Logger) *Config {
c.Log.Setup(logger)

return c
func (c *Config) SetupLogger(logger *log.Logger) *log.Logger {
return c.Log.Setup(logger)
}

// Reader common config reader based on viper.
Expand All @@ -62,7 +60,7 @@ func GetEnvName(prefix string, name string) string {
// config values to initialize the map. The `default` tags are only used, if
// the config values are zero.
func New[C any](
prefix, name string, config *C,
prefix, name string,
) *Reader[C] {
r := &Reader[C]{
Viper: viper.New(),
Expand All @@ -75,7 +73,7 @@ func New[C any](
r.SetConfigName(GetEnvName(prefix, name))
r.SetConfigType("yaml")
r.AddConfigPath(".")
r.SetSubDefaults("", config, true)
r.SetSubDefaults("", new(C), true)

return r
}
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestConfig(t *testing.T) {
if param.setenv != nil {
param.setenv(t)
}
reader := config.New("TC", "test", &config.Config{}).
reader := config.New[config.Config]("TC", "test").
SetDefaults(param.setup)

// When
Expand All @@ -108,7 +108,7 @@ func TestSetupLogger(t *testing.T) {

// Given
logger := log.New()
config := config.New("TC", "test", &config.Config{}).
config := config.New[config.Config]("TC", "test").
SetDefaults(func(r *config.Reader[config.Config]) {
r.AddConfigPath("fixtures")
r.SetDefault("log.level", "trace")
Expand Down
4 changes: 2 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestSetup(t *testing.T) {
Run(func(t test.Test, param setupParams) {
// Given
logger := log.New()
config := config.New("TEST", "test", &config.Config{}).
config := config.New[config.Config]("TEST", "test").
SetSubDefaults("log", param.config, false).
GetConfig(t.Name())

Expand All @@ -102,7 +102,7 @@ func TestSetup(t *testing.T) {

func TestSetupNil(t *testing.T) {
// Given
config := config.New("TEST", "test", &config.Config{}).
config := config.New[config.Config]("TEST", "test").
GetConfig(t.Name())

// When
Expand Down

0 comments on commit 3e23247

Please sign in to comment.