-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return exit code 1 even if stop on error is enabled #142
Merged
pballandras
merged 3 commits into
main
from
fix/finish-healthy-syncs-before-erroring-out
Apr 11, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"github.com/coveooss/credentials-sync/credentials" | ||
"github.com/coveooss/credentials-sync/logger" | ||
"github.com/coveooss/credentials-sync/targets" | ||
"github.com/hashicorp/go-multierror" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gives us a struct
|
||
) | ||
|
||
// Configuration represents the parsed configuration file given to the application | ||
|
@@ -50,12 +51,16 @@ func (config *Configuration) Sync() error { | |
for _, target := range allTargets { | ||
go config.initTarget(target, creds, initChannel) | ||
} | ||
// We will use this to accumulate errors that happen if config.StopOnError is set to false | ||
// the multierror.Error implements error so we use the interface to type the accumulator | ||
var errorAccumulator error | ||
for i := 0; i < len(allTargets); i++ { | ||
initTarget := <-initChannel | ||
if err, ok := initTarget.(error); ok { | ||
if config.StopOnError { | ||
return err | ||
} | ||
errorAccumulator = multierror.Append(errorAccumulator, err) | ||
logger.Log.Error(err) | ||
} else { | ||
validTargets = append(validTargets, initTarget.(targets.Target)) | ||
|
@@ -72,7 +77,10 @@ func (config *Configuration) Sync() error { | |
// Check for errors. Errors are only passed back if StopOnError is true so this should always return | ||
err := <-errorChannel | ||
if err != nil { | ||
return err | ||
if config.StopOnError { | ||
return err | ||
} | ||
errorAccumulator = multierror.Append(errorAccumulator, err) | ||
} | ||
} | ||
|
||
|
@@ -81,7 +89,8 @@ func (config *Configuration) Sync() error { | |
parallelismChannel <- true | ||
} | ||
|
||
return nil | ||
// This is either a nil, or a collection of past errors which we want to bubble up | ||
return errorAccumulator | ||
} | ||
|
||
func (config *Configuration) initTarget(target targets.Target, creds []credentials.Credentials, channel chan interface{}) { | ||
|
@@ -101,9 +110,11 @@ func (config *Configuration) initTarget(target targets.Target, creds []credentia | |
} | ||
|
||
func (config *Configuration) syncCredentials(target targets.Target, credentialsList []credentials.Credentials, parallelismChannel chan bool, errorChannel chan error) { | ||
var err error | ||
// We will use this to accumulate errors that happen if config.StopOnError is set to false | ||
// the multierror.Error implements error so we use the interface to type the accumulator | ||
var errorAccumulator error | ||
defer func() { | ||
errorChannel <- err | ||
errorChannel <- errorAccumulator | ||
<-parallelismChannel | ||
}() | ||
|
||
|
@@ -114,12 +125,17 @@ func (config *Configuration) syncCredentials(target targets.Target, credentialsL | |
} | ||
} | ||
|
||
if err = config.UpdateListOfCredentials(target, filteredCredentials); err != nil { | ||
return | ||
if err := config.UpdateListOfCredentials(target, filteredCredentials); err != nil { | ||
errorAccumulator = multierror.Append(errorAccumulator, err) | ||
if config.StopOnError { | ||
return | ||
} | ||
} | ||
if err = config.DeleteListOfCredentials(target); err != nil { | ||
return | ||
if err := config.DeleteListOfCredentials(target); err != nil { | ||
errorAccumulator = multierror.Append(errorAccumulator, err) | ||
if config.StopOnError { | ||
return | ||
} | ||
} | ||
|
||
logger.Log.Infof("Finished sync to %s", target.GetName()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it so when returning an error from a command, we don't: