|
1 | | -import json |
2 | | -import sys |
3 | | -from typing import Dict, List, Union |
4 | | - |
5 | | -import marshmallow |
6 | | -import yaml |
7 | | -from marshmallow import Schema |
8 | | -from rich.table import Table |
9 | | - |
10 | 1 | from probely_cli.cli.commands.targets.schemas import TargetApiFiltersSchema |
11 | | -from probely_cli.cli.enums import OutputEnum |
12 | | -from probely_cli.cli.renderers import ( |
13 | | - get_printable_date, |
14 | | - get_printable_enum_value, |
15 | | - get_printable_labels, |
16 | | -) |
| 2 | +from probely_cli.cli.common import prepare_filters_for_api |
| 3 | +from probely_cli.cli.enums import EntityTypeEnum, OutputEnum |
| 4 | + |
| 5 | +from probely_cli.cli.renderers import OutputRenderer |
17 | 6 | from probely_cli.exceptions import ProbelyCLIValidation |
18 | | -from probely_cli.sdk.enums import TargetRiskEnum |
19 | 7 | from probely_cli.sdk.targets import list_targets, retrieve_targets |
20 | 8 |
|
21 | | -TARGET_NEVER_SCANNED_OUTPUT: str = "Never_scanned" |
22 | | - |
23 | | - |
24 | | -def _get_printable_last_scan_date(target: Dict) -> str: |
25 | | - last_scan_obj: Union[dict, None] = target.get("last_scan", None) |
26 | | - |
27 | | - if last_scan_obj is None: |
28 | | - return TARGET_NEVER_SCANNED_OUTPUT |
29 | | - |
30 | | - last_scan_start_date_str: Union[str, None] = last_scan_obj.get("started", None) |
31 | | - |
32 | | - return get_printable_date(last_scan_start_date_str, TARGET_NEVER_SCANNED_OUTPUT) |
33 | | - |
34 | | - |
35 | | -def get_tabled_targets(targets_list: List[Dict]) -> Table: |
36 | | - table = Table(box=None) |
37 | | - table.add_column("ID") |
38 | | - table.add_column("NAME") |
39 | | - table.add_column("URL") |
40 | | - table.add_column("RISK") |
41 | | - table.add_column("LAST_SCAN") |
42 | | - table.add_column("LABELS") |
43 | | - |
44 | | - for target in targets_list: |
45 | | - asset = target.get("site") |
46 | | - |
47 | | - table.add_row( |
48 | | - target.get("id"), |
49 | | - asset.get("name", "N/D"), |
50 | | - asset.get("url"), |
51 | | - get_printable_enum_value(TargetRiskEnum, target["risk"]), |
52 | | - _get_printable_last_scan_date(target), # last_scan |
53 | | - get_printable_labels(target["labels"]), |
54 | | - ) |
55 | | - |
56 | | - return table |
57 | | - |
58 | | - |
59 | | -def target_filters_handler(args) -> dict: |
60 | | - filters_schema: Schema = TargetApiFiltersSchema() |
61 | | - try: |
62 | | - api_ready_filters = filters_schema.load(vars(args)) |
63 | | - except marshmallow.ValidationError as e: |
64 | | - # TODO: translate validations? |
65 | | - raise ProbelyCLIValidation(str(e)) |
66 | | - |
67 | | - return api_ready_filters |
68 | | - |
69 | | - |
70 | | -def build_cmd_output(args, targets_list): |
71 | | - output_type = OutputEnum[args.output] if args.output else None |
72 | | - |
73 | | - if output_type == OutputEnum.JSON: |
74 | | - return json.dumps(targets_list, indent=2) |
75 | | - |
76 | | - if output_type == OutputEnum.YAML: |
77 | | - return yaml.dump( |
78 | | - targets_list, |
79 | | - indent=2, |
80 | | - width=sys.maxsize, # avoids word wrapping |
81 | | - ) |
82 | | - |
83 | | - return get_tabled_targets(targets_list) |
84 | | - |
85 | 9 |
|
86 | 10 | def targets_get_command_handler(args): |
87 | 11 | """ |
88 | 12 | Lists all accessible targets of client |
89 | 13 | """ |
90 | | - filters = target_filters_handler(args) |
| 14 | + filters = prepare_filters_for_api(TargetApiFiltersSchema, args) |
91 | 15 | targets_ids = args.target_ids |
92 | 16 |
|
93 | 17 | if filters and targets_ids: |
94 | 18 | raise ProbelyCLIValidation("filters and Target IDs are mutually exclusive.") |
95 | 19 |
|
96 | 20 | if targets_ids: |
97 | | - targets_list = retrieve_targets(targets_ids=targets_ids) |
| 21 | + targets_generator = retrieve_targets(targets_ids=targets_ids) |
98 | 22 | else: |
99 | | - targets_list = list_targets(targets_filters=filters) |
| 23 | + targets_generator = list_targets(targets_filters=filters) |
| 24 | + |
| 25 | + output_type = OutputEnum[args.output] if args.output else None |
100 | 26 |
|
101 | | - cmd_output = build_cmd_output(args, targets_list) |
102 | | - args.console.print(cmd_output) |
| 27 | + renderer = OutputRenderer( |
| 28 | + records=targets_generator, |
| 29 | + output_type=output_type, |
| 30 | + console=args.console, |
| 31 | + entity_type=EntityTypeEnum.TARGET, |
| 32 | + ) |
| 33 | + renderer.render() |
0 commit comments