From c4967dccea0d2c1fccfa83302ee3180bfe04ffb8 Mon Sep 17 00:00:00 2001 From: Sergey Gladkovskiy Date: Wed, 27 May 2020 21:36:53 +0300 Subject: [PATCH] Fix passing stage folder if it is in more than 1 folder after defaults one. Update yaml dependence version. --- config_examples/configuration/prod/log.yaml | 4 +++ .../configuration/prod/service.yaml | 2 ++ go.mod | 2 +- go.sum | 4 +-- reader.go | 5 ++- reader_test.go | 35 +++++++++++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 config_examples/configuration/prod/log.yaml create mode 100644 config_examples/configuration/prod/service.yaml diff --git a/config_examples/configuration/prod/log.yaml b/config_examples/configuration/prod/log.yaml new file mode 100644 index 0000000..cac955c --- /dev/null +++ b/config_examples/configuration/prod/log.yaml @@ -0,0 +1,4 @@ +prod: + log: + level: "error" + format: "text" \ No newline at end of file diff --git a/config_examples/configuration/prod/service.yaml b/config_examples/configuration/prod/service.yaml new file mode 100644 index 0000000..050505a --- /dev/null +++ b/config_examples/configuration/prod/service.yaml @@ -0,0 +1,2 @@ +prod: + debug: true \ No newline at end of file diff --git a/go.mod b/go.mod index 720a5cc..adc4f04 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 0816fae..11503c5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/reader.go b/reader.go index a0e2426..95d3ddb 100644 --- a/reader.go +++ b/reader.go @@ -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 } diff --git a/reader_test.go b/reader_test.go index d3dfcd7..c4e65ea 100644 --- a/reader_test.go +++ b/reader_test.go @@ -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")