Execution Context

This commit is contained in:
Roey Darwish Dror
2020-02-08 22:13:56 +02:00
committed by GitHub
parent fb5cf5c176
commit c716b68e9c
3 changed files with 69 additions and 4 deletions

57
src/execution_context.rs Normal file
View File

@@ -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<PathBuf>,
config: &'a Config,
base_dirs: &'a BaseDirs,
}
impl<'a> ExecutionContext<'a> {
#[cfg(unix)]
pub fn new(
run_type: RunType,
sudo: &'a Option<PathBuf>,
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<PathBuf> {
&self.sudo
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn base_dirs(&self) -> &BaseDirs {
&self.base_dirs
}
}

View File

@@ -2,6 +2,7 @@
mod config; mod config;
mod ctrlc; mod ctrlc;
mod error; mod error;
mod execution_context;
mod executor; mod executor;
mod report; mod report;
#[cfg(feature = "self-update")] #[cfg(feature = "self-update")]
@@ -95,6 +96,12 @@ fn run() -> Result<()> {
let sudo = utils::sudo(); let sudo = utils::sudo();
let run_type = executor::RunType::new(config.dry_run()); 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")] #[cfg(feature = "self-update")]
{ {
openssl_probe::init_ssl_cert_env_vars(); openssl_probe::init_ssl_cert_env_vars();
@@ -115,7 +122,7 @@ fn run() -> Result<()> {
if let Some(commands) = config.pre_commands() { if let Some(commands) = config.pre_commands() {
for (name, command) in 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( execute(
&mut report, &mut report,
name, name,
|| generic::run_custom_command(&name, &command, run_type), || generic::run_custom_command(&name, &command, &ctx),
config.no_retry(), config.no_retry(),
)?; )?;
} }

View File

@@ -1,4 +1,5 @@
use crate::error::{SkipStep, TopgradeError}; use crate::error::{SkipStep, TopgradeError};
use crate::execution_context::ExecutionContext;
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::{print_separator, shell}; use crate::terminal::{print_separator, shell};
use crate::utils::{self, PathExt}; use crate::utils::{self, PathExt};
@@ -166,9 +167,9 @@ pub fn run_myrepos_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<()>
.check_run() .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); 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<()> { pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {