From bcb729ce0cae2cd8bbd8f60f8e98a81674bcd4fb Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 01:37:39 +0000 Subject: config: the report of a configuration, and --print-config The report moves to the crate, as it was (report::report, Reports, Values): each option of a schema with its value, by the settings in effect given as JSON by option name, and where it came from; imported values and secrets left out. And the argument a program prints it by (arguments::with_print_config, prints_config). continuum gives its settings and what it loaded to it; --print-config and GET /admin/config say what they said. --- crates/config/src/arguments.rs | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'crates/config/src/arguments.rs') diff --git a/crates/config/src/arguments.rs b/crates/config/src/arguments.rs index 5f51b2b..4a251da 100644 --- a/crates/config/src/arguments.rs +++ b/crates/config/src/arguments.rs @@ -14,6 +14,24 @@ use crate::layer::{Entry, Layer, Overlay, Provenance, Sources, from_import, over use crate::option::{Kind, OptionSpec, Schema, from_text}; pub const CONFIG: &str = "config"; +/// Printing the report of the configuration and exiting (`with_print_config`). +pub const PRINT_CONFIG: &str = "print-config"; + +/// `command` taking `--print-config`: a program given it prints its configuration's +/// report (`report::report`) and exits (`prints_config`). +pub fn with_print_config(command: Command) -> Command { + command.arg( + Arg::new(PRINT_CONFIG) + .long(PRINT_CONFIG) + .action(ArgAction::SetTrue) + .help("Print the effective configuration as JSON (with each value's source) and exit."), + ) +} + +/// Whether `--print-config` was given. +pub fn prints_config(matches: &ArgMatches) -> bool { + matches.get_flag(PRINT_CONFIG) +} /// Stops on an argument naming an option that is gone, saying why, before the command /// line is read: clap would call it unexpected. @@ -211,6 +229,44 @@ mod tests { assert_eq!(layer["token"].value, json!("hush")); assert_eq!(layer["token"].shown(SCHEMA.secret("token")), ""); assert!(layer["name"].origin.to_string().ends_with("main.json")); + // The report: each option's value and where it is from, a secret's left out. + let settings: serde_json::Map = layer + .iter() + .map(|(name, entry)| (name.clone(), entry.value.clone())) + .collect(); + let origins = layer + .iter() + .map(|(name, entry)| (name.clone(), entry.origin.clone())) + .collect(); + let files = [scratch.0.join("main.json")]; + let report = crate::report::report( + &SCHEMA, + &settings, + &origins, + &files, + crate::report::Values::Interpolated, + ); + assert_eq!(report["files"], json!([path("main.json")])); + let options = &report["options"]; + assert_eq!(options["name"]["value"], json!("main")); + assert_eq!(options["name"]["source"], json!("file")); + assert!( + options["name"]["file"] + .as_str() + .is_some_and(|file| file.ends_with("main.json")) + ); + assert_eq!( + options["remote"], + json!({ "source": "default", "value": null }) + ); + assert_eq!(options["token"]["secret"], json!(true)); + assert!(options["token"].get("value").is_none()); + assert!(!report.to_string().contains("hush")); + let command = with_print_config(with_options(Command::new("program"), OPTIONS)); + let matches = command + .try_get_matches_from(["program", "--print-config"]) + .unwrap(); + assert!(prints_config(&matches)); // Arguments over files, and an unset back to the default. let layer = merged_of(&[ "--config", -- cgit v1.3.1