Files
topgrade/src/steps/tmux.rs

117 lines
3.1 KiB
Rust
Raw Normal View History

use crate::executor::RunType;
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
2020-08-22 14:46:17 +03:00
use crate::{
execution_context::ExecutionContext,
utils::{which, Check, PathExt},
};
use anyhow::Result;
2018-09-04 11:05:54 +03:00
use directories::BaseDirs;
use std::env;
use std::io;
2018-09-04 11:05:54 +03:00
use std::os::unix::process::CommandExt;
2020-08-22 14:46:17 +03:00
use std::path::PathBuf;
use std::process::{exit, Command};
2018-09-04 11:05:54 +03:00
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-03-10 21:48:49 +02:00
let tpm = base_dirs
2018-09-04 11:05:54 +03:00
.home_dir()
.join(".tmux/plugins/tpm/bin/update_plugins")
2019-03-10 21:48:49 +02:00
.require()?;
2018-09-04 11:05:54 +03:00
2019-03-10 21:48:49 +02:00
print_separator("tmux plugins");
2018-09-04 11:05:54 +03:00
run_type.execute(tpm).arg("all").check_run()
2018-09-04 11:05:54 +03:00
}
struct Tmux {
tmux: PathBuf,
args: Option<Vec<String>>,
}
impl Tmux {
fn new(args: Vec<String>) -> Self {
Self {
tmux: which("tmux").expect("Could not find tmux"),
args: if args.is_empty() { None } else { Some(args) },
}
}
fn build(&self) -> Command {
let mut command = Command::new(&self.tmux);
if let Some(args) = self.args.as_ref() {
command.args(args).env_remove("TMUX");
}
command
}
fn has_session(&self, session_name: &str) -> Result<bool, io::Error> {
Ok(self
.build()
.args(["has-session", "-t", session_name])
.output()?
.status
.success())
}
fn new_session(&self, session_name: &str) -> Result<bool, io::Error> {
Ok(self
.build()
.args(["new-session", "-d", "-s", session_name, "-n", "dummy"])
.spawn()?
.wait()?
.success())
}
fn run_in_session(&self, command: &str) -> Result<()> {
self.build()
.args(["new-window", "-t", "topgrade", command])
.spawn()?
.wait()?
.check()?;
Ok(())
}
}
pub fn run_in_tmux(args: Vec<String>) -> ! {
let command = {
let mut command = vec![
String::from("env"),
String::from("TOPGRADE_KEEP_END=1"),
String::from("TOPGRADE_INSIDE_TMUX=1"),
];
command.extend(env::args());
shell_words::join(command)
};
2018-09-04 11:05:54 +03:00
let tmux = Tmux::new(args.clone());
2019-12-08 20:59:42 +02:00
if !tmux.has_session("topgrade").expect("Error detecting a tmux session") {
tmux.new_session("topgrade").expect("Error creating a tmux session");
}
tmux.run_in_session(&command).expect("Error running Topgrade in tmux");
tmux.build()
.args(["kill-window", "-t", "topgrade:dummy"])
.output()
2019-12-08 20:59:42 +02:00
.expect("Error killing the dummy tmux window");
if env::var("TMUX").is_err() {
let err = tmux.build().args(["attach", "-t", "topgrade"]).exec();
panic!("{:?}", err);
} else {
println!("Topgrade launched in a new tmux session");
exit(0);
}
2018-09-04 11:05:54 +03:00
}
2019-06-13 16:43:23 +03:00
2020-08-22 14:46:17 +03:00
pub fn run_command(ctx: &ExecutionContext, command: &str) -> Result<()> {
Tmux::new(ctx.config().tmux_arguments()?)
.build()
.args(["new-window", "-a", "-t", "topgrade:1", command])
2019-06-13 16:43:23 +03:00
.env_remove("TMUX")
.spawn()?
.wait()?
2019-06-13 16:43:23 +03:00
.check()
}