diff --git a/cmd/piper.go b/cmd/piper.go index 1817048c1..33532bbcd 100644 --- a/cmd/piper.go +++ b/cmd/piper.go @@ -5,6 +5,8 @@ import ( "fmt" "io" "os" + "path/filepath" + "strings" "github.com/SAP/jenkins-library/pkg/config" "github.com/SAP/jenkins-library/pkg/log" @@ -100,20 +102,18 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin var err error //accept that config file and defaults cannot be loaded since both are not mandatory here { - exists, e := piperutils.FileExists(GeneralConfig.CustomConfig) - - if e != nil { - return e - } + projectConfigFile := getProjectConfigFile(GeneralConfig.CustomConfig) + exists, err := piperutils.FileExists(projectConfigFile) if exists { - if customConfig, err = openFile(GeneralConfig.CustomConfig); err != nil { - errors.Wrapf(err, "Cannot read '%s'", GeneralConfig.CustomConfig) + if customConfig, err = openFile(projectConfigFile); err != nil { + errors.Wrapf(err, "Cannot read '%s'", projectConfigFile) } } else { - log.Entry().Infof("Project config file '%s' does not exist. No project configuration available.", GeneralConfig.CustomConfig) + log.Entry().Infof("Project config file '%s' does not exist. No project configuration available.", projectConfigFile) customConfig = nil } + } var defaultConfig []io.ReadCloser for _, f := range GeneralConfig.DefaultConfig { @@ -145,3 +145,22 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin return nil } + +func getProjectConfigFile(name string) string { + + var altName string + if ext := filepath.Ext(name); ext == ".yml" { + altName = fmt.Sprintf("%v.yaml", strings.TrimSuffix(name, ext)) + } else if ext == "yaml" { + altName = fmt.Sprintf("%v.yml", strings.TrimSuffix(name, ext)) + } + + fileExists, _ := piperutils.FileExists(name) + altExists, _ := piperutils.FileExists(altName) + + // configured filename will always take precedence, even if not existing + if !fileExists && altExists { + return altName + } + return name +} diff --git a/cmd/piper_test.go b/cmd/piper_test.go index 9d612d59b..ab2f61053 100644 --- a/cmd/piper_test.go +++ b/cmd/piper_test.go @@ -1,8 +1,11 @@ package cmd import ( + "fmt" "io" "io/ioutil" + "os" + "path/filepath" "strings" "testing" @@ -195,3 +198,38 @@ func TestPrepareConfig(t *testing.T) { }) }) } + +func TestGetProjectConfigFile(t *testing.T) { + + tt := []struct { + filename string + filesAvailable []string + expected string + }{ + {filename: ".pipeline/config.yml", filesAvailable: []string{}, expected: ".pipeline/config.yml"}, + {filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yml"}, expected: ".pipeline/config.yml"}, + {filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yaml"}, expected: ".pipeline/config.yaml"}, + {filename: ".pipeline/config.yaml", filesAvailable: []string{".pipeline/config.yml", ".pipeline/config.yaml"}, expected: ".pipeline/config.yaml"}, + {filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yml", ".pipeline/config.yaml"}, expected: ".pipeline/config.yml"}, + } + + for run, test := range tt { + t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) { + dir, err := ioutil.TempDir("", "") + defer os.RemoveAll(dir) // clean up + assert.NoError(t, err) + + if len(test.filesAvailable) > 0 { + configFolder := filepath.Join(dir, filepath.Dir(test.filesAvailable[0])) + err = os.MkdirAll(configFolder, 0700) + assert.NoError(t, err) + } + + for _, file := range test.filesAvailable { + ioutil.WriteFile(filepath.Join(dir, file), []byte("general:"), 0700) + } + + assert.Equal(t, filepath.Join(dir, test.expected), getProjectConfigFile(filepath.Join(dir, test.filename))) + }) + } +}