diff options
Diffstat (limited to 'src/config/mod.rs')
| -rw-r--r-- | src/config/mod.rs | 124 |
1 files changed, 105 insertions, 19 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs index 142dc01..533b44c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -65,7 +65,7 @@ impl OptionSpec { } } -const fn option( +pub(crate) const fn option( name: &'static str, kind: Kind, value_name: &'static str, @@ -718,7 +718,59 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ ]; pub(crate) fn spec(name: &str) -> Option<&'static OptionSpec> { - OPTIONS.iter().find(|spec| spec.name == name) + spec_in(OPTIONS, name) +} + +/// The option of `options` named `name`. +pub(super) fn spec_in(options: &'static [OptionSpec], name: &str) -> Option<&'static OptionSpec> { + options.iter().find(|spec| spec.name == name) +} + +/// The options of a party's client (`continuum-party`), set as the node's are: on the +/// command line, or in configuration files of the same format (`with_options`, +/// `merged`). +#[cfg(feature = "federation")] +pub(crate) const PARTY_OPTIONS: &[OptionSpec] = &[ + option( + "node", + Kind::Text, + "URL", + Some("http://localhost:3000"), + "The node whose API it puts events through; its user and password in the URL", + ) + .secret(), + option( + "token", + Kind::Text, + "TOKEN", + None, + "A token the node issued, instead of a password", + ) + .secret(), + option( + "key", + Kind::Text, + "PATH", + None, + "The party's key it signs with: an OpenSSH private key file, unencrypted", + ), + option( + "record", + Kind::Text, + "PATH", + None, + "The party's record of its votes, written before each vote is sent", + ), +]; +#[cfg(not(feature = "federation"))] +const PARTY_OPTIONS: &[OptionSpec] = &[]; + +/// Whether `option`, the node's or a party's client's, is never shown. +fn secret(option: &str) -> bool { + OPTIONS + .iter() + .chain(PARTY_OPTIONS) + .any(|spec| spec.name == option && spec.secret) } /// The effective configuration. @@ -1511,7 +1563,7 @@ impl Entry { pub(crate) fn shown(&self, option: &str) -> String { if self.origin.unset { "<default>".to_owned() - } else if spec(option).is_some_and(|spec| spec.secret) { + } else if secret(option) { "<secret>".to_owned() } else if self.origin.is_imported() { "<imported>".to_owned() @@ -1798,9 +1850,22 @@ const PRINT_CONFIG: &str = "print-config"; pub(super) const UNSET: &str = "unset"; fn command() -> Command { - let mut command = Command::new("continuum") + let command = Command::new("continuum") .version(env!("CARGO_PKG_VERSION")) .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."), + ); + with_options(command, OPTIONS) +} + +/// `command` taking `options` as arguments, and configuration files for them +/// (`--config`) and unsets of what those set (`--unset`). +pub(crate) fn with_options(command: Command, options: &'static [OptionSpec]) -> Command { + let mut command = command .after_help( "Every option can also be set in a configuration file (--config); arguments win.\n\ A value can be imported instead of written out: env:NAME, file:PATH or cmd:COMMAND \ @@ -1820,14 +1885,8 @@ fn command() -> Command { .value_name("OPTION") .action(ArgAction::Append) .help("Unset an option set by a configuration file (back to its default); repeatable."), - ) - .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."), ); - for spec in OPTIONS { + for spec in options { let help = match spec.default { Some(default) => format!("{} [default: {default}]", spec.help), None => spec.help.to_owned(), @@ -1848,15 +1907,21 @@ fn command() -> Command { } /// The values given as arguments (unsets first, so a value given too wins). +#[cfg(test)] fn arguments(matches: &ArgMatches) -> Result<Layer> { + arguments_with(matches, OPTIONS) +} + +/// The values of `options` given as arguments (unsets first, so a value given too wins). +fn arguments_with(matches: &ArgMatches, options: &'static [OptionSpec]) -> Result<Layer> { let mut layer = Layer::new(); for name in matches.get_many::<String>(UNSET).into_iter().flatten() { - if spec(name).is_none() { + if spec_in(options, name).is_none() { bail!("--unset {name}: no such option"); } layer.insert(name.clone(), unset_entry(Provenance::Argument)); } - for spec in OPTIONS { + for spec in options { if matches.value_source(spec.name) != Some(ValueSource::CommandLine) { continue; } @@ -2047,25 +2112,46 @@ pub(crate) fn load() -> Result<Loaded> { // Logs go to stdout too; the report says where each value came from anyway. let _ = crate::logging::set_filter("off"); } + let (merged, files) = merged(&matches, OPTIONS)?; + let mut loaded = resolve(merged, files)?; + loaded.print_only = print_only; + Ok(loaded) +} + +/// The values of `options`: from the configuration files `matches` names (`--config`), in +/// order, then from its arguments; and those files. +pub(crate) fn merged( + matches: &ArgMatches, + options: &'static [OptionSpec], +) -> Result<(Layer, Vec<PathBuf>)> { let files: Vec<PathBuf> = matches .get_many::<String>(CONFIG) .into_iter() .flatten() .map(PathBuf::from) .collect(); - let mut merged = Layer::new(); for path in &files { - for (name, entry) in file::load(path)? { + for (name, entry) in file::load_with(path, options)? { overlay(&mut merged, name, entry, Overlay::FileOverFile); } } - for (name, entry) in arguments(&matches)? { + for (name, entry) in arguments_with(matches, options)? { overlay(&mut merged, name, entry, Overlay::ArgumentOverFile); } - let mut loaded = resolve(merged, files)?; - loaded.print_only = print_only; - Ok(loaded) + Ok((merged, files)) +} + +/// `T` read from the values `merged` sets, each option's by its name: those unset take +/// `T`'s defaults. +#[cfg(feature = "federation")] +pub(crate) fn read<T: serde::de::DeserializeOwned>(merged: &Layer) -> Result<T> { + let object: serde_json::Map<String, JsonValue> = merged + .iter() + .filter(|(_, entry)| !entry.value.is_null()) + .map(|(name, entry)| (name.clone(), entry.value.clone())) + .collect(); + serde_json::from_value(JsonValue::Object(object)).map_err(|error| anyhow!("{error}")) } /// HyParView's active view, where `neighbours` isn't set: the paper's, for swarms of up |
