Refactor reporting to provide instant feedback on default output
This commit is contained in:
53
certo/report.py
Normal file
53
certo/report.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import json
|
||||
|
||||
|
||||
class CheckReporter:
|
||||
def __init__(self):
|
||||
self.checks = list()
|
||||
|
||||
def add_check(self, check):
|
||||
self.checks.append(check)
|
||||
|
||||
def failed(self):
|
||||
return list(r for r in self.checks if not r.check_successful)
|
||||
|
||||
def num_failed(self):
|
||||
return len(self.failed())
|
||||
|
||||
def report(self):
|
||||
...
|
||||
|
||||
|
||||
class JSONReporter(CheckReporter):
|
||||
def __make_check_serialisable(check):
|
||||
"""
|
||||
Converts a CertCheckResult for json serialisation
|
||||
:param check: CertCheckResult as output by checks
|
||||
:return: check as dict() with appropriate type conversions for json.dumps
|
||||
"""
|
||||
ret = check._asdict()
|
||||
if check.expiration_date:
|
||||
ret["expiration_date"] = check.expiration_date.strftime("%c %Z")
|
||||
return ret
|
||||
|
||||
def report(self):
|
||||
return json.dumps(
|
||||
list(
|
||||
map(
|
||||
lambda check: JSONReporter.__make_check_serialisable(check),
|
||||
self.checks,
|
||||
)
|
||||
),
|
||||
indent=4,
|
||||
)
|
||||
|
||||
|
||||
class DefaultReporter(CheckReporter):
|
||||
def add_check(self, check):
|
||||
super().add_check(check)
|
||||
result = f"[{'PASS' if check.check_successful else 'FAIL'}] Check host {check.hostname}"
|
||||
if check.debug:
|
||||
result += f" - {check.debug}"
|
||||
if check.expiration_date:
|
||||
result += f" - Certificate expires on {check.expiration_date} {check.expiration_date.tzname()}"
|
||||
print(result)
|
||||
Reference in New Issue
Block a user