Skip to content

Commit

Permalink
Merge pull request #143 from Skyscanner/issue-139
Browse files Browse the repository at this point in the history
Return non-zero exit code (1) if any template is marked as not valid by CFRipper
  • Loading branch information
ocrawford555 committed Jan 25, 2021
2 parents 63fee31 + d3ff7b0 commit e715957
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cfripper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def process_template(
output_folder: Optional[str],
output_format: str,
rules_config_file: Optional[str],
) -> None:
) -> bool:
logging.info(f"Analysing {template.name}...")

cfmodel = get_cfmodel(template)
Expand All @@ -105,6 +105,8 @@ def process_template(

output_handling(template.name, formatted_result, output_format, output_folder)

return result.valid


@click.command()
@click.version_option(prog_name="cfripper", version=__version__)
Expand Down Expand Up @@ -149,25 +151,35 @@ def process_template(
"--rules-config-file", type=click.File("r"), help="Loads rules configuration file (type: [.py, .pyc])",
)
def cli(templates, logging_level, resolve_parameters, **kwargs):
"""Analyse AWS Cloudformation templates passed by parameter."""
"""
Analyse AWS Cloudformation templates passed by parameter.
Exit codes:
- 0 = all templates valid and scanned successfully
- 1 = error / issue in scanning at least one template
- 2 = at least one template is not valid according to CFRipper (template scanned successfully)
- 3 = unknown / unhandled exception in scanning the templates
"""
try:
setup_logging(logging_level)

if kwargs["resolve"] and resolve_parameters:
resolve_parameters = convert_json_or_yaml_to_dict(resolve_parameters.read())

for template in templates:
results_of_templates = [
process_template(template=template, resolve_parameters=resolve_parameters, **kwargs)
for template in templates
]
sys.exit(2 if False in results_of_templates else 0)

except Exception as e:
logging.exception(
"Unhandled exception raised, please create an issue wit the error message at "
"Unhandled exception raised, please create an issue with the error message at "
"https://github.com/Skyscanner/cfripper/issues"
)
try:
sys.exit(e.errno)
except AttributeError:
sys.exit(1)
sys.exit(3)


if __name__ == "__main__":
Expand Down

0 comments on commit e715957

Please sign in to comment.