aboutsummaryrefslogtreecommitdiffstats
path: root/crates/config/src/arguments.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 01:37:39 +0000
committertv <tv@krebsco.de>2026-11-29 01:37:39 +0000
commitbcb729ce0cae2cd8bbd8f60f8e98a81674bcd4fb (patch)
tree7ed9a9bdfd0b55061cc701585e550957563f5597 /crates/config/src/arguments.rs
parentfd0e8b29a86444b772f71d5db767c201b04cad2b (diff)
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.
Diffstat (limited to 'crates/config/src/arguments.rs')
-rw-r--r--crates/config/src/arguments.rs56
1 files changed, 56 insertions, 0 deletions
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")), "<secret>");
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<String, JsonValue> = 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",