From 7bb97810649d456e1e1ea03443bda4a556425c27 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Sep 2026 13:30:00 +0200 Subject: [PATCH] feat: inspect resolved configuration --- Cargo.lock | 67 +++++++++++-- Cargo.toml | 1 + src/bin/john-slop.rs | 98 +++++++++++++++++-- tasks.md | 2 +- tests/cli.rs | 48 ++++++++- ...p_is_successful_and_names_the_command.snap | 5 + 6 files changed, 203 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ce525f..3d887a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -327,7 +327,7 @@ checksum = "8cb01cd46b0cf372153850f4c6c272d9cbea2da513e07538405148f95bd789f3" dependencies = [ "atomic", "serde", - "toml", + "toml 0.8.23", "uncased", "version_check", ] @@ -566,6 +566,7 @@ dependencies = [ "serde", "tempfile", "thiserror", + "toml 1.1.6+spec-1.1.0", "walkdir", "zip", ] @@ -888,6 +889,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +dependencies = [ + "serde_core", +] + [[package]] name = "sha1" version = "0.11.0" @@ -1097,11 +1107,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" dependencies = [ "serde", - "serde_spanned", - "toml_datetime", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", "toml_edit", ] +[[package]] +name = "toml" +version = "1.1.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "920602543f0911ab71da12c50d59701da54c196d1a2bf5cb4b75667f137a406a" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned 1.1.1", + "toml_datetime 1.1.1+spec-1.1.0", + "toml_parser", + "toml_writer", + "winnow 1.0.4", +] + [[package]] name = "toml_datetime" version = "0.6.11" @@ -1111,6 +1136,15 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_datetime" +version = "1.1.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7" +dependencies = [ + "serde_core", +] + [[package]] name = "toml_edit" version = "0.22.27" @@ -1119,10 +1153,19 @@ checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" dependencies = [ "indexmap", "serde", - "serde_spanned", - "toml_datetime", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", "toml_write", - "winnow", + "winnow 0.7.15", +] + +[[package]] +name = "toml_parser" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" +dependencies = [ + "winnow 1.0.4", ] [[package]] @@ -1131,6 +1174,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" +[[package]] +name = "toml_writer" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" + [[package]] name = "typed-path" version = "0.12.3" @@ -1270,6 +1319,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" + [[package]] name = "writeable" version = "0.6.4" diff --git a/Cargo.toml b/Cargo.toml index c58f18f..347d1b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ miette = { version = "7.6.0", default-features = false, features = ["derive"] } regex = "1.13.1" serde = { version = "1.0.229", features = ["derive"] } thiserror = "2.0.21" +toml = "1.1.6" walkdir = "2.5.0" zip = "8.6.0" diff --git a/src/bin/john-slop.rs b/src/bin/john-slop.rs index 0886bbf..e1d81ec 100644 --- a/src/bin/john-slop.rs +++ b/src/bin/john-slop.rs @@ -2,7 +2,7 @@ use std::{path::PathBuf, process::ExitCode}; -use clap::Parser; +use clap::{Parser, Subcommand}; use john_slop::{ ConfigLoader, ConfigName, ConfigOverrides, InputOverrides, analyze_source, read_inputs, }; @@ -10,18 +10,25 @@ 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)] +#[command( + name = "john-slop", + version, + about, + subcommand_negates_reqs = true, + subcommand_precedence_over_arg = true, + args_conflicts_with_subcommands = true +)] struct Cli { /// Load a named user configuration from the platform config directory. - #[arg(long, value_name = "NAME")] + #[arg(long, global = true, value_name = "NAME")] config_name: Option, /// Load an explicit TOML configuration after user configuration. - #[arg(long, value_name = "FILE")] + #[arg(long, global = true, value_name = "FILE")] config: Option, /// Override the maximum accepted bytes for one input document. - #[arg(long, value_name = "BYTES")] + #[arg(long, global = true, value_name = "BYTES")] max_document_bytes: Option, /// Return exit code 1 when a document reaches this evidence-confidence score. @@ -31,6 +38,30 @@ struct Cli { /// Files, directories, or supported ZIP-based containers to inspect. #[arg(required = true, value_name = "PATH")] paths: Vec, + + /// Inspect resolved configuration instead of analyzing documents. + #[command(subcommand)] + command: Option, +} + +/// Top-level utility commands. +#[derive(Debug, Subcommand)] +enum Command { + /// Inspect configuration sources and resolved values. + Config { + /// Configuration inspection operation. + #[command(subcommand)] + command: ConfigCommand, + }, +} + +/// Configuration inspection operations. +#[derive(Debug, Subcommand)] +enum ConfigCommand { + /// Print the fully resolved, validated configuration as deterministic TOML. + Show, + /// Print every selected configuration path in precedence order. + Paths, } fn main() -> ExitCode { @@ -47,16 +78,26 @@ fn main() -> ExitCode { /// Execute one parsed detector invocation and report whether its threshold fired. fn run(cli: &Cli) -> Result { - let config = ConfigLoader::discover(cli.config_name.as_ref(), cli.config.clone()) - .into_diagnostic()? + let loader = ConfigLoader::discover(cli.config_name.as_ref(), cli.config.clone())? .with_overrides(ConfigOverrides { input: InputOverrides { max_document_bytes: cli.max_document_bytes, }, ..ConfigOverrides::default() - }) - .load() - .into_diagnostic()?; + }); + + if let Some(Command::Config { command }) = &cli.command { + match command { + ConfigCommand::Show => { + let config = loader.load()?; + print!("{}", toml::to_string_pretty(&config).into_diagnostic()?); + } + ConfigCommand::Paths => print_config_paths(&loader), + } + return Ok(false); + } + + let config = loader.load()?; let mut threshold_reached = false; for path in &cli.paths { @@ -81,6 +122,29 @@ fn run(cli: &Cli) -> Result { Ok(threshold_reached) } +/// Print selected layers without reading or validating their contents. +fn print_config_paths(loader: &ConfigLoader) { + let paths = loader.paths(); + println!("embedded_config = \"\""); + println!("embedded_heuristics = \"\""); + println!("user_base = {:?}", paths.base.display().to_string()); + println!( + "user_named = {:?}", + paths.named.as_ref().map_or_else( + || "".to_owned(), + |path| path.display().to_string() + ) + ); + println!( + "explicit = {:?}", + paths.explicit.as_ref().map_or_else( + || "".to_owned(), + |path| path.display().to_string() + ) + ); + println!("command_line_overrides = \"\""); +} + /// Parse and validate an inclusive confidence threshold. fn parse_confidence(value: &str) -> Result { let score = value @@ -104,6 +168,7 @@ mod tests { assert_eq!(cli.fail_confidence, 42); assert_eq!(cli.config_name, None); + assert!(cli.command.is_none()); assert_eq!( cli.paths, [PathBuf::from("one.txt"), PathBuf::from("two.txt")] @@ -124,4 +189,17 @@ mod tests { assert_eq!(error.exit_code(), 2); } + + #[test] + fn parses_configuration_subcommands_without_detector_paths() { + let cli = Cli::try_parse_from(["john-slop", "config", "show"]).unwrap(); + + assert!(cli.paths.is_empty()); + assert!(matches!( + cli.command, + Some(Command::Config { + command: ConfigCommand::Show + }) + )); + } } diff --git a/tasks.md b/tasks.md index e1d7a22..9735e6a 100644 --- a/tasks.md +++ b/tasks.md @@ -97,7 +97,7 @@ entry do not discard successful documents. ordered thresholds, safe matchers, and edit conflicts. - [x] Preserve enough provider metadata to identify the offending file and key in a `miette` configuration diagnostic. -- [ ] Add `john-slop config show` and `john-slop config paths` so precedence is +- [x] Add `john-slop config show` and `john-slop config paths` so precedence is inspectable; output deterministic TOML or JSON. - [ ] Test every layer alone and in combination, including missing optional files, malformed TOML, unknown fields, named configs, array semantics, and CLI wins. diff --git a/tests/cli.rs b/tests/cli.rs index 798b4bd..a09a203 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -10,7 +10,11 @@ fn detector() -> Command { fn help_is_successful_and_names_the_command() { let output = detector().arg("--help").output().expect("run detector"); - assert!(output.status.success()); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); insta::assert_snapshot!(String::from_utf8(output.stdout).expect("UTF-8 stdout")); } @@ -47,3 +51,45 @@ fn findings_control_the_threshold_exit_code() { assert_eq!(clean_output.status.code(), Some(0)); assert_eq!(suspicious_output.status.code(), Some(1)); } + +#[test] +fn config_show_emits_resolved_toml_with_explicit_overrides() { + let directory = tempdir().expect("temp dir"); + let config = directory.path().join("project.toml"); + fs::write(&config, "[thresholds]\nlow = 23\n").expect("write config"); + + let output = detector() + .env("XDG_CONFIG_HOME", directory.path()) + .args(["config", "show"]) + .arg("--config") + .arg(&config) + .output() + .expect("run config show"); + + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let stdout = String::from_utf8(output.stdout).expect("UTF-8 stdout"); + let resolved = toml::from_str::(&stdout).expect("valid TOML"); + assert_eq!(resolved["schema_version"].as_integer(), Some(1)); + assert_eq!(resolved["thresholds"]["low"].as_integer(), Some(23)); + assert_eq!(resolved["replacements"].as_array().unwrap().len(), 10); +} + +#[test] +fn config_paths_lists_embedded_and_platform_sources() { + let directory = tempdir().expect("temp dir"); + let output = detector() + .env("XDG_CONFIG_HOME", directory.path()) + .args(["config", "paths"]) + .output() + .expect("run config paths"); + + assert!(output.status.success()); + let stdout = String::from_utf8(output.stdout).expect("UTF-8 stdout"); + assert!(stdout.contains("")); + assert!(stdout.contains("config.toml")); + assert!(stdout.contains("")); +} diff --git a/tests/snapshots/cli__help_is_successful_and_names_the_command.snap b/tests/snapshots/cli__help_is_successful_and_names_the_command.snap index 1f7d119..b2ef9a5 100644 --- a/tests/snapshots/cli__help_is_successful_and_names_the_command.snap +++ b/tests/snapshots/cli__help_is_successful_and_names_the_command.snap @@ -6,6 +6,11 @@ expression: "String::from_utf8(output.stdout).expect(\"UTF-8 stdout\")" Inspect files, directories, or ZIP-based documents for low-care signals Usage: john-slop [OPTIONS] ... + john-slop + +Commands: + config Inspect configuration sources and resolved values + help Print this message or the help of the given subcommand(s) Arguments: ... Files, directories, or supported ZIP-based containers to inspect