@@ -12,6 +12,8 @@ import (
1212 "gopkg.in/yaml.v2"
1313)
1414
15+ const defaultStage = "defaults"
16+
1517// ReadConfigs Reads yaml files from configuration directory with sub folders
1618// as application stage and merges config files in one configuration per stage
1719func ReadConfigs (cfgPath string ) ([]byte , error ) {
@@ -38,20 +40,20 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
3840 return nil
3941 }
4042
41- if f .IsDir () {
43+ if f .IsDir () && ( stageDir == "" || stageDir == defaultStage ) {
4244 stageDir = f .Name ()
4345 return nil
4446 }
4547
46- if filepath .Ext (f .Name ()) == ".yaml" && (stageDir == "defaults" || stageDir == stage ) {
48+ if filepath .Ext (f .Name ()) == ".yaml" && (stageDir == defaultStage || stageDir == stage ) {
4749 fileList [stageDir ] = append (fileList [stageDir ], f .Name ())
4850 }
4951
5052 return nil
5153 })
5254
5355 // check defaults config existence. Fall down if not
54- if _ , ok := fileList ["defaults" ]; ! ok || len (fileList ["defaults" ]) == 0 {
56+ if _ , ok := fileList [defaultStage ]; ! ok || len (fileList [defaultStage ]) == 0 {
5557 iSay ("defaults config is not found in file list `%+v`! Fall down." , fileList )
5658 return nil , fmt .Errorf ("no default config" )
5759 }
@@ -78,8 +80,6 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
7880 continue
7981 }
8082
81- //iSay("file `%s` in folder `%s` config: %+v", file, folder, configFromFile[folder])
82-
8383 if _ , ok := configs [folder ]; ! ok {
8484 configs [folder ] = configFromFile [folder ]
8585 }
@@ -95,15 +95,14 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
9595
9696 iSay ("Parsed config list: `%+v`" , fileListResult )
9797
98- config := configs ["defaults" ]
98+ config := configs [defaultStage ]
9999
100100 if c , ok := configs [stage ]; ok {
101101 if err := mergo .Merge (& config , c , mergo .WithOverride ); err == nil {
102102 iSay ("Stage `%s` config is loaded and merged with `defaults`" , stage )
103103 }
104104 }
105105
106- //iSay("Resulted config: `%+v`", config)
107106 return yaml .Marshal (config )
108107}
109108
@@ -114,7 +113,7 @@ func iSay(pattern string, args ...interface{}) {
114113 // }
115114}
116115
117- // getStage Load configuration for stage with fallback to 'development '
116+ // getStage Load configuration for stage with fallback to 'dev '
118117func getStage () (stage string ) {
119118 stage = GetEnv ("STAGE" , "development" )
120119 iSay ("Current stage: `%s`" , stage )
0 commit comments