You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
Support yml and yaml extension of config files (#1153)
* Support yml and yaml extension of config files * Update cmd/piper.go Co-Authored-By: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
35
cmd/piper.go
35
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
|
||||
}
|
||||
|
@@ -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)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user