Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions contributions/cli_json_output_for_scan_repor.py
Original file line number Diff line number Diff line change
@@ -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))