Skip to content

Commit

Permalink
cli: let custom binaries add extra default configs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinvonz committed Aug 19, 2023
1 parent b6794ca commit c3d9ba9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,7 @@ pub fn handle_command_result(
pub struct CliRunner {
tracing_subscription: TracingSubscription,
app: Command,
extra_configs: Option<config::Config>,
store_factories: Option<StoreFactories>,
dispatch_fn: CliDispatchFn,
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
Expand All @@ -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![],
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c3d9ba9

Please sign in to comment.