#![allow(dead_code)] use color_eyre::eyre::Result; use std::env::var; use std::path::Path; use std::sync::{LazyLock, Mutex}; use crate::executor::RunType; use crate::powershell::Powershell; #[cfg(target_os = "linux")] use crate::steps::linux::Distribution; use crate::sudo::Sudo; use crate::utils::{get_require_sudo_string, require_option}; use crate::{config::Config, executor::Executor}; pub struct ExecutionContext<'a> { run_type: RunType, sudo: Option, config: &'a Config, /// Name of a tmux session to execute commands in, if any. /// This is used in `./steps/remote/ssh.rs`, where we want to run `topgrade` in a new /// tmux window for each remote. tmux_session: Mutex>, /// True if topgrade is running under ssh. under_ssh: bool, #[cfg(target_os = "linux")] distribution: &'a Result, powershell: LazyLock, } impl<'a> ExecutionContext<'a> { pub fn new( run_type: RunType, sudo: Option, config: &'a Config, #[cfg(target_os = "linux")] distribution: &'a Result, ) -> Self { let under_ssh = var("SSH_CLIENT").is_ok() || var("SSH_TTY").is_ok(); Self { run_type, sudo, config, tmux_session: Mutex::new(None), under_ssh, #[cfg(target_os = "linux")] distribution, powershell: LazyLock::new(Powershell::new), } } pub fn execute_elevated(&self, command: &Path, interactive: bool) -> Result { let sudo = require_option(self.sudo.as_ref(), get_require_sudo_string())?; Ok(sudo.execute_elevated(self, command, interactive)) } pub fn run_type(&self) -> RunType { self.run_type } pub fn sudo(&self) -> &Option { &self.sudo } pub fn config(&self) -> &Config { self.config } pub fn under_ssh(&self) -> bool { self.under_ssh } pub fn set_tmux_session(&self, session_name: String) { self.tmux_session.lock().unwrap().replace(session_name); } pub fn get_tmux_session(&self) -> Option { self.tmux_session.lock().unwrap().clone() } #[cfg(target_os = "linux")] pub fn distribution(&self) -> &Result { self.distribution } pub fn powershell(&self) -> &Powershell { &self.powershell } }