1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Don't swallow error raised from file exists helper method (#1019)

* Don't swallow error raised from file exists helper method

* streamline
This commit is contained in:
Marcus Holl 2019-12-11 10:13:23 +01:00 committed by GitHub
parent 3f3db13a8a
commit 9cd4950437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 24 deletions

View File

@ -55,10 +55,18 @@ func generateConfig() error {
} }
var customConfig io.ReadCloser var customConfig io.ReadCloser
if piperutils.FileExists(GeneralConfig.CustomConfig) { {
customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig) exists, e := piperutils.FileExists(GeneralConfig.CustomConfig)
if err != nil {
return errors.Wrap(err, "config: open failed") if e != nil {
return e
}
if exists {
customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig)
if err != nil {
return errors.Wrap(err, "config: open failed")
}
} }
} }

View File

@ -83,15 +83,22 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
var customConfig io.ReadCloser var customConfig io.ReadCloser
var err error var err error
//accept that config file and defaults cannot be loaded since both are not mandatory here //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 { exists, e := piperutils.FileExists(GeneralConfig.CustomConfig)
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
}
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 var defaultConfig []io.ReadCloser
for _, f := range GeneralConfig.DefaultConfig { for _, f := range GeneralConfig.DefaultConfig {
//ToDo: support also https as source //ToDo: support also https as source

View File

@ -113,7 +113,7 @@ func xsDeploy(XsDeployOptions xsDeployOptions) error {
} }
func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner, func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner,
fExists func(string) bool, fExists func(string) (bool, error),
fCopy func(string, string) (int64, error), fCopy func(string, string) (int64, error),
fRemove func(string) error, fRemove func(string) error,
stdout io.Writer) error { stdout io.Writer) error {
@ -143,8 +143,14 @@ func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner,
performLogout := mode == Deploy || (mode == BGDeploy && action != None) performLogout := mode == Deploy || (mode == BGDeploy && action != None)
log.Entry().Debugf("performLogin: %t, performLogout: %t", performLogin, performLogout) 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 { if action != None && len(XsDeployOptions.OperationID) == 0 {
@ -194,8 +200,14 @@ func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner,
if loginErr == nil && err == nil { 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) copyFileFromPwdToHome(xsSessionFile, fCopy)

View File

@ -33,8 +33,8 @@ func TestDeploy(t *testing.T) {
var copiedFiles []string var copiedFiles []string
var removedFiles []string var removedFiles []string
fExists := func(path string) bool { fExists := func(path string) (bool, error) {
return path == "dummy.mtar" || path == ".xs_session" return path == "dummy.mtar" || path == ".xs_session", nil
} }
fCopy := func(src, dest string) (int64, error) { fCopy := func(src, dest string) (int64, error) {

View File

@ -167,7 +167,6 @@ func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON stri
} }
} }
} }
return stepConfig, nil return stepConfig, nil
} }

View File

@ -7,18 +7,29 @@ import (
) )
// FileExists ... // FileExists ...
func FileExists(filename string) bool { func FileExists(filename string) (bool, error) {
info, err := os.Stat(filename) info, err := os.Stat(filename)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false return false, nil
} }
return !info.IsDir() if err != nil {
return false, err
}
return !info.IsDir(), nil
} }
// Copy ... // Copy ...
func Copy(src, dst string) (int64, error) { 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") return 0, errors.New("Source file '" + src + "' does not exist")
} }