Fix tmux panic (#165)

Fix `tmux` sessions

This will create a new session named `topgrade`, `topgrade-1`,
`topgrade-2`, using the first nonexistent session name it finds. That
session will have a window in it named `topgrade` in which `topgrade` is
run. If `topgrade --tmux` is being run from within tmux, it won't attach
to the new tmux session. If the user is not currently in tmux, it will
attach to the newly-created session.

Co-authored-by: Thomas Schönauer <37108907+DottoDev@users.noreply.github.com>
This commit is contained in:
Rebecca Turner
2022-11-15 10:30:26 -05:00
committed by Thomas Schönauer
parent d4fe748814
commit 71883d7164
4 changed files with 112 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ use crate::{config::Config, executor::Executor};
use color_eyre::eyre::Result;
use directories::BaseDirs;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
pub struct ExecutionContext<'a> {
run_type: RunType,
@@ -13,6 +14,10 @@ pub struct ExecutionContext<'a> {
git: &'a Git,
config: &'a Config,
base_dirs: &'a BaseDirs,
/// 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>>,
}
impl<'a> ExecutionContext<'a> {
@@ -22,13 +27,14 @@ impl<'a> ExecutionContext<'a> {
git: &'a Git,
config: &'a Config,
base_dirs: &'a BaseDirs,
) -> ExecutionContext<'a> {
ExecutionContext {
) -> Self {
Self {
run_type,
sudo,
git,
config,
base_dirs,
tmux_session: Mutex::new(None),
}
}
@@ -67,4 +73,12 @@ impl<'a> ExecutionContext<'a> {
pub fn base_dirs(&self) -> &BaseDirs {
self.base_dirs
}
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()
}
}