From 5326571cbd88ea1558daf0c6e5b2c67c67be4cfd Mon Sep 17 00:00:00 2001 From: Daniel Ploch Date: Tue, 30 Jan 2024 18:13:01 -0500 Subject: [PATCH] cli_util: allow extensions to run custom code before all commands --- cli/src/cli_util.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 828787d7a..cbb515645 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -2837,6 +2837,7 @@ pub struct CliRunner { store_factories: Option, working_copy_factories: Option>>, dispatch_fn: CliDispatchFn, + start_hook_fns: Vec, process_global_args_fns: Vec, } @@ -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(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) }