From c9883bf5b082350c0bce8f68d55a87d9913f8011 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Mon, 11 Nov 2019 09:52:44 +0100 Subject: [PATCH] Pr/read project config only if it exists (#959) * Read the project config only if it exists This avoid trying reading the file and have the control flow based on errors. Beside that it helps troubleshooting when we have some logging (debug level only). * formatting only * Adjust log level --- cmd/piper.go | 16 +++++++++++++--- cmd/piper_test.go | 8 ++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cmd/piper.go b/cmd/piper.go index 9f2995c11..d74474f56 100644 --- a/cmd/piper.go +++ b/cmd/piper.go @@ -8,6 +8,8 @@ import ( "strings" "github.com/SAP/jenkins-library/pkg/config" + "github.com/SAP/jenkins-library/pkg/log" + "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -78,9 +80,18 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.StepConfigJSON, filters) } else { // use config & defaults - + var customConfig io.ReadCloser + var err error //accept that config file and defaults cannot be loaded since both are not mandatory here - customConfig, _ := openFile(GeneralConfig.CustomConfig) + if piperutils.FileExists(GeneralConfig.CustomConfig) { + if customConfig, err = openFile(GeneralConfig.CustomConfig); err != nil { + errors.Wrapf(err, "Cannot read '%s'", GeneralConfig.CustomConfig) + } + } else { + log.Entry().Infof("Project config file '%s' does not exist. No project configuration available.", GeneralConfig.CustomConfig) + customConfig = nil + } + var defaultConfig []io.ReadCloser for _, f := range GeneralConfig.DefaultConfig { //ToDo: support also https as source @@ -88,7 +99,6 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin defaultConfig = append(defaultConfig, fc) } - var err error stepConfig, err = myConfig.GetStepConfig(flagValues, GeneralConfig.ParametersJSON, customConfig, defaultConfig, filters, metadata.Spec.Inputs.Parameters, GeneralConfig.StageName, stepName) if err != nil { return errors.Wrap(err, "retrieving step configuration failed") diff --git a/cmd/piper_test.go b/cmd/piper_test.go index 35e2ee5c5..8d94cae7e 100644 --- a/cmd/piper_test.go +++ b/cmd/piper_test.go @@ -13,8 +13,8 @@ import ( ) type execMockRunner struct { - dir []string - calls []execCall + dir []string + calls []execCall shouldFailWith error } @@ -24,8 +24,8 @@ type execCall struct { } type shellMockRunner struct { - dir string - calls []string + dir string + calls []string shouldFailWith error }