Skip to main content

agx_cli/
validate.rs

1//! `agx validate` subcommand.
2
3use crate::output::{format_human, format_json};
4use crate::OutputFormat;
5use agx::preset::validate::{FileReport, ValidationReport};
6use std::path::PathBuf;
7
8/// Entry point for `agx validate`. Returns the process exit code (0, 1, or 2).
9///
10/// - 0: all files clean
11/// - 1: at least one file has errors
12/// - 2: no files given
13pub fn run_validate(paths: &[PathBuf], quiet: bool, format: OutputFormat) -> i32 {
14    if paths.is_empty() {
15        eprintln!("error: no preset files given");
16        return 2;
17    }
18
19    let file_reports: Vec<FileReport> = paths
20        .iter()
21        .map(|p| agx::preset::Preset::validate(p))
22        .collect();
23
24    let report = ValidationReport::from_files(file_reports);
25
26    let output = match format {
27        OutputFormat::Human => format_human(&report, quiet),
28        OutputFormat::Json => format_json(&report),
29    };
30    print!("{}", output);
31
32    if report.has_errors() {
33        1
34    } else {
35        0
36    }
37}