From c3d9ba9ca91e4a4fc415db60af0f0ec9697d0aac Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 18 Aug 2023 21:58:28 -0700 Subject: [PATCH] cli: let custom binaries add extra default configs Custom binaries will often want to provide e.g. additional command aliases, additional revset aliases, custom colors, etc. This adds a mechanism for them to do that. --- cli/src/cli_util.rs | 20 ++++++++++++++++++-- cli/src/config.rs | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index ac2633c6e..5bc984dc0 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -2587,6 +2587,7 @@ pub fn handle_command_result( pub struct CliRunner { tracing_subscription: TracingSubscription, app: Command, + extra_configs: Option, store_factories: Option, dispatch_fn: CliDispatchFn, process_global_args_fns: Vec, @@ -2605,6 +2606,7 @@ impl CliRunner { CliRunner { tracing_subscription, app: crate::commands::default_app(), + extra_configs: None, store_factories: None, dispatch_fn: Box::new(crate::commands::run_command), process_global_args_fns: vec![], @@ -2617,6 +2619,12 @@ impl CliRunner { self } + /// Adds default configs in addition to the normal defaults. + pub fn set_extra_config(mut self, extra_configs: config::Config) -> Self { + self.extra_configs = Some(extra_configs); + self + } + /// Replaces `StoreFactories` to be used. pub fn set_store_factories(mut self, store_factories: StoreFactories) -> Self { self.store_factories = Some(store_factories); @@ -2709,8 +2717,16 @@ impl CliRunner { #[must_use] #[instrument(skip(self))] - pub fn run(self) -> ExitCode { - let layered_configs = LayeredConfigs::from_environment(); + pub fn run(mut self) -> ExitCode { + let mut default_config = crate::config::default_config(); + if let Some(extra_configs) = self.extra_configs.take() { + default_config = config::Config::builder() + .add_source(default_config) + .add_source(extra_configs) + .build() + .unwrap(); + } + let layered_configs = LayeredConfigs::from_environment(default_config); let mut ui = Ui::with_config(&layered_configs.merge()) .expect("default config should be valid, env vars are stringly typed"); let result = self.run_internal(&mut ui, layered_configs); diff --git a/cli/src/config.rs b/cli/src/config.rs index bd151d244..b2c22a06c 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -74,9 +74,9 @@ pub struct LayeredConfigs { impl LayeredConfigs { /// Initializes configs with infallible sources. - pub fn from_environment() -> Self { + pub fn from_environment(default: config::Config) -> Self { LayeredConfigs { - default: default_config(), + default, env_base: env_base(), user: None, repo: None,