Skip to content

Commit

Permalink
resolve error with chained symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
smgladkovskiy committed Apr 30, 2020
1 parent e2e1980 commit 15c64bf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
15 changes: 7 additions & 8 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"gopkg.in/yaml.v2"
)

const defaultStage = "defaults"

// ReadConfigs Reads yaml files from configuration directory with sub folders
// as application stage and merges config files in one configuration per stage
func ReadConfigs(cfgPath string) ([]byte, error) {
Expand All @@ -38,20 +40,20 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
return nil
}

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

if filepath.Ext(f.Name()) == ".yaml" && (stageDir == "defaults" || stageDir == stage) {
if filepath.Ext(f.Name()) == ".yaml" && (stageDir == defaultStage || stageDir == stage) {
fileList[stageDir] = append(fileList[stageDir], f.Name())
}

return nil
})

// check defaults config existence. Fall down if not
if _, ok := fileList["defaults"]; !ok || len(fileList["defaults"]) == 0 {
if _, ok := fileList[defaultStage]; !ok || len(fileList[defaultStage]) == 0 {
iSay("defaults config is not found in file list `%+v`! Fall down.", fileList)
return nil, fmt.Errorf("no default config")
}
Expand All @@ -78,8 +80,6 @@ func ReadConfigs(cfgPath string) ([]byte, error) {
continue
}

//iSay("file `%s` in folder `%s` config: %+v", file, folder, configFromFile[folder])

if _, ok := configs[folder]; !ok {
configs[folder] = configFromFile[folder]
}
Expand All @@ -95,15 +95,14 @@ func ReadConfigs(cfgPath string) ([]byte, error) {

iSay("Parsed config list: `%+v`", fileListResult)

config := configs["defaults"]
config := configs[defaultStage]

if c, ok := configs[stage]; ok {
if err := mergo.Merge(&config, c, mergo.WithOverride); err == nil {
iSay("Stage `%s` config is loaded and merged with `defaults`", stage)
}
}

//iSay("Resulted config: `%+v`", config)
return yaml.Marshal(config)
}

Expand All @@ -114,7 +113,7 @@ func iSay(pattern string, args ...interface{}) {
// }
}

// getStage Load configuration for stage with fallback to 'development'
// getStage Load configuration for stage with fallback to 'dev'
func getStage() (stage string) {
stage = GetEnv("STAGE", "development")
iSay("Current stage: `%s`", stage)
Expand Down
1 change: 0 additions & 1 deletion test/symnlinkedConfigs/defaults/data

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dev:
log:
level: "error"
format: "text"
1 change: 1 addition & 0 deletions test/symnlinkedConfigs/dev/..data
1 change: 0 additions & 1 deletion test/symnlinkedConfigs/dev/data

This file was deleted.

2 changes: 1 addition & 1 deletion test/symnlinkedConfigs/dev/log.yaml

0 comments on commit 15c64bf

Please sign in to comment.