use std::path::Path;
use sokf_core::{CacheMode, Expr, ValidateOptions};
use crate::{cli::ValidateArgs, output, render};
pub fn validate(
repo_root: Option<&Path>,
cache: CacheMode,
args: ValidateArgs,
) -> sokf_core::Result<u8> {
let (cwd, root) = output::root(repo_root)?;
let opts = ValidateOptions {
paths: args.paths,
cwd,
fix: args.fix,
cache,
query: args.query.as_deref().map(Expr::compile).transpose()?,
changed: args.changed,
home: std::env::home_dir(),
};
let (report, projection) = sokf_core::validate(&root, &opts)?;
let buf = if let Some(projection) = &projection {
output::json_line(projection)?
} else if args.json {
output::json_line(&report)?
} else {
let mut buf = Vec::new();
render::text(&mut buf, &report, args.warnings, args.fix).map_err(output::stdout_err)?;
buf
};
output::write_stdout(&buf)?;
Ok(if report.has_errors() {
output::EXIT_ERRORS
} else {
output::EXIT_OK
})
}