From ae2fe488bcbdc90fe06dae60e68ec0b040c661c5 Mon Sep 17 00:00:00 2001 From: Pavle Prica Date: Wed, 16 Nov 2022 17:29:06 +0100 Subject: [PATCH] Add alternative file path for yaml We do this because because sometimes name files with .yaml sometimes with .yml even though the same thing. --- cfgl/yaml_file.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cfgl/yaml_file.go b/cfgl/yaml_file.go index 4e3fc7a..6e190a2 100644 --- a/cfgl/yaml_file.go +++ b/cfgl/yaml_file.go @@ -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)) @@ -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))