2018-12-15 21:52:21 +02:00
|
|
|
use crate::error::{Error, ErrorKind};
|
2018-12-31 13:26:17 +02:00
|
|
|
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;
|
2018-09-04 11:21:32 +03:00
|
|
|
use std::io;
|
2018-09-04 11:05:54 +03:00
|
|
|
use std::os::unix::process::CommandExt;
|
2018-09-04 11:21:32 +03:00
|
|
|
use std::path::Path;
|
2018-09-04 11:05:54 +03:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-09-04 11:05:54 +03:00
|
|
|
if let Some(tpm) = base_dirs
|
|
|
|
|
.home_dir()
|
|
|
|
|
.join(".tmux/plugins/tpm/bin/update_plugins")
|
|
|
|
|
.if_exists()
|
|
|
|
|
{
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("tmux plugins");
|
2018-09-04 11:05:54 +03:00
|
|
|
|
|
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&tpm).arg("all").check_run()?;
|
2018-09-04 11:05:54 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-09-04 11:05:54 +03:00
|
|
|
|
|
|
|
|
return Some(("tmux", success));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 11:21:32 +03:00
|
|
|
fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {
|
|
|
|
|
Ok(Command::new(tmux)
|
|
|
|
|
.args(&["has-session", "-t", session_name])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.success())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_in_session(tmux: &Path, command: &str) -> Result<(), Error> {
|
|
|
|
|
Command::new(tmux)
|
|
|
|
|
.args(&["new-window", "-a", "-t", "topgrade:1", command])
|
2018-12-11 16:43:26 +02:00
|
|
|
.spawn()
|
|
|
|
|
.context(ErrorKind::ProcessExecution)?
|
|
|
|
|
.wait()
|
|
|
|
|
.context(ErrorKind::ProcessExecution)?
|
2018-09-04 11:21:32 +03:00
|
|
|
.check()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 11:05:54 +03:00
|
|
|
pub fn run_in_tmux() -> ! {
|
|
|
|
|
let tmux = which("tmux").expect("Could not find tmux");
|
2018-09-04 11:21:32 +03:00
|
|
|
let command = env::args().collect::<Vec<String>>().join(" ");
|
2018-09-04 11:05:54 +03:00
|
|
|
|
2018-09-04 11:21:32 +03:00
|
|
|
if has_session(&tmux, "topgrade").expect("Error launching tmux") {
|
|
|
|
|
run_in_session(&tmux, &command).expect("Error launching tmux");
|
|
|
|
|
|
|
|
|
|
let err = Command::new(tmux).args(&["attach", "-t", "topgrade"]).exec();
|
|
|
|
|
|
|
|
|
|
panic!("{:?}", err);
|
|
|
|
|
} else {
|
|
|
|
|
let err = Command::new(tmux)
|
|
|
|
|
.args(&[
|
|
|
|
|
"new-session",
|
|
|
|
|
"-s",
|
|
|
|
|
"topgrade",
|
|
|
|
|
"-n",
|
|
|
|
|
"topgrade",
|
|
|
|
|
&command,
|
|
|
|
|
";",
|
|
|
|
|
"set",
|
|
|
|
|
"remain-on-exit",
|
|
|
|
|
"on",
|
2018-12-11 16:00:19 +02:00
|
|
|
])
|
|
|
|
|
.exec();
|
2018-09-04 11:21:32 +03:00
|
|
|
|
|
|
|
|
panic!("{:?}", err);
|
|
|
|
|
}
|
2018-09-04 11:05:54 +03:00
|
|
|
}
|