Skip to content

Commit c4c7c98

Browse files
committed
feat(fxconfig): Added possibility to provide additional config files lookup paths
1 parent 4f13ecd commit c4c7c98

File tree

9 files changed

+97
-22
lines changed

9 files changed

+97
-22
lines changed

fxconfig/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454

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

6060
Check the [configuration files documentation](https://github.com/ankorstore/yokai/tree/main/config#configuration-files) for more details.

fxconfig/go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/ankorstore/yokai/fxconfig
33
go 1.20
44

55
require (
6-
github.com/ankorstore/yokai/config v1.4.0
6+
github.com/ankorstore/yokai/config v1.5.0
77
github.com/stretchr/testify v1.9.0
8-
go.uber.org/fx v1.21.0
8+
go.uber.org/fx v1.22.2
99
)
1010

1111
require (
@@ -14,17 +14,17 @@ require (
1414
github.com/hashicorp/hcl v1.0.0 // indirect
1515
github.com/magiconair/properties v1.8.7 // indirect
1616
github.com/mitchellh/mapstructure v1.5.0 // indirect
17-
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
17+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
1818
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1919
github.com/sagikazarmark/locafero v0.4.0 // indirect
2020
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
2121
github.com/sourcegraph/conc v0.3.0 // indirect
2222
github.com/spf13/afero v1.11.0 // indirect
2323
github.com/spf13/cast v1.6.0 // indirect
2424
github.com/spf13/pflag v1.0.5 // indirect
25-
github.com/spf13/viper v1.18.2 // indirect
25+
github.com/spf13/viper v1.19.0 // indirect
2626
github.com/subosito/gotenv v1.6.0 // indirect
27-
go.uber.org/dig v1.17.1 // indirect
27+
go.uber.org/dig v1.18.0 // indirect
2828
go.uber.org/multierr v1.11.0 // indirect
2929
go.uber.org/zap v1.27.0 // indirect
3030
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect

fxconfig/go.sum

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/ankorstore/yokai/config v1.4.0 h1:O3ZuTGud388Gq55bQwrfs/vdjYSZZvj0VUL6yZp4rcg=
2-
github.com/ankorstore/yokai/config v1.4.0/go.mod h1:OV2QiL2dyNLCxhcGO+GcSa8Wm20+00H03VBHm9SPVuE=
1+
github.com/ankorstore/yokai/config v1.5.0 h1:vL/l0dcnq34FtxE+Up1NvzgcRB0G/vI4Yo/H5PccfN0=
2+
github.com/ankorstore/yokai/config v1.5.0/go.mod h1:C8ggYvcrG+J0Ra2vTtcDCANa8HMf3FdrC0Ek8o3tTEw=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -16,8 +16,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
1616
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
1717
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
1818
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
19-
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
20-
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
19+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
20+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
2121
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2222
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
2323
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -34,22 +34,23 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
3434
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
3535
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3636
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
37-
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
38-
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
37+
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
38+
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
3939
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4040
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
4141
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
42+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
4243
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4344
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4445
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
4546
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4647
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4748
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
4849
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
49-
go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
50-
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
51-
go.uber.org/fx v1.21.0 h1:qqD6k7PyFHONffW5speYx403ywanuASqU4Rqdpc22XY=
52-
go.uber.org/fx v1.21.0/go.mod h1:HT2M7d7RHo+ebKGh9NRcrsrHHfpZ60nW3QRubMRfv48=
50+
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
51+
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
52+
go.uber.org/fx v1.22.2 h1:iPW+OPxv0G8w75OemJ1RAnTUrF55zOJlXlo1TbJ0Buw=
53+
go.uber.org/fx v1.22.2/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
5354
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5455
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
5556
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=

fxconfig/module.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ var FxConfigModule = fx.Module(
2424
// FxConfigParam allows injection of the required dependencies in [NewFxConfig].
2525
type FxConfigParam struct {
2626
fx.In
27-
Factory config.ConfigFactory
27+
Factory config.ConfigFactory
28+
ConfigPaths []string `group:"config-paths"`
2829
}
2930

3031
// NewFxConfig returns a [config.Config].
3132
func NewFxConfig(p FxConfigParam) (*config.Config, error) {
33+
configFilePaths := append([]string{os.Getenv("APP_CONFIG_PATH")}, p.ConfigPaths...)
34+
3235
return p.Factory.Create(
3336
config.WithFileName("config"),
34-
config.WithFilePaths(
35-
".",
36-
"./configs",
37-
os.Getenv("APP_CONFIG_PATH"),
38-
),
37+
config.WithFilePaths(configFilePaths...),
3938
)
4039
}

fxconfig/module_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ func TestModuleWithCustomEnv(t *testing.T) {
9696
assert.Equal(t, "foo-bar-baz", cfg.GetString("config.substitution"))
9797
}
9898

99+
func TestModuleWithOtherConfigPath(t *testing.T) {
100+
var cfg *config.Config
101+
102+
fxtest.New(
103+
t,
104+
fx.NopLogger,
105+
fxconfig.FxConfigModule,
106+
fxconfig.AsConfigPath("testdata/other"),
107+
fx.Populate(&cfg),
108+
).RequireStart().RequireStop()
109+
110+
assert.Equal(t, "other-app", cfg.AppName())
111+
assert.Equal(t, "dev", cfg.AppEnv())
112+
assert.Equal(t, "bar", cfg.GetString("config.foo"))
113+
}
114+
115+
func TestModuleWithTestEnvAndOtherConfigPath(t *testing.T) {
116+
t.Setenv("APP_ENV", "test")
117+
118+
var cfg *config.Config
119+
120+
fxtest.New(
121+
t,
122+
fx.NopLogger,
123+
fxconfig.FxConfigModule,
124+
fxconfig.AsConfigPath("testdata/other"),
125+
fx.Populate(&cfg),
126+
).RequireStart().RequireStop()
127+
128+
assert.Equal(t, "other-app", cfg.AppName())
129+
assert.Equal(t, "test", cfg.AppEnv())
130+
assert.Equal(t, "test", cfg.GetString("config.foo"))
131+
}
132+
99133
func TestModuleDecoration(t *testing.T) {
100134
var cfg *config.Config
101135

fxconfig/register.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fxconfig
2+
3+
import (
4+
"go.uber.org/fx"
5+
)
6+
7+
// AsConfigPath registers an additional config files lookup path.
8+
func AsConfigPath(path string) fx.Option {
9+
return fx.Supply(
10+
fx.Annotate(
11+
path,
12+
fx.ResultTags(`group:"config-paths"`),
13+
),
14+
)
15+
}

fxconfig/register_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fxconfig_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/ankorstore/yokai/fxconfig"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestAsConfigPath(t *testing.T) {
12+
t.Parallel()
13+
14+
result := fxconfig.AsConfigPath("foo")
15+
16+
assert.Equal(t, "fx.supplyOption", fmt.Sprintf("%T", result))
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
app:
2+
env: test
3+
config:
4+
foo: test

fxconfig/testdata/other/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
app:
2+
name: other-app
3+
env: dev
4+
config:
5+
foo: bar

0 commit comments

Comments
 (0)