From bfca1f706a730910a04f92d663748f57b9d959be Mon Sep 17 00:00:00 2001 From: Jonathan Vuillemin Date: Wed, 25 Sep 2024 12:30:02 +0200 Subject: [PATCH] feat(config)L Added config default path --- config/.golangci.yml | 2 +- config/README.md | 20 ++++++++--------- config/go.mod | 4 ++-- config/go.sum | 9 ++++---- config/option.go | 12 +++++++--- config/option_test.go | 51 +++++++++++++++++++++++++++++-------------- 6 files changed, 61 insertions(+), 37 deletions(-) diff --git a/config/.golangci.yml b/config/.golangci.yml index edf0e9ec..c5443fc8 100644 --- a/config/.golangci.yml +++ b/config/.golangci.yml @@ -40,7 +40,7 @@ linters: - importas - ineffassign - interfacebloat - - logrlint + - loggercheck - maintidx - makezero - misspell diff --git a/config/README.md b/config/README.md index c5de4e96..a84a8d86 100644 --- a/config/README.md +++ b/config/README.md @@ -9,16 +9,14 @@ > Configuration module based on [Viper](https://github.com/spf13/viper). - * [Installation](#installation) * [Documentation](#documentation) - * [Configuration files](#configuration-files) - * [Configuration usage](#configuration-usage) - * [Configuration access](#configuration-access) - * [Configuration dynamic env overrides](#configuration-dynamic-env-overrides) - * [Configuration env var placeholders](#configuration-env-var-placeholders) - * [Configuration env var substitution](#configuration-env-var-substitution) - + * [Configuration files](#configuration-files) + * [Configuration usage](#configuration-usage) + * [Configuration access](#configuration-access) + * [Configuration dynamic env overrides](#configuration-dynamic-env-overrides) + * [Configuration env var placeholders](#configuration-env-var-placeholders) + * [Configuration env var substitution](#configuration-env-var-substitution) ## Installation @@ -33,7 +31,7 @@ go get github.com/ankorstore/yokai/config By default, the module expects configuration files: -- to be present in `.` (root) or `./configs` directories of your project +- to be present in `.` (root), `./config` or `./configs` directories of your project - to be named `config.{format}` (ex: `config.yaml`, `config.json`, etc.) - to offer env overrides files named `config.{env}.{format}` based on the env var `APP_ENV` (ex: `config.test.yaml` if env var `APP_ENV=test`) @@ -86,8 +84,8 @@ var cfg, _ = config.NewDefaultConfigFactory().Create() // equivalent to: var cfg, _ = config.NewDefaultConfigFactory().Create( - config.WithFileName("config"), // config files base name - config.WithFilePaths(".", "./configs"), // config files lookup paths + config.WithFileName("config"), // config files base name + config.WithFilePaths(".", "./config", "./configs"), // config files lookup paths ) ``` diff --git a/config/go.mod b/config/go.mod index 08bfbb00..344df0c1 100644 --- a/config/go.mod +++ b/config/go.mod @@ -3,7 +3,7 @@ module github.com/ankorstore/yokai/config go 1.20 require ( - github.com/spf13/viper v1.18.2 + github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 ) @@ -13,7 +13,7 @@ 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 diff --git a/config/go.sum b/config/go.sum index 22445fd1..5f480abf 100644 --- a/config/go.sum +++ b/config/go.sum @@ -14,8 +14,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= @@ -32,11 +32,12 @@ 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= diff --git a/config/option.go b/config/option.go index c190dad1..020c3890 100644 --- a/config/option.go +++ b/config/option.go @@ -17,13 +17,19 @@ func DefaultConfigOptions() Options { FileName: "config", FilePaths: []string{ ".", + "./config", "./configs", }, } - // check if the OCI image has been build using ko and use KO_DATA_PATH as config root + // KO embeddings, see https://ko.build/features/static-assets/ if val, ok := os.LookupEnv("KO_DATA_PATH"); ok { - opts.FilePaths = append(opts.FilePaths, val, path.Join(val, "configs")) + opts.FilePaths = append( + opts.FilePaths, + val, + path.Join(val, "config"), + path.Join(val, "configs"), + ) } return opts @@ -42,6 +48,6 @@ func WithFileName(n string) ConfigOption { // WithFilePaths is used to specify the list of file paths to lookup config files to load. func WithFilePaths(p ...string) ConfigOption { return func(o *Options) { - o.FilePaths = p + o.FilePaths = append(o.FilePaths, p...) } } diff --git a/config/option_test.go b/config/option_test.go index 56e97064..8d8b84bb 100644 --- a/config/option_test.go +++ b/config/option_test.go @@ -7,6 +7,41 @@ import ( "github.com/stretchr/testify/assert" ) +func TestDefaultConfigOptions(t *testing.T) { + opts := config.DefaultConfigOptions() + + assert.Equal(t, "config", opts.FileName) + assert.Equal( + t, + []string{ + ".", + "./config", + "./configs", + }, + opts.FilePaths, + ) +} + +func TestDefaultConfigOptionsWithKO(t *testing.T) { + t.Setenv("KO_DATA_PATH", "/var/run/ko") + + opts := config.DefaultConfigOptions() + + assert.Equal(t, "config", opts.FileName) + assert.Equal( + t, + []string{ + ".", + "./config", + "./configs", + "/var/run/ko", + "/var/run/ko/config", + "/var/run/ko/configs", + }, + opts.FilePaths, + ) +} + func TestWithFileName(t *testing.T) { option := config.WithFileName("test") @@ -24,19 +59,3 @@ func TestWithFilePaths(t *testing.T) { assert.Equal(t, []string{"path1", "path2"}, opts.FilePaths) } - -func TestDefaultConfigOptions(t *testing.T) { - opts := config.DefaultConfigOptions() - - assert.Equal(t, "config", opts.FileName) - assert.Equal(t, []string{".", "./configs"}, opts.FilePaths) -} - -func TestDefaultConfigOptions_KoBuild(t *testing.T) { - t.Setenv("KO_DATA_PATH", "/var/run/ko") - - opts := config.DefaultConfigOptions() - - assert.Equal(t, "config", opts.FileName) - assert.Equal(t, []string{".", "./configs", "/var/run/ko", "/var/run/ko/configs"}, opts.FilePaths) -}