feat(xatu): set CLI exit statuses

This commit is contained in:
2026-09-19 16:58:27 -04:00
parent 58f2350d5f
commit f6474315f8

View File

@@ -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<Vec<SourceFile>> {
collect_source_files_with_exclusions(path, false)
}
fn collect_source_files_with_exclusions(
path: &Path,
exclude_current_dir: bool,
) -> status::Result<Vec<SourceFile>> {
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<Vec<SourceFile>> {
{
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<PathBuf>,
}
fn main() -> status::Result<()> {
let args = Args::parse();
fn analyze(args: Args) -> status::Result<Report> {
let paths: Vec<PathBuf> = 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)
}
}
}