Skip to main content

json_output/
json_output.rs

1#!/usr/bin/env cargo
2//! JSON output example for programmatic consumption
3//! 
4//! Run with: cargo run --example json_output
5
6use execheck::{analyze_file, print_report, OutputFormat, SecurityReport, ReportSummary};
7use std::path::PathBuf;
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Analyze multiple files
11    let files = vec![
12        PathBuf::from("/bin/ls"),
13        PathBuf::from("/bin/cat"),
14        PathBuf::from("/bin/echo"),
15    ];
16    
17    let mut results = Vec::new();
18    let mut secure_count = 0;
19    let mut insecure_count = 0;
20    let mut unsupported_count = 0;
21    
22    println!("Analyzing {} files...", files.len());
23    
24    for file_path in files {
25        match analyze_file(&file_path) {
26            Ok(result) => {
27                match result.overall_status.as_str() {
28                    "Secure" => secure_count += 1,
29                    "Mostly Secure" | "Insecure" => insecure_count += 1,
30                    _ => unsupported_count += 1,
31                }
32                results.push(result);
33            }
34            Err(e) => {
35                eprintln!("Warning: Failed to analyze {}: {}", file_path.display(), e);
36                unsupported_count += 1;
37            }
38        }
39    }
40    
41    // Create report
42    let report = SecurityReport {
43        files: results,
44        summary: ReportSummary {
45            total_files: secure_count + insecure_count + unsupported_count,
46            secure_files: secure_count,
47            insecure_files: insecure_count,
48            unsupported_files: unsupported_count,
49        },
50    };
51    
52    println!("\n=== JSON Output ===");
53    print_report(&report, &OutputFormat::Json, None)?;
54    
55    Ok(())
56}