From f6474315f81d606f8304bed308599780e8b252a9 Mon Sep 17 00:00:00 2001 From: alexion Date: Sat, 19 Sep 2026 16:58:27 -0400 Subject: [PATCH] feat(xatu): set CLI exit statuses --- src/xatu/xatu_main.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/xatu/xatu_main.rs b/src/xatu/xatu_main.rs index 5c50fe8..d7e4474 100644 --- a/src/xatu/xatu_main.rs +++ b/src/xatu/xatu_main.rs @@ -1,7 +1,8 @@ use clap::Parser; use status::ResultExt; use std::path::{Path, PathBuf}; -use xatu::SourceFile; +use std::process::ExitCode; +use xatu::{Report, SourceFile}; fn is_nix_file(path: &Path) -> bool { path.extension().is_some_and(|extension| extension == "nix") @@ -28,11 +29,18 @@ fn is_excluded_dir(path: &Path) -> bool { } fn collect_source_files(path: &Path) -> status::Result> { + collect_source_files_with_exclusions(path, false) +} + +fn collect_source_files_with_exclusions( + path: &Path, + exclude_current_dir: bool, +) -> status::Result> { let metadata = std::fs::symlink_metadata(path) .with_context(|| format!("failed to read metadata for {}", path.display()))?; if metadata.file_type().is_dir() { - if is_excluded_dir(path) { + if exclude_current_dir && is_excluded_dir(path) { return Ok(Vec::new()); } @@ -43,7 +51,7 @@ fn collect_source_files(path: &Path) -> status::Result> { { let entry = entry.with_context(|| format!("failed to read entry in {}", path.display()))?; - files.extend(collect_source_files(&entry.path())?); + files.extend(collect_source_files_with_exclusions(&entry.path(), true)?); } return Ok(files); @@ -66,9 +74,7 @@ struct Args { paths: Vec, } -fn main() -> status::Result<()> { - let args = Args::parse(); - +fn analyze(args: Args) -> status::Result { let paths: Vec = args .paths .into_iter() @@ -83,8 +89,22 @@ fn main() -> status::Result<()> { .flatten() .collect(); - let report = xatu::future_sight(files)?; - print!("{report}"); - - Ok(()) + xatu::future_sight(files) +} + +fn main() -> ExitCode { + match analyze(Args::parse()) { + Ok(report) if report.is_clean() => { + println!("No findings."); + ExitCode::SUCCESS + } + Ok(report) => { + print!("{report}"); + ExitCode::from(1) + } + Err(error) => { + eprintln!("{error}"); + ExitCode::from(2) + } + } }