mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-07 04:51:45 +00:00
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.
This commit is contained in:
parent
b6794ca04a
commit
c3d9ba9ca9
2 changed files with 20 additions and 4 deletions
|
@ -2587,6 +2587,7 @@ pub fn handle_command_result(
|
||||||
pub struct CliRunner {
|
pub struct CliRunner {
|
||||||
tracing_subscription: TracingSubscription,
|
tracing_subscription: TracingSubscription,
|
||||||
app: Command,
|
app: Command,
|
||||||
|
extra_configs: Option<config::Config>,
|
||||||
store_factories: Option<StoreFactories>,
|
store_factories: Option<StoreFactories>,
|
||||||
dispatch_fn: CliDispatchFn,
|
dispatch_fn: CliDispatchFn,
|
||||||
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
|
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
|
||||||
|
@ -2605,6 +2606,7 @@ impl CliRunner {
|
||||||
CliRunner {
|
CliRunner {
|
||||||
tracing_subscription,
|
tracing_subscription,
|
||||||
app: crate::commands::default_app(),
|
app: crate::commands::default_app(),
|
||||||
|
extra_configs: None,
|
||||||
store_factories: None,
|
store_factories: None,
|
||||||
dispatch_fn: Box::new(crate::commands::run_command),
|
dispatch_fn: Box::new(crate::commands::run_command),
|
||||||
process_global_args_fns: vec![],
|
process_global_args_fns: vec![],
|
||||||
|
@ -2617,6 +2619,12 @@ impl CliRunner {
|
||||||
self
|
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.
|
/// Replaces `StoreFactories` to be used.
|
||||||
pub fn set_store_factories(mut self, store_factories: StoreFactories) -> Self {
|
pub fn set_store_factories(mut self, store_factories: StoreFactories) -> Self {
|
||||||
self.store_factories = Some(store_factories);
|
self.store_factories = Some(store_factories);
|
||||||
|
@ -2709,8 +2717,16 @@ impl CliRunner {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[instrument(skip(self))]
|
#[instrument(skip(self))]
|
||||||
pub fn run(self) -> ExitCode {
|
pub fn run(mut self) -> ExitCode {
|
||||||
let layered_configs = LayeredConfigs::from_environment();
|
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())
|
let mut ui = Ui::with_config(&layered_configs.merge())
|
||||||
.expect("default config should be valid, env vars are stringly typed");
|
.expect("default config should be valid, env vars are stringly typed");
|
||||||
let result = self.run_internal(&mut ui, layered_configs);
|
let result = self.run_internal(&mut ui, layered_configs);
|
||||||
|
|
|
@ -74,9 +74,9 @@ pub struct LayeredConfigs {
|
||||||
|
|
||||||
impl LayeredConfigs {
|
impl LayeredConfigs {
|
||||||
/// Initializes configs with infallible sources.
|
/// Initializes configs with infallible sources.
|
||||||
pub fn from_environment() -> Self {
|
pub fn from_environment(default: config::Config) -> Self {
|
||||||
LayeredConfigs {
|
LayeredConfigs {
|
||||||
default: default_config(),
|
default,
|
||||||
env_base: env_base(),
|
env_base: env_base(),
|
||||||
user: None,
|
user: None,
|
||||||
repo: None,
|
repo: None,
|
||||||
|
|
Loading…
Reference in a new issue