feat: inspect resolved configuration

This commit is contained in:
Dandaedre 2026-09-29 13:30:00 +02:00
parent 72fe28a94c
commit 7bb9781064
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
6 changed files with 203 additions and 18 deletions

67
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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<ConfigName>,
/// Load an explicit TOML configuration after user configuration.
#[arg(long, value_name = "FILE")]
#[arg(long, global = true, value_name = "FILE")]
config: Option<PathBuf>,
/// 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<u64>,
/// 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<PathBuf>,
/// Inspect resolved configuration instead of analyzing documents.
#[command(subcommand)]
command: Option<Command>,
}
/// 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<bool> {
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<bool> {
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 = \"<embedded:assets/config.toml>\"");
println!("embedded_heuristics = \"<embedded:assets/heuristics.toml>\"");
println!("user_base = {:?}", paths.base.display().to_string());
println!(
"user_named = {:?}",
paths.named.as_ref().map_or_else(
|| "<not selected>".to_owned(),
|path| path.display().to_string()
)
);
println!(
"explicit = {:?}",
paths.explicit.as_ref().map_or_else(
|| "<not selected>".to_owned(),
|path| path.display().to_string()
)
);
println!("command_line_overrides = \"<current invocation>\"");
}
/// Parse and validate an inclusive confidence threshold.
fn parse_confidence(value: &str) -> Result<u8, String> {
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
})
));
}
}

View File

@ -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.

View File

@ -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::<toml::Value>(&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("<embedded:assets/config.toml>"));
assert!(stdout.contains("config.toml"));
assert!(stdout.contains("<not selected>"));
}

View File

@ -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] <PATH>...
john-slop <COMMAND>
Commands:
config Inspect configuration sources and resolved values
help Print this message or the help of the given subcommand(s)
Arguments:
<PATH>... Files, directories, or supported ZIP-based containers to inspect