diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/mod.rs | 115 |
1 files changed, 22 insertions, 93 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs index a32211e..efc190d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -32,6 +32,7 @@ pub(crate) use ::config::arguments::{merged, with_options}; pub(crate) use ::config::layer::read; pub(crate) use ::config::layer::{Layer, Origin, Provenance}; pub(crate) use ::config::option::{Kind, OptionSpec, Schema, option}; +pub(crate) use ::config::report::{Reports, Values}; /// What the node's configuration is read by: its options, those that can't be combined, /// and those that are gone. @@ -1541,19 +1542,11 @@ const CONFLICTS: &[(&[&str], &[&str])] = &[( // --- command line -------------------------------------------------------------- -const PRINT_CONFIG: &str = "print-config"; - fn command() -> Command { let command = Command::new("continuum") .version(env!("CARGO_PKG_VERSION")) .long_version(crate::release::described()) .about("continuum: a replicated Automerge document over Mycelium, libp2p or Tor") - .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."), - ) .subcommand( Command::new(NODE_ID) .about( @@ -1568,7 +1561,7 @@ fn command() -> Command { .help("Make a new key in the file where there is none, as node-key-create"), ), ); - with_options(command, OPTIONS) + with_options(::config::arguments::with_print_config(command), OPTIONS) } /// `continuum node-id FILE`: the node ID of the key in `FILE` (`Loaded::node_id`). @@ -1615,100 +1608,36 @@ impl Loaded { } } -/// How `GET /admin/config` shows text with placeholders (`?values=`). -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)] -#[serde(rename_all = "camelCase")] -pub(crate) enum Values { - /// As it ends up. - #[default] - Interpolated, - /// As written, placeholders and all. - Original, -} - -/// The configuration reports, made once at start for `GET /admin/config`. -#[derive(Debug, Clone, Default)] -pub(crate) struct Reports { - interpolated: JsonValue, - original: JsonValue, -} - -impl Reports { - pub(crate) fn get(&self, values: Values) -> &JsonValue { - match values { - Values::Interpolated => &self.interpolated, - Values::Original => &self.original, - } - } -} - impl Loaded { /// The effective configuration with each value's source, for `--print-config` and /// `GET /admin/config`. 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. pub(crate) fn report(&self, values: Values) -> JsonValue { - let settings = match serde_json::to_value(&self.settings) { + ::config::report::report( + &SCHEMA, + &self.settings_by_option(), + &self.origins, + &self.files, + values, + ) + } + + /// The settings in effect, by option name. + fn settings_by_option(&self) -> serde_json::Map<String, JsonValue> { + match serde_json::to_value(&self.settings) { Ok(JsonValue::Object(settings)) => settings, _ => serde_json::Map::new(), - }; - let options: serde_json::Map<String, JsonValue> = OPTIONS - .iter() - .filter(|spec| !spec.hidden || self.origins.contains_key(spec.name)) - .map(|spec| { - let mut entry = serde_json::Map::new(); - let origin = self.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": self.files.iter().map(|path| path.display().to_string()).collect::<Vec<_>>(), - "options": options, - }) + } } pub(crate) fn reports(&self) -> Reports { - Reports { - interpolated: self.report(Values::Interpolated), - original: self.report(Values::Original), - } + Reports::of( + &SCHEMA, + &self.settings_by_option(), + &self.origins, + &self.files, + ) } } @@ -1717,7 +1646,7 @@ impl Loaded { pub(crate) fn load() -> Result<Loaded> { ::config::arguments::refuse_gone(std::env::args().skip(1), &SCHEMA)?; let matches = command().get_matches(); - let print_only = matches.get_flag(PRINT_CONFIG); + let print_only = ::config::arguments::prints_config(&matches); let node_id = matches.subcommand_matches(NODE_ID).map(|sub| { let file = sub.get_one::<String>("file").expect("required"); (PathBuf::from(file), sub.get_flag("create")) |
