2020-02-08 22:13:56 +02:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
use crate::executor::RunType;
|
2020-03-08 21:38:49 +02:00
|
|
|
use crate::git::Git;
|
2022-11-25 17:19:32 -05:00
|
|
|
use crate::sudo::Sudo;
|
2023-06-13 22:15:57 +08:00
|
|
|
use crate::utils::{require_option, REQUIRE_SUDO};
|
2022-04-09 16:29:55 +03:00
|
|
|
use crate::{config::Config, executor::Executor};
|
2022-11-11 09:39:29 -05:00
|
|
|
use color_eyre::eyre::Result;
|
2023-07-18 13:59:55 +08:00
|
|
|
use std::env::var;
|
2022-11-25 17:19:32 -05:00
|
|
|
use std::path::Path;
|
2022-11-15 10:30:26 -05:00
|
|
|
use std::sync::Mutex;
|
2020-02-08 22:13:56 +02:00
|
|
|
|
|
|
|
|
pub struct ExecutionContext<'a> {
|
|
|
|
|
run_type: RunType,
|
2022-11-25 17:19:32 -05:00
|
|
|
sudo: Option<Sudo>,
|
2020-03-08 21:38:49 +02:00
|
|
|
git: &'a Git,
|
2020-02-08 22:13:56 +02:00
|
|
|
config: &'a Config,
|
2022-11-15 10:30:26 -05:00
|
|
|
/// 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<Option<String>>,
|
2023-07-18 13:59:55 +08:00
|
|
|
/// True if topgrade is running under ssh.
|
|
|
|
|
under_ssh: bool,
|
2020-02-08 22:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> ExecutionContext<'a> {
|
2023-05-01 00:02:13 +05:30
|
|
|
pub fn new(run_type: RunType, sudo: Option<Sudo>, git: &'a Git, config: &'a Config) -> Self {
|
2023-07-18 13:59:55 +08:00
|
|
|
let under_ssh = var("SSH_CLIENT").is_ok() || var("SSH_TTY").is_ok();
|
2022-11-15 10:30:26 -05:00
|
|
|
Self {
|
2020-02-08 22:13:56 +02:00
|
|
|
run_type,
|
|
|
|
|
sudo,
|
2020-03-08 21:38:49 +02:00
|
|
|
git,
|
2020-02-08 22:13:56 +02:00
|
|
|
config,
|
2022-11-15 10:30:26 -05:00
|
|
|
tmux_session: Mutex::new(None),
|
2023-07-18 13:59:55 +08:00
|
|
|
under_ssh,
|
2020-02-08 22:13:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 16:29:55 +03:00
|
|
|
pub fn execute_elevated(&self, command: &Path, interactive: bool) -> Result<Executor> {
|
2023-06-13 22:15:57 +08:00
|
|
|
let sudo = require_option(self.sudo.as_ref(), REQUIRE_SUDO.to_string())?;
|
2022-11-25 17:19:32 -05:00
|
|
|
Ok(sudo.execute_elevated(self, command, interactive))
|
2022-04-09 16:29:55 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-08 22:13:56 +02:00
|
|
|
pub fn run_type(&self) -> RunType {
|
|
|
|
|
self.run_type
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 21:38:49 +02:00
|
|
|
pub fn git(&self) -> &Git {
|
2021-09-02 07:27:09 +03:00
|
|
|
self.git
|
2020-03-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 17:19:32 -05:00
|
|
|
pub fn sudo(&self) -> &Option<Sudo> {
|
|
|
|
|
&self.sudo
|
2020-02-08 22:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn config(&self) -> &Config {
|
2021-09-02 07:27:09 +03:00
|
|
|
self.config
|
2020-02-08 22:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 13:59:55 +08:00
|
|
|
pub fn under_ssh(&self) -> bool {
|
|
|
|
|
self.under_ssh
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 10:30:26 -05:00
|
|
|
pub fn set_tmux_session(&self, session_name: String) {
|
|
|
|
|
self.tmux_session.lock().unwrap().replace(session_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_tmux_session(&self) -> Option<String> {
|
|
|
|
|
self.tmux_session.lock().unwrap().clone()
|
|
|
|
|
}
|
2020-02-08 22:13:56 +02:00
|
|
|
}
|