Files
topgrade/src/steps/tmux.rs

101 lines
2.9 KiB
Rust
Raw Normal View History

2018-12-15 21:52:21 +02:00
use crate::error::{Error, ErrorKind};
use crate::executor::RunType;
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
use crate::utils::{which, Check, PathExt};
2018-09-04 11:05:54 +03:00
use directories::BaseDirs;
2018-12-11 16:43:26 +02:00
use failure::ResultExt;
2018-09-04 11:05:54 +03:00
use std::env;
use std::io;
2018-09-04 11:05:54 +03:00
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{exit, Command};
2018-09-04 11:05:54 +03:00
2019-03-10 21:48:49 +02:00
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
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
}
fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
Ok(Command::new(tmux)
.args(&["has-session", "-t", session_name])
.env_remove("TMUX")
.output()?
.status
.success())
}
fn new_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
Ok(Command::new(tmux)
.args(&["new-session", "-d", "-s", session_name, "-n", "dummy"])
.env_remove("TMUX")
.spawn()?
.wait()?
.success())
}
fn run_in_session(tmux: &Path, command: &str) -> Result<(), Error> {
Command::new(tmux)
.args(&["new-window", "-a", "-t", "topgrade:1", command])
.env_remove("TMUX")
2018-12-11 16:43:26 +02:00
.spawn()
.context(ErrorKind::ProcessExecution)?
.wait()
.context(ErrorKind::ProcessExecution)?
.check()?;
Ok(())
}
2018-09-04 11:05:54 +03:00
pub fn run_in_tmux() -> ! {
let tmux = which("tmux").expect("Could not find tmux");
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
if !has_session(&tmux, "topgrade").expect("Error launching tmux") {
new_session(&tmux, "topgrade").expect("Error launching tmux");
}
run_in_session(&tmux, &command).expect("Error launching tmux");
Command::new(&tmux)
.args(&["kill-window", "-t", "topgrade:dummy"])
.output()
.unwrap();
if env::var("TMUX").is_err() {
let err = Command::new(tmux).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) -> Result<(), Error> {
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
);
Command::new(which("tmux").unwrap())
.args(&["new-window", "-a", "-t", "topgrade:1", &command])
2019-06-13 16:43:23 +03:00
.env_remove("TMUX")
.spawn()
.context(ErrorKind::ProcessExecution)?
.wait()
.context(ErrorKind::ProcessExecution)?
.check()
}