Skip to content

Commit 2de70d2

Browse files
authored
feat(config): Added config default path (#280)
1 parent f42bf67 commit 2de70d2

File tree

6 files changed

+61
-37
lines changed

6 files changed

+61
-37
lines changed

config/.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ linters:
4040
- importas
4141
- ineffassign
4242
- interfacebloat
43-
- logrlint
43+
- loggercheck
4444
- maintidx
4545
- makezero
4646
- misspell

config/README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
> Configuration module based on [Viper](https://github.com/spf13/viper).
1010
1111
<!-- TOC -->
12-
1312
* [Installation](#installation)
1413
* [Documentation](#documentation)
15-
* [Configuration files](#configuration-files)
16-
* [Configuration usage](#configuration-usage)
17-
* [Configuration access](#configuration-access)
18-
* [Configuration dynamic env overrides](#configuration-dynamic-env-overrides)
19-
* [Configuration env var placeholders](#configuration-env-var-placeholders)
20-
* [Configuration env var substitution](#configuration-env-var-substitution)
21-
14+
* [Configuration files](#configuration-files)
15+
* [Configuration usage](#configuration-usage)
16+
* [Configuration access](#configuration-access)
17+
* [Configuration dynamic env overrides](#configuration-dynamic-env-overrides)
18+
* [Configuration env var placeholders](#configuration-env-var-placeholders)
19+
* [Configuration env var substitution](#configuration-env-var-substitution)
2220
<!-- TOC -->
2321

2422
## Installation
@@ -33,7 +31,7 @@ go get github.com/ankorstore/yokai/config
3331

3432
By default, the module expects configuration files:
3533

36-
- to be present in `.` (root) or `./configs` directories of your project
34+
- to be present in `.` (root), `./config` or `./configs` directories of your project
3735
- to be named `config.{format}` (ex: `config.yaml`, `config.json`, etc.)
3836
- to offer env overrides files named `config.{env}.{format}` based on the env var `APP_ENV` (ex: `config.test.yaml` if
3937
env var `APP_ENV=test`)
@@ -86,8 +84,8 @@ var cfg, _ = config.NewDefaultConfigFactory().Create()
8684
8785
// equivalent to:
8886
var cfg, _ = config.NewDefaultConfigFactory().Create(
89-
config.WithFileName("config"), // config files base name
90-
config.WithFilePaths(".", "./configs"), // config files lookup paths
87+
config.WithFileName("config"), // config files base name
88+
config.WithFilePaths(".", "./config", "./configs"), // config files lookup paths
9189
)
9290
```
9391

config/go.mod

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

55
require (
6-
github.com/spf13/viper v1.18.2
6+
github.com/spf13/viper v1.19.0
77
github.com/stretchr/testify v1.9.0
88
)
99

@@ -13,7 +13,7 @@ require (
1313
github.com/hashicorp/hcl v1.0.0 // indirect
1414
github.com/magiconair/properties v1.8.7 // indirect
1515
github.com/mitchellh/mapstructure v1.5.0 // indirect
16-
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
16+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
1717
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1818
github.com/sagikazarmark/locafero v0.4.0 // indirect
1919
github.com/sagikazarmark/slog-shim v0.1.0 // indirect

config/go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
1414
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
1515
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
1616
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
17-
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
18-
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
17+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
18+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
1919
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2020
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
2121
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -32,11 +32,12 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
3232
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
3333
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3434
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
35-
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
36-
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
35+
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
36+
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
3737
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3838
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
3939
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
40+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
4041
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4142
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4243
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=

config/option.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ func DefaultConfigOptions() Options {
1717
FileName: "config",
1818
FilePaths: []string{
1919
".",
20+
"./config",
2021
"./configs",
2122
},
2223
}
2324

24-
// check if the OCI image has been build using ko and use KO_DATA_PATH as config root
25+
// KO embeddings, see https://ko.build/features/static-assets/
2526
if val, ok := os.LookupEnv("KO_DATA_PATH"); ok {
26-
opts.FilePaths = append(opts.FilePaths, val, path.Join(val, "configs"))
27+
opts.FilePaths = append(
28+
opts.FilePaths,
29+
val,
30+
path.Join(val, "config"),
31+
path.Join(val, "configs"),
32+
)
2733
}
2834

2935
return opts
@@ -42,6 +48,6 @@ func WithFileName(n string) ConfigOption {
4248
// WithFilePaths is used to specify the list of file paths to lookup config files to load.
4349
func WithFilePaths(p ...string) ConfigOption {
4450
return func(o *Options) {
45-
o.FilePaths = p
51+
o.FilePaths = append(o.FilePaths, p...)
4652
}
4753
}

config/option_test.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10+
func TestDefaultConfigOptions(t *testing.T) {
11+
opts := config.DefaultConfigOptions()
12+
13+
assert.Equal(t, "config", opts.FileName)
14+
assert.Equal(
15+
t,
16+
[]string{
17+
".",
18+
"./config",
19+
"./configs",
20+
},
21+
opts.FilePaths,
22+
)
23+
}
24+
25+
func TestDefaultConfigOptionsWithKO(t *testing.T) {
26+
t.Setenv("KO_DATA_PATH", "/var/run/ko")
27+
28+
opts := config.DefaultConfigOptions()
29+
30+
assert.Equal(t, "config", opts.FileName)
31+
assert.Equal(
32+
t,
33+
[]string{
34+
".",
35+
"./config",
36+
"./configs",
37+
"/var/run/ko",
38+
"/var/run/ko/config",
39+
"/var/run/ko/configs",
40+
},
41+
opts.FilePaths,
42+
)
43+
}
44+
1045
func TestWithFileName(t *testing.T) {
1146
option := config.WithFileName("test")
1247

@@ -24,19 +59,3 @@ func TestWithFilePaths(t *testing.T) {
2459

2560
assert.Equal(t, []string{"path1", "path2"}, opts.FilePaths)
2661
}
27-
28-
func TestDefaultConfigOptions(t *testing.T) {
29-
opts := config.DefaultConfigOptions()
30-
31-
assert.Equal(t, "config", opts.FileName)
32-
assert.Equal(t, []string{".", "./configs"}, opts.FilePaths)
33-
}
34-
35-
func TestDefaultConfigOptions_KoBuild(t *testing.T) {
36-
t.Setenv("KO_DATA_PATH", "/var/run/ko")
37-
38-
opts := config.DefaultConfigOptions()
39-
40-
assert.Equal(t, "config", opts.FileName)
41-
assert.Equal(t, []string{".", "./configs", "/var/run/ko", "/var/run/ko/configs"}, opts.FilePaths)
42-
}

0 commit comments

Comments
 (0)