diff --git a/Cargo.lock b/Cargo.lock index b1ecc7e..6e2c842 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + [[package]] name = "arbitrary" version = "1.4.2" @@ -128,6 +134,45 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "clap_lex" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" + [[package]] name = "constant_time_eq" version = "0.3.1" @@ -304,6 +349,12 @@ version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hmac" version = "0.12.1" @@ -439,6 +490,7 @@ dependencies = [ name = "john_slop" version = "0.1.0" dependencies = [ + "clap", "miette", "regex", "tempfile", @@ -761,6 +813,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" diff --git a/Cargo.toml b/Cargo.toml index a51b4e7..5f41cae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ name = "john-slop-lsp" path = "src/bin/john-slop-lsp.rs" [dependencies] +clap = { version = "4.6.7", default-features = false, features = ["derive", "error-context", "help", "std", "suggestions", "usage"] } miette = { version = "7.6.0", features = ["fancy"] } regex = "1.12.2" thiserror = "2.0.17" diff --git a/README.md b/README.md index fa44238..892b7d1 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ Cargo exposes explicit `john-slop`, `john-slop-fmt`, and `john-slop-lsp` binary targets. The formatter and LSP currently exit with an explanatory unsupported message until their dedicated implementation milestones. +Run the detector over one or more paths with: + +```sh +cargo run --bin john-slop -- [--fail-confidence 0..100] ... +``` + +The detector exits `0` below the selected threshold, `1` when any document meets +it, and `2` for invalid arguments or operational failures. Confidence describes +configured evidence, not an AI-authorship probability. + Reusable consumers should call the library facade: `load_config`, `analyze_source`, `plan_replacements`, and `read_inputs`. Replacement planning is pure and shares the exact resolved configuration used by analysis. diff --git a/src/bin/john-slop.rs b/src/bin/john-slop.rs index 621fe01..6da6097 100644 --- a/src/bin/john-slop.rs +++ b/src/bin/john-slop.rs @@ -1,31 +1,103 @@ //! Detector command-line adapter. -use std::{env, path::Path}; +use std::{path::PathBuf, process::ExitCode}; +use clap::Parser; use john_slop::{analyze_source, load_config, read_inputs}; -use miette::{IntoDiagnostic, Result, WrapErr, miette}; +use miette::{IntoDiagnostic, Result, WrapErr}; -fn main() -> Result<()> { - let path = env::args_os() - .nth(1) - .ok_or_else(|| miette!("usage: john-slop "))?; +/// 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, +} + +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 { let config = load_config(); - let documents = read_inputs(&config, Path::new(&path)) - .into_diagnostic() - .wrap_err("failed to open requested input")?; - for document in documents { - let document = document + let mut threshold_reached = false; + + for path in &cli.paths { + let documents = read_inputs(&config, path) .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() + .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 { + let score = value + .parse::() + .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")] ); } - Ok(()) + #[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); + } } diff --git a/tasks.md b/tasks.md index e9054df..2c0fb05 100644 --- a/tasks.md +++ b/tasks.md @@ -59,7 +59,7 @@ are written down, and a clean baseline passes all quality gates. - [x] Add explicit `[[bin]]` targets for `john-slop`, `john-slop-fmt`, and `john-slop-lsp`; the latter two may initially call stub adapters but all three must compile. -- [ ] Keep binary parsing/orchestration out of the library. Add `clap` with derive +- [x] Keep binary parsing/orchestration out of the library. Add `clap` with derive for stable help, validation, and exit-code handling. Acceptance: integration tests can call the library without spawning a process; diff --git a/tests/cli.rs b/tests/cli.rs new file mode 100644 index 0000000..d010735 --- /dev/null +++ b/tests/cli.rs @@ -0,0 +1,52 @@ +use std::{fs, process::Command}; + +use tempfile::tempdir; + +fn detector() -> Command { + Command::new(env!("CARGO_BIN_EXE_john-slop")) +} + +#[test] +fn help_is_successful_and_names_the_command() { + let output = detector().arg("--help").output().expect("run detector"); + + assert!(output.status.success()); + assert!( + String::from_utf8(output.stdout) + .expect("UTF-8 stdout") + .contains("Usage: john-slop") + ); +} + +#[test] +fn invalid_confidence_is_a_usage_error() { + let output = detector() + .args(["--fail-confidence", "101", "file.txt"]) + .output() + .expect("run detector"); + + assert_eq!(output.status.code(), Some(2)); +} + +#[test] +fn findings_control_the_threshold_exit_code() { + let directory = tempdir().expect("temp dir"); + let clean = directory.path().join("clean.txt"); + let suspicious = directory.path().join("suspicious.txt"); + fs::write(&clean, "ordinary-word").expect("write clean fixture"); + fs::write(&suspicious, "suspicious—dash").expect("write suspicious fixture"); + + let clean_output = detector() + .args(["--fail-confidence", "10"]) + .arg(&clean) + .output() + .expect("run clean detector"); + let suspicious_output = detector() + .args(["--fail-confidence", "10"]) + .arg(&suspicious) + .output() + .expect("run suspicious detector"); + + assert_eq!(clean_output.status.code(), Some(0)); + assert_eq!(suspicious_output.status.code(), Some(1)); +}