xbp_cli/commands/
fix_process_monitor_json.rs1use crate::utils::{fix_cursor_process_monitor_json, fix_cursor_process_monitor_json_file};
2use colored::Colorize;
3use std::fs;
4use std::path::PathBuf;
5
6pub fn run_fix_process_monitor_json(
7 path: PathBuf,
8 check: bool,
9 stdout: bool,
10) -> Result<(), String> {
11 if check {
12 let content = fs::read_to_string(&path)
13 .map_err(|error| format!("Failed to read {}: {error}", path.display()))?;
14 let (_, report) = fix_cursor_process_monitor_json(&content)?;
15
16 if report.is_some() {
17 println!("{} {} needs repair", "fix".yellow().bold(), path.display());
18 } else {
19 println!("{} {}", "ok".green().bold(), path.display());
20 }
21
22 return Ok(());
23 }
24
25 if stdout {
26 let content = fs::read_to_string(&path)
27 .map_err(|error| format!("Failed to read {}: {error}", path.display()))?;
28 let (fixed, report) = fix_cursor_process_monitor_json(&content)?;
29 print!("{fixed}");
30
31 if let Some(report) = report {
32 eprintln!(
33 "{} repaired {} samples from {}",
34 "fix".green().bold(),
35 report.sample_count,
36 path.display()
37 );
38 }
39
40 return Ok(());
41 }
42
43 match fix_cursor_process_monitor_json_file(&path)? {
44 Some(report) => {
45 println!(
46 "{} {} ({} samples)",
47 "repaired".green().bold(),
48 path.display(),
49 report.sample_count
50 );
51 }
52 None => {
53 println!("{} {}", "ok".green().bold(), path.display());
54 }
55 }
56
57 Ok(())
58}