Skip to content

Commit

Permalink
Fix passing stage folder if it is in more than 1 folder after default…
Browse files Browse the repository at this point in the history
…s one. Update yaml dependence version.
  • Loading branch information
smgladkovskiy committed May 27, 2020
1 parent fe064d6 commit c4967dc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config_examples/configuration/prod/log.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
prod:
log:
level: "error"
format: "text"
2 changes: 2 additions & 0 deletions config_examples/configuration/prod/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
prod:
debug: true
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go 1.14
require (
github.com/imdario/mergo v0.3.9
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v2 v2.3.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5 changes: 4 additions & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
return nil
}

if f.IsDir() && (stageDir == "" || stageDir == defaultStage) {
if stageDir == "" || f.Name() == defaultStage || f.Name() == stage {
stageDir = f.Name()
}

if f.IsDir() {
return nil
}

Expand Down
35 changes: 35 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,41 @@ func TestReadConfigs(t *testing.T) {

assert.EqualValues(t, refConfig, config)
})
t.Run("Success parsing common dirs and files with different stages", func(t *testing.T) {
os.Setenv("STAGE", "prod")
configBytes, err := ReadConfigs("./config_examples/configuration")
if !assert.NoError(t, err) {
t.FailNow()
}

type cfg struct {
Debug bool `yaml:"debug"`
Log struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
} `yaml:"log"`
Host string `yaml:"host"`
Port string `yaml:"port"`
}

config := &cfg{}
err = yaml.Unmarshal(configBytes, &config)
if !assert.NoError(t, err) {
t.FailNow()
}

refConfig := &cfg{
Debug: true,
Log: struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
}{Level: "error", Format: "text"},
Host: "127.0.0.1",
Port: "8888",
}

assert.EqualValues(t, refConfig, config)
})
t.Run("Success parsing complex dirs and files", func(t *testing.T) {
os.Setenv("STAGE", "development")
configBytes, err := ReadConfigs("./config_examples/configuration2")
Expand Down

0 comments on commit c4967dc

Please sign in to comment.