Files
topgrade/src/execution_context.rs

64 lines
1.8 KiB
Rust
Raw Normal View History

2020-02-08 22:13:56 +02:00
#![allow(dead_code)]
use crate::executor::RunType;
use crate::sudo::Sudo;
use crate::utils::{get_require_sudo_string, require_option};
use crate::{config::Config, executor::Executor};
2022-11-11 09:39:29 -05:00
use color_eyre::eyre::Result;
use std::env::var;
use std::path::Path;
use std::sync::Mutex;
2020-02-08 22:13:56 +02:00
pub struct ExecutionContext<'a> {
run_type: RunType,
sudo: Option<Sudo>,
2020-02-08 22:13:56 +02:00
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<Option<String>>,
/// True if topgrade is running under ssh.
under_ssh: bool,
2020-02-08 22:13:56 +02:00
}
impl<'a> ExecutionContext<'a> {
pub fn new(run_type: RunType, sudo: Option<Sudo>, config: &'a Config) -> Self {
let under_ssh = var("SSH_CLIENT").is_ok() || var("SSH_TTY").is_ok();
Self {
2020-02-08 22:13:56 +02:00
run_type,
sudo,
config,
tmux_session: Mutex::new(None),
under_ssh,
2020-02-08 22:13:56 +02:00
}
}
pub fn execute_elevated(&self, command: &Path, interactive: bool) -> Result<Executor> {
let sudo = require_option(self.sudo.as_ref(), get_require_sudo_string())?;
Ok(sudo.execute_elevated(self, command, interactive))
}
2020-02-08 22:13:56 +02:00
pub fn run_type(&self) -> RunType {
self.run_type
}
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
}
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<String> {
self.tmux_session.lock().unwrap().clone()
}
2020-02-08 22:13:56 +02:00
}