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/report.rs | 120 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 crates/config/src/report.rs (limited to 'crates/config/src/report.rs') diff --git a/crates/config/src/report.rs b/crates/config/src/report.rs new file mode 100644 index 0000000..dfbaa23 --- /dev/null +++ b/crates/config/src/report.rs @@ -0,0 +1,120 @@ +//! The report of a configuration: what each option ended up as and where its value came +//! from, for a program to print (`--print-config`) or to serve. + +use std::collections::BTreeMap; +use std::path::PathBuf; + +use serde_json::Value as JsonValue; + +use crate::layer::{Origin, Provenance}; +use crate::option::Schema; + +/// How a report shows text with placeholders. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub enum Values { + /// As it ends up. + #[default] + Interpolated, + /// As written, placeholders and all. + Original, +} + +/// The reports of a configuration, either way of showing text with placeholders: made +/// once, at a program's start, for it to serve. +#[derive(Debug, Clone, Default)] +pub struct Reports { + interpolated: JsonValue, + original: JsonValue, +} + +impl Reports { + /// Both reports (`report`). + pub fn of( + schema: &Schema, + settings: &serde_json::Map, + origins: &BTreeMap, + files: &[PathBuf], + ) -> Reports { + Reports { + interpolated: report(schema, settings, origins, files, Values::Interpolated), + original: report(schema, settings, origins, files, Values::Original), + } + } + + pub fn get(&self, values: Values) -> &JsonValue { + match values { + Values::Interpolated => &self.interpolated, + Values::Original => &self.original, + } + } +} + +/// The effective configuration with each value's source, as JSON: `files`, those read, +/// and `options`, each of `schema`'s by name, with its `value` (of `settings`, the +/// settings in effect by option name; `null` where they hold none), its `source` +/// (`default`, `file` with the `file`, or `argument`), and where they apply `imported`, +/// `templates`, `unset` and `secret`. Imported values are left out (they may be secret); +/// their importers are listed instead. Secrets are left out however they were given. +/// Text with placeholders is shown as `values` says, the text as written listed. Hidden +/// options are left out unless set. +pub fn report( + schema: &Schema, + settings: &serde_json::Map, + origins: &BTreeMap, + files: &[PathBuf], + values: Values, +) -> JsonValue { + let options: serde_json::Map = schema + .options + .iter() + .filter(|spec| !spec.hidden || origins.contains_key(spec.name)) + .map(|spec| { + let mut entry = serde_json::Map::new(); + let origin = origins.get(spec.name); + match origin { + None => { + entry.insert("source".into(), "default".into()); + } + Some(origin) => { + match &origin.provenance { + Provenance::File(path) => { + entry.insert("source".into(), "file".into()); + entry.insert("file".into(), path.display().to_string().into()); + } + Provenance::Argument => { + entry.insert("source".into(), "argument".into()); + } + } + if origin.is_imported() { + entry.insert("imported".into(), origin.imports.clone().into()); + } + // Of a secret, the text may hold some of it. + if !origin.templates.is_empty() && !spec.secret { + entry.insert("templates".into(), origin.templates.clone().into()); + } + if origin.unset { + entry.insert("unset".into(), true.into()); + } + } + } + let given = origin.is_some_and(|origin| !origin.unset); + if spec.secret && given { + entry.insert("secret".into(), true.into()); + } else if !origin.is_some_and(Origin::is_imported) { + let written = origin.and_then(|origin| origin.written.clone()); + let value = match (values, written) { + (Values::Original, Some(written)) => written, + _ => settings.get(spec.name).cloned().unwrap_or(JsonValue::Null), + }; + entry.insert("value".into(), value); + } + (spec.name.to_owned(), JsonValue::Object(entry)) + }) + .collect(); + serde_json::json!({ + "files": files.iter().map(|path| path.display().to_string()).collect::>(), + "options": options, + }) +} -- cgit v1.3.1