Refactor reporting to provide instant feedback on default output
This commit is contained in:
@@ -12,7 +12,7 @@ import logging
|
||||
from docopt import docopt
|
||||
|
||||
from certo.checks.hostname import check_host_certificate_expiration
|
||||
from certo.output import default_output, json_output
|
||||
from certo.report import JSONReporter, DefaultReporter
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = docopt(__doc__)
|
||||
@@ -23,17 +23,18 @@ if __name__ == "__main__":
|
||||
hostnames = args.get("<hostnames>")
|
||||
days_to_expiration = int(args.get("--days-to-expiration"))
|
||||
|
||||
results = []
|
||||
# @todo factory
|
||||
if output_as_json:
|
||||
reporter = JSONReporter()
|
||||
else:
|
||||
reporter = DefaultReporter()
|
||||
|
||||
# @todo async
|
||||
for hs in hostnames:
|
||||
logging.info(f"Getting CERT from {hs}")
|
||||
results.append(check_host_certificate_expiration(hs, days_to_expiration))
|
||||
reporter.add_check(check_host_certificate_expiration(hs, days_to_expiration))
|
||||
|
||||
failed = list(r for r in results if not r.check_successful)
|
||||
if log := reporter.report():
|
||||
print(log)
|
||||
|
||||
if output_as_json:
|
||||
print(json_output(results))
|
||||
else:
|
||||
print(default_output(results))
|
||||
|
||||
exit(len(failed))
|
||||
exit(reporter.num_failed())
|
||||
|
||||
Reference in New Issue
Block a user