Files
topgrade/src/steps/tmux.rs

121 lines
3.3 KiB
Rust
Raw Normal View History

use crate::executor::RunType;
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
use crate::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;
use std::path::{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
2019-03-10 21:48:49 +02: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: &Option<String>) -> Self {
Self {
tmux: which("tmux").expect("Could not find tmux"),
args: args
.as_ref()
.map(|args| args.split_whitespace().map(String::from).collect()),
}
}
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()
2020-06-03 22:12:45 +03:00
.args(&["new-window", "-t", "topgrade", command])
.spawn()?
.wait()?
.check()?;
Ok(())
}
}
pub fn run_in_tmux(args: &Option<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());
command.join(" ")
};
2018-09-04 11:05:54 +03:00
let tmux = Tmux::new(args);
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");
}
2019-12-08 20:59:42 +02:00
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
pub fn run_remote_topgrade(hostname: &str, ssh: &Path, tmux_args: &Option<String>) -> Result<()> {
2019-06-13 16:43:23 +03:00
let command = format!(
2019-06-17 08:16:08 +03:00
"{ssh} -t {hostname} env TOPGRADE_PREFIX={hostname} TOPGRADE_KEEP_END=1 topgrade",
2019-06-13 16:43:23 +03:00
ssh = ssh.display(),
hostname = hostname
);
Tmux::new(tmux_args)
.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()
}