Skip to content

Commit

Permalink
Add alternative file path for yaml
Browse files Browse the repository at this point in the history
We do this because because sometimes name files with .yaml sometimes with
.yml even though the same thing.
  • Loading branch information
pavleprica committed Nov 16, 2022
1 parent 3d6679b commit ae2fe48
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cfgl/yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func LoadConfigFile(environment string) (*cfgm.Manager, error) {
}

configFilePath := getConfigFilePath(environment, workDir)
fileContent, err := os.ReadFile(configFilePath)
fileContent, err := readFile(configFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, errors.New(fmt.Sprintf("%s - %s", configFileNotFound, configFilePath))
Expand All @@ -49,6 +49,18 @@ func LoadConfigFile(environment string) (*cfgm.Manager, error) {
return cfgm.NewManager(configuration), nil
}

func readFile(configFilePath string) ([]byte, error) {
fileContent, err := os.ReadFile(configFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) && strings.HasSuffix(configFilePath, ".yaml") {
alternativeYamlExtensionPath := configFilePath[:strings.Index(configFilePath, ".yaml")] + ".yml"
return readFile(alternativeYamlExtensionPath)
}
return nil, err
}
return fileContent, nil
}

func getConfigFilePath(environment string, workDir string) string {
if cfge.GetEnvBoolOrDefault(configTestRunKey, false) {
return filepath.Join(workDir, "..", fmt.Sprintf("config-%s.yaml", environment))
Expand Down

0 comments on commit ae2fe48

Please sign in to comment.