/
output_handler.py
56 lines (45 loc) · 1.46 KB
/
output_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from abc import ABC, abstractmethod
from typing import Optional
import click
from ggshield.scan import ScanCollection
class OutputHandler(ABC):
show_secrets: bool = False
verbose: bool = False
output: Optional[str] = None
def __init__(
self,
show_secrets: bool,
verbose: bool,
output: Optional[str] = None,
):
self.show_secrets = show_secrets
self.verbose = verbose
self.output = output
def process_scan(self, scan: ScanCollection) -> int:
"""Process a scan collection, write the report to :attr:`self.output`
:param scan: The scan collection to process
:return: The exit code
"""
text = self._process_scan_impl(scan)
if self.output:
with open(self.output, "w+") as f:
f.write(text)
else:
click.echo(text)
return OutputHandler._get_exit_code(scan)
@abstractmethod
def _process_scan_impl(self, scan: ScanCollection) -> str:
"""Implementation of scan processing,
called by :meth:`OutputHandler.process_scan`
Must return a string for the report.
:param scan: The scan collection to process
:return: The content
"""
raise NotImplementedError()
@staticmethod
def _get_exit_code(scan: ScanCollection) -> int:
if scan.has_iac_result:
return 1
if scan.has_new_secrets:
return 1
return 0