Skip to content

Commit

Permalink
feat(fxconfig): Added possibility to provide additional config files …
Browse files Browse the repository at this point in the history
…lookup paths (#282)
  • Loading branch information
ekkinox authored Sep 26, 2024
1 parent 4f13ecd commit 32699ae
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fxconfig/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ linters:
- importas
- ineffassign
- interfacebloat
- logrlint
- loggercheck
- maintidx
- makezero
- misspell
Expand Down
2 changes: 1 addition & 1 deletion fxconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {

The module expects configuration files to be present:
- in `.` (project root)
- or in the`./configs` directory
- or in the`./config` or `./configs` directories
- or any directory referenced in the `APP_CONFIG_PATH` env var

Check the [configuration files documentation](https://github.com/ankorstore/yokai/tree/main/config#configuration-files) for more details.
Expand Down
10 changes: 5 additions & 5 deletions fxconfig/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/ankorstore/yokai/fxconfig
go 1.20

require (
github.com/ankorstore/yokai/config v1.4.0
github.com/ankorstore/yokai/config v1.5.0
github.com/stretchr/testify v1.9.0
go.uber.org/fx v1.21.0
go.uber.org/fx v1.22.2
)

require (
Expand All @@ -14,17 +14,17 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
Expand Down
21 changes: 11 additions & 10 deletions fxconfig/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/ankorstore/yokai/config v1.4.0 h1:O3ZuTGud388Gq55bQwrfs/vdjYSZZvj0VUL6yZp4rcg=
github.com/ankorstore/yokai/config v1.4.0/go.mod h1:OV2QiL2dyNLCxhcGO+GcSa8Wm20+00H03VBHm9SPVuE=
github.com/ankorstore/yokai/config v1.5.0 h1:vL/l0dcnq34FtxE+Up1NvzgcRB0G/vI4Yo/H5PccfN0=
github.com/ankorstore/yokai/config v1.5.0/go.mod h1:C8ggYvcrG+J0Ra2vTtcDCANa8HMf3FdrC0Ek8o3tTEw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand All @@ -16,8 +16,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -34,22 +34,23 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/fx v1.21.0 h1:qqD6k7PyFHONffW5speYx403ywanuASqU4Rqdpc22XY=
go.uber.org/fx v1.21.0/go.mod h1:HT2M7d7RHo+ebKGh9NRcrsrHHfpZ60nW3QRubMRfv48=
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/fx v1.22.2 h1:iPW+OPxv0G8w75OemJ1RAnTUrF55zOJlXlo1TbJ0Buw=
go.uber.org/fx v1.22.2/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down
11 changes: 5 additions & 6 deletions fxconfig/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ var FxConfigModule = fx.Module(
// FxConfigParam allows injection of the required dependencies in [NewFxConfig].
type FxConfigParam struct {
fx.In
Factory config.ConfigFactory
Factory config.ConfigFactory
ConfigPaths []string `group:"config-paths"`
}

// NewFxConfig returns a [config.Config].
func NewFxConfig(p FxConfigParam) (*config.Config, error) {
configFilePaths := append([]string{os.Getenv("APP_CONFIG_PATH")}, p.ConfigPaths...)

return p.Factory.Create(
config.WithFileName("config"),
config.WithFilePaths(
".",
"./configs",
os.Getenv("APP_CONFIG_PATH"),
),
config.WithFilePaths(configFilePaths...),
)
}
34 changes: 34 additions & 0 deletions fxconfig/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ func TestModuleWithCustomEnv(t *testing.T) {
assert.Equal(t, "foo-bar-baz", cfg.GetString("config.substitution"))
}

func TestModuleWithOtherConfigPath(t *testing.T) {
var cfg *config.Config

fxtest.New(
t,
fx.NopLogger,
fxconfig.FxConfigModule,
fxconfig.AsConfigPath("testdata/other"),
fx.Populate(&cfg),
).RequireStart().RequireStop()

assert.Equal(t, "other-app", cfg.AppName())
assert.Equal(t, "dev", cfg.AppEnv())
assert.Equal(t, "bar", cfg.GetString("config.foo"))
}

func TestModuleWithTestEnvAndOtherConfigPath(t *testing.T) {
t.Setenv("APP_ENV", "test")

var cfg *config.Config

fxtest.New(
t,
fx.NopLogger,
fxconfig.FxConfigModule,
fxconfig.AsConfigPath("testdata/other"),
fx.Populate(&cfg),
).RequireStart().RequireStop()

assert.Equal(t, "other-app", cfg.AppName())
assert.Equal(t, "test", cfg.AppEnv())
assert.Equal(t, "test", cfg.GetString("config.foo"))
}

func TestModuleDecoration(t *testing.T) {
var cfg *config.Config

Expand Down
15 changes: 15 additions & 0 deletions fxconfig/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fxconfig

import (
"go.uber.org/fx"
)

// AsConfigPath registers an additional config files lookup path.
func AsConfigPath(path string) fx.Option {
return fx.Supply(
fx.Annotate(
path,
fx.ResultTags(`group:"config-paths"`),
),
)
}
17 changes: 17 additions & 0 deletions fxconfig/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fxconfig_test

import (
"fmt"
"testing"

"github.com/ankorstore/yokai/fxconfig"
"github.com/stretchr/testify/assert"
)

func TestAsConfigPath(t *testing.T) {
t.Parallel()

result := fxconfig.AsConfigPath("foo")

assert.Equal(t, "fx.supplyOption", fmt.Sprintf("%T", result))
}
4 changes: 4 additions & 0 deletions fxconfig/testdata/other/config.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
app:
env: test
config:
foo: test
5 changes: 5 additions & 0 deletions fxconfig/testdata/other/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app:
name: other-app
env: dev
config:
foo: bar

0 comments on commit 32699ae

Please sign in to comment.