diff --git a/src/execution_context.rs b/src/execution_context.rs new file mode 100644 index 00000000..0f039e56 --- /dev/null +++ b/src/execution_context.rs @@ -0,0 +1,57 @@ +#![allow(dead_code)] +use crate::config::Config; +use crate::executor::RunType; +use directories::BaseDirs; +#[cfg(unix)] +use std::path::PathBuf; + +pub struct ExecutionContext<'a> { + run_type: RunType, + #[cfg(unix)] + sudo: &'a Option, + config: &'a Config, + base_dirs: &'a BaseDirs, +} + +impl<'a> ExecutionContext<'a> { + #[cfg(unix)] + pub fn new( + run_type: RunType, + sudo: &'a Option, + config: &'a Config, + base_dirs: &'a BaseDirs, + ) -> ExecutionContext<'a> { + ExecutionContext { + run_type, + sudo, + config, + base_dirs, + } + } + + #[cfg(not(unix))] + pub fn new(run_type: RunType, config: &'a Config, base_dirs: &'a BaseDirs) -> ExecutionContext<'a> { + ExecutionContext { + run_type, + config, + base_dirs, + } + } + + pub fn run_type(&self) -> RunType { + self.run_type + } + + #[cfg(unix)] + pub fn sudo(&self) -> &Option { + &self.sudo + } + + pub fn config(&self) -> &Config { + &self.config + } + + pub fn base_dirs(&self) -> &BaseDirs { + &self.base_dirs + } +} diff --git a/src/main.rs b/src/main.rs index 6825cfab..35052ce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod config; mod ctrlc; mod error; +mod execution_context; mod executor; mod report; #[cfg(feature = "self-update")] @@ -95,6 +96,12 @@ fn run() -> Result<()> { let sudo = utils::sudo(); let run_type = executor::RunType::new(config.dry_run()); + #[cfg(unix)] + let ctx = execution_context::ExecutionContext::new(run_type, &sudo, &config, &base_dirs); + + #[cfg(not(unix))] + let ctx = execution_context::ExecutionContext::new(run_type, &config, &base_dirs); + #[cfg(feature = "self-update")] { openssl_probe::init_ssl_cert_env_vars(); @@ -115,7 +122,7 @@ fn run() -> Result<()> { if let Some(commands) = config.pre_commands() { for (name, command) in commands { - generic::run_custom_command(&name, &command, run_type)?; + generic::run_custom_command(&name, &command, &ctx)?; } } @@ -555,7 +562,7 @@ fn run() -> Result<()> { execute( &mut report, name, - || generic::run_custom_command(&name, &command, run_type), + || generic::run_custom_command(&name, &command, &ctx), config.no_retry(), )?; } diff --git a/src/steps/generic.rs b/src/steps/generic.rs index 7187daae..987a6466 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -1,4 +1,5 @@ use crate::error::{SkipStep, TopgradeError}; +use crate::execution_context::ExecutionContext; use crate::executor::{CommandExt, RunType}; use crate::terminal::{print_separator, shell}; use crate::utils::{self, PathExt}; @@ -166,9 +167,9 @@ pub fn run_myrepos_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> .check_run() } -pub fn run_custom_command(name: &str, command: &str, run_type: RunType) -> Result<()> { +pub fn run_custom_command(name: &str, command: &str, ctx: &ExecutionContext) -> Result<()> { print_separator(name); - run_type.execute(shell()).arg("-c").arg(command).check_run() + ctx.run_type().execute(shell()).arg("-c").arg(command).check_run() } pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {