From 9cd4950437c2dfd3fa0e241ba10034aa6182e612 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 11 Dec 2019 10:13:23 +0100 Subject: [PATCH] Don't swallow error raised from file exists helper method (#1019) * Don't swallow error raised from file exists helper method * streamline --- cmd/getConfig.go | 16 ++++++++++++---- cmd/piper.go | 23 +++++++++++++++-------- cmd/xsDeploy.go | 22 +++++++++++++++++----- cmd/xsDeploy_test.go | 4 ++-- pkg/config/config.go | 1 - pkg/piperutils/FileUtils.go | 19 +++++++++++++++---- 6 files changed, 61 insertions(+), 24 deletions(-) diff --git a/cmd/getConfig.go b/cmd/getConfig.go index b22e7ad9b..d40aab99a 100644 --- a/cmd/getConfig.go +++ b/cmd/getConfig.go @@ -55,10 +55,18 @@ func generateConfig() error { } var customConfig io.ReadCloser - if piperutils.FileExists(GeneralConfig.CustomConfig) { - customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig) - if err != nil { - return errors.Wrap(err, "config: open failed") + { + exists, e := piperutils.FileExists(GeneralConfig.CustomConfig) + + if e != nil { + return e + } + + if exists { + customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig) + if err != nil { + return errors.Wrap(err, "config: open failed") + } } } diff --git a/cmd/piper.go b/cmd/piper.go index 3dd3f3d5e..a2ef764dc 100644 --- a/cmd/piper.go +++ b/cmd/piper.go @@ -83,15 +83,22 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin var customConfig io.ReadCloser var err error //accept that config file and defaults cannot be loaded since both are not mandatory here - 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 - } + { + exists, e := piperutils.FileExists(GeneralConfig.CustomConfig) + if e != nil { + return e + } + + if exists { + 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 diff --git a/cmd/xsDeploy.go b/cmd/xsDeploy.go index 82c56a13d..d9ae289dd 100644 --- a/cmd/xsDeploy.go +++ b/cmd/xsDeploy.go @@ -113,7 +113,7 @@ func xsDeploy(XsDeployOptions xsDeployOptions) error { } func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner, - fExists func(string) bool, + fExists func(string) (bool, error), fCopy func(string, string) (int64, error), fRemove func(string) error, stdout io.Writer) error { @@ -143,8 +143,14 @@ func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner, performLogout := mode == Deploy || (mode == BGDeploy && action != None) log.Entry().Debugf("performLogin: %t, performLogout: %t", performLogin, performLogout) - if action == None && !fExists(XsDeployOptions.MtaPath) { - return errors.New(fmt.Sprintf("Deployable '%s' does not exist", XsDeployOptions.MtaPath)) + { + exists, e := fExists(XsDeployOptions.MtaPath) + if e != nil { + return e + } + if action == None && !exists { + return errors.New(fmt.Sprintf("Deployable '%s' does not exist", XsDeployOptions.MtaPath)) + } } if action != None && len(XsDeployOptions.OperationID) == 0 { @@ -194,8 +200,14 @@ func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner, if loginErr == nil && err == nil { - if !fExists(xsSessionFile) { - return fmt.Errorf("xs session file does not exist (%s)", xsSessionFile) + { + exists, e := fExists(xsSessionFile) + if e != nil { + return e + } + if !exists { + return fmt.Errorf("xs session file does not exist (%s)", xsSessionFile) + } } copyFileFromPwdToHome(xsSessionFile, fCopy) diff --git a/cmd/xsDeploy_test.go b/cmd/xsDeploy_test.go index d672eca9d..14b2847c4 100644 --- a/cmd/xsDeploy_test.go +++ b/cmd/xsDeploy_test.go @@ -33,8 +33,8 @@ func TestDeploy(t *testing.T) { var copiedFiles []string var removedFiles []string - fExists := func(path string) bool { - return path == "dummy.mtar" || path == ".xs_session" + fExists := func(path string) (bool, error) { + return path == "dummy.mtar" || path == ".xs_session", nil } fCopy := func(src, dest string) (int64, error) { diff --git a/pkg/config/config.go b/pkg/config/config.go index c28bf128e..6f3430c89 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -167,7 +167,6 @@ func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON stri } } } - return stepConfig, nil } diff --git a/pkg/piperutils/FileUtils.go b/pkg/piperutils/FileUtils.go index 241bc1ceb..edf5b60f3 100644 --- a/pkg/piperutils/FileUtils.go +++ b/pkg/piperutils/FileUtils.go @@ -7,18 +7,29 @@ import ( ) // FileExists ... -func FileExists(filename string) bool { +func FileExists(filename string) (bool, error) { info, err := os.Stat(filename) + if os.IsNotExist(err) { - return false + return false, nil } - return !info.IsDir() + if err != nil { + return false, err + } + + return !info.IsDir(), nil } // Copy ... func Copy(src, dst string) (int64, error) { - if !FileExists(src) { + exists, err := FileExists(src) + + if err != nil { + return 0, err + } + + if !exists { return 0, errors.New("Source file '" + src + "' does not exist") }