1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +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,12 +55,20 @@ func generateConfig() error {
}
var customConfig io.ReadCloser
if piperutils.FileExists(GeneralConfig.CustomConfig) {
{
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")
}
}
}
defaultConfig, paramFilter, err := defaultsAndFilters(&metadata, metadata.Metadata.Name)
if err != nil {

View File

@ -83,7 +83,14 @@ 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) {
{
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)
}
@ -91,7 +98,7 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
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

View File

@ -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,9 +143,15 @@ 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) {
{
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 {
return errors.New(fmt.Sprintf("OperationID was not provided. This is required for action '%s'.", action))
@ -194,9 +200,15 @@ func runXsDeploy(XsDeployOptions xsDeployOptions, s shellRunner,
if loginErr == nil && err == nil {
if !fExists(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)

View File

@ -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) {

View File

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

View File

@ -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")
}