Skip to content

Commit

Permalink
feat(config)L Added config default path
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Sep 25, 2024
1 parent f42bf67 commit bfca1f7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion config/.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
20 changes: 9 additions & 11 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
> Configuration module based on [Viper](https://github.com/spf13/viper).
<!-- TOC -->

* [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)
<!-- TOC -->

## Installation
Expand All @@ -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`)
Expand Down Expand Up @@ -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
)
```

Expand Down
4 changes: 2 additions & 2 deletions config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions config/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
12 changes: 9 additions & 3 deletions config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...)
}
}
51 changes: 35 additions & 16 deletions config/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
}

0 comments on commit bfca1f7

Please sign in to comment.