104 lines
3.1 KiB
Rust
104 lines
3.1 KiB
Rust
//! Detector command-line adapter.
|
|
|
|
use std::{path::PathBuf, process::ExitCode};
|
|
|
|
use clap::Parser;
|
|
use john_slop::{analyze_source, load_config, read_inputs};
|
|
use miette::{IntoDiagnostic, Result, WrapErr};
|
|
|
|
/// Inspect files, directories, or ZIP-based documents for low-care signals.
|
|
#[derive(Debug, Parser)]
|
|
#[command(name = "john-slop", version, about)]
|
|
struct Cli {
|
|
/// Return exit code 1 when a document reaches this evidence-confidence score.
|
|
#[arg(long, default_value_t = 100, value_parser = parse_confidence)]
|
|
fail_confidence: u8,
|
|
|
|
/// Files, directories, or supported ZIP-based containers to inspect.
|
|
#[arg(required = true, value_name = "PATH")]
|
|
paths: Vec<PathBuf>,
|
|
}
|
|
|
|
fn main() -> ExitCode {
|
|
let cli = Cli::parse();
|
|
match run(&cli) {
|
|
Ok(true) => ExitCode::from(1),
|
|
Ok(false) => ExitCode::SUCCESS,
|
|
Err(error) => {
|
|
eprintln!("{error:?}");
|
|
ExitCode::from(2)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Execute one parsed detector invocation and report whether its threshold fired.
|
|
fn run(cli: &Cli) -> Result<bool> {
|
|
let config = load_config();
|
|
let mut threshold_reached = false;
|
|
|
|
for path in &cli.paths {
|
|
let documents = read_inputs(&config, path)
|
|
.into_diagnostic()
|
|
.wrap_err_with(|| format!("failed to open requested input `{}`", path.display()))?;
|
|
for document in documents {
|
|
let document = document
|
|
.into_diagnostic()
|
|
.wrap_err("failed to read an input document")?;
|
|
let analysis = analyze_source(&config, &document.content);
|
|
println!(
|
|
"{}: confidence {}, {} finding(s)",
|
|
document.source,
|
|
analysis.confidence.score(),
|
|
analysis.findings.len()
|
|
);
|
|
threshold_reached |= analysis.confidence.score() >= cli.fail_confidence;
|
|
}
|
|
}
|
|
|
|
Ok(threshold_reached)
|
|
}
|
|
|
|
/// Parse and validate an inclusive confidence threshold.
|
|
fn parse_confidence(value: &str) -> Result<u8, String> {
|
|
let score = value
|
|
.parse::<u8>()
|
|
.map_err(|_| "confidence must be an integer from 0 through 100".to_owned())?;
|
|
if score > 100 {
|
|
return Err("confidence must be an integer from 0 through 100".to_owned());
|
|
}
|
|
Ok(score)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn parses_multiple_paths_and_threshold() {
|
|
let cli =
|
|
Cli::try_parse_from(["john-slop", "--fail-confidence", "42", "one.txt", "two.txt"])
|
|
.expect("valid CLI");
|
|
|
|
assert_eq!(cli.fail_confidence, 42);
|
|
assert_eq!(
|
|
cli.paths,
|
|
[PathBuf::from("one.txt"), PathBuf::from("two.txt")]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_invalid_threshold() {
|
|
let error = Cli::try_parse_from(["john-slop", "--fail-confidence", "101", "file.txt"])
|
|
.expect_err("threshold above 100 should fail");
|
|
|
|
assert_eq!(error.exit_code(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn requires_at_least_one_path() {
|
|
let error = Cli::try_parse_from(["john-slop"]).expect_err("path should be required");
|
|
|
|
assert_eq!(error.exit_code(), 2);
|
|
}
|
|
}
|