From d4340732397c4f0f6a8ca79c2874eab6765effa1 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Wed, 20 Jun 2018 21:05:49 +0300 Subject: [PATCH] Add flag to invoke in tmux (fix #10) --- README.md | 4 ++++ src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 428e4b59..a396000e 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,10 @@ Just run `topgrade`. It will run the following steps: * *Linux*: Run [needrestart](https://github.com/liske/needrestart) * *macOS*: Upgrade App Store applications +## Flags +* `-t/--tmux` - Topgrade will launch itself in a new tmux session. This flag has no effect if + Topgrade already runs inside tmux. This is useful when using topgrade on remote systems. + ## Customization You can place a configuration file at `~/.config/topgrade.toml`. Here's an example: diff --git a/src/main.rs b/src/main.rs index 1e02888e..c09574d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,14 +25,18 @@ mod terminal; mod utils; mod vim; -use clap::App; +use clap::{App, Arg}; use config::Config; use failure::Error; use git::{Git, Repositories}; use report::{Report, Reporter}; +use std::env; use std::env::home_dir; +#[cfg(unix)] +use std::os::unix::process::CommandExt; use std::path::PathBuf; use std::process::exit; +use std::process::Command; use steps::*; use terminal::Terminal; use utils::{home_path, is_ancestor}; @@ -53,11 +57,40 @@ fn tpm() -> Option { } fn run() -> Result<(), Error> { - let _ = App::new("Topgrade") + let matches = App::new("Topgrade") .version(crate_version!()) .about("Upgrade all the things") + .arg( + Arg::with_name("tmux") + .help("Invoke inside tmux") + .short("t") + .long("tmux"), + ) .get_matches(); + if matches.is_present("tmux") && !env::var("TMUX").is_ok() { + if cfg!(unix) { + let tmux = utils::which("tmux").expect("Could not find tmux"); + let err = Command::new(tmux) + .args(&[ + "new-session", + "-s", + "topgrade", + "-n", + "topgrade", + &env::args().collect::>().join(" "), + ";", + "set", + "remain-on-exit", + "on", + ]) + .exec(); + panic!("{:?}", err); + } else { + panic!("This flag is only implemented in Unix systems"); + } + } + env_logger::init(); let git = Git::new(); let mut git_repos = Repositories::new(&git);