Initial commit: Basic CLI and checks
Certo supports polling one or several hostnames. Output can be human-readable or JSON.
This commit is contained in:
34
certo/output.py
Normal file
34
certo/output.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
from typing import List
|
||||
|
||||
from certo.checks.hostname import CertCheckResult
|
||||
|
||||
|
||||
def json_output(cert_check_results: List[CertCheckResult]):
|
||||
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
|
||||
|
||||
return json.dumps(
|
||||
list(map(lambda check: make_check_serialisable(check), cert_check_results)),
|
||||
indent=4,
|
||||
)
|
||||
|
||||
|
||||
def default_output(cert_check_results: List[CertCheckResult]):
|
||||
res = list()
|
||||
for check in cert_check_results:
|
||||
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()}"
|
||||
res.append(result)
|
||||
return "\n".join(res)
|
||||
Reference in New Issue
Block a user