diff --git a/contributions/cli_json_output_for_scan_repor.py b/contributions/cli_json_output_for_scan_repor.py new file mode 100644 index 0000000..c095648 --- /dev/null +++ b/contributions/cli_json_output_for_scan_repor.py @@ -0,0 +1,59 @@ +import json +from typing import List, Dict +# Assuming these are defined elsewhere in the project structure +# from maccleaner.core.scan_report import ScanReport +# from maccleaner.util.reporting import report # (placeholder for scan results) + + +def format_scan_report(reports: List[Dict]) -> str: + """ + Formats the list of reports either as pretty JSON or structured text. + + Args: + reports: A list of dictionaries, where each dict represents a + ScanReport and must contain keys: 'path', 'target_id', + 'size_bytes', 'age_days'. + + Returns: + A string containing the formatted report output. + """ + if not reports: + return "No scan reports found." + + # Check if a specific format requirement (JSON) is met, usually handled by + # checking arguments upstream, but this function must handle both paths. + is_json_mode = True # In a real system, this flag would be passed in or derived + + if is_json_mode: + try: + # Serialize the list of reports directly into JSON format + return json.dumps(reports, indent=4) + except TypeError as e: + # Handle cases where report data might contain non-serializable types + return f"Error generating JSON output: {e}" + else: + # Fallback to human-readable text formatting (existing functionality) + output = ["\n--- Scan Report Summary ---"] + for i, report in enumerate(reports): + output.append(f"\n[{i+1}] Path: {report['path']}") + output.append(f" Target ID: {report['target_id']}") + output.append(f" Size: {report['size_bytes']:,} bytes") + output.append(f" Age: {report['age_days']} days") + return "\n".join(output) + +def handle_scan_command(args: Dict): + """ + Main entry point for handling the scan command, now incorporating + JSON output logic based on arguments. + """ + # Assume 'reports' is generated by a separate scanning function + mock_reports = [ + {'path': '/usr/bin/old_loader', 'target_id': 'TGT001', 'size_bytes': 5120, 'age_days': 365}, + {'path': '/tmp/.cache/appdata', 'target_id': 'TGT002', 'size_bytes': 409600, 'age_days': 7} + ] + + if args.get('json'): + print(format_scan_report(mock_reports)) + else: + # Standard output path (existing behavior) + print(format_scan_report(mock_reports))