Skip to content

Commit 15c64bf

Browse files
committed
resolve error with chained symlinks
1 parent e2e1980 commit 15c64bf

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

reader.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1719
func 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'
118117
func getStage() (stage string) {
119118
stage = GetEnv("STAGE", "development")
120119
iSay("Current stage: `%s`", stage)

test/symnlinkedConfigs/defaults/data

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dev:
2+
log:
3+
level: "error"
4+
format: "text"

test/symnlinkedConfigs/dev/..data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
..2018_10_05_08_42_42.488643040/

test/symnlinkedConfigs/dev/data

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../configuration/dev/log.yaml
1+
..data/log.yaml

0 commit comments

Comments
 (0)