ok/jj
1
0
Fork 0
forked from mirrors/jj

cli_util: allow extensions to run custom code before all commands

This commit is contained in:
Daniel Ploch 2024-01-30 18:13:01 -05:00 committed by Daniel Ploch
parent 976b801208
commit 5326571cbd

View file

@ -2837,6 +2837,7 @@ pub struct CliRunner {
store_factories: Option<StoreFactories>,
working_copy_factories: Option<HashMap<String, Box<dyn WorkingCopyFactory>>>,
dispatch_fn: CliDispatchFn,
start_hook_fns: Vec<CliDispatchFn>,
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
}
@ -2857,6 +2858,7 @@ impl CliRunner {
store_factories: None,
working_copy_factories: None,
dispatch_fn: Box::new(crate::commands::run_command),
start_hook_fns: vec![],
process_global_args_fns: vec![],
}
}
@ -2888,6 +2890,11 @@ impl CliRunner {
self
}
pub fn add_start_hook(mut self, start_hook_fn: CliDispatchFn) -> Self {
self.start_hook_fns.push(start_hook_fn);
self
}
/// Registers new subcommands in addition to the default ones.
pub fn add_subcommand<C, F>(mut self, custom_dispatch_fn: F) -> Self
where
@ -3001,6 +3008,9 @@ impl CliRunner {
self.store_factories.unwrap_or_default(),
working_copy_factories,
);
for start_hook_fn in self.start_hook_fns {
start_hook_fn(ui, &command_helper)?;
}
(self.dispatch_fn)(ui, &command_helper)
}