diff --git a/src/main.rs b/src/main.rs index 65acd653..17e589e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use self::error::StepFailed; #[cfg(all(windows, feature = "self-update"))] use self::error::Upgraded; -use self::steps::*; +use self::steps::{remote::*, *}; use self::terminal::*; use anyhow::{anyhow, Result}; use log::debug; @@ -115,7 +115,7 @@ fn run() -> Result<()> { if let Some(topgrades) = config.remote_topgrades() { for remote_topgrade in topgrades.iter().filter(|t| config.should_execute_remote(t)) { runner.execute(Step::Remotes, format!("Remote ({})", remote_topgrade), || { - generic::run_remote_topgrade(&ctx, remote_topgrade) + remote::ssh::ssh_step(&ctx, remote_topgrade) })?; } } diff --git a/src/steps/generic.rs b/src/steps/generic.rs index 2ba0e49d..60b9abfe 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -269,38 +269,3 @@ pub fn run_composer_update(ctx: &ExecutionContext) -> Result<()> { Ok(()) } - -pub fn run_remote_topgrade(ctx: &ExecutionContext, hostname: &str) -> Result<()> { - let ssh = utils::require("ssh")?; - - let topgrade = ctx.config().remote_topgrade_path(); - - if ctx.config().run_in_tmux() && !ctx.run_type().dry() { - #[cfg(unix)] - { - crate::tmux::run_remote_topgrade(hostname, &ssh, topgrade, ctx.config().tmux_arguments())?; - Err(SkipStep(String::from("Remote Topgrade launched in Tmux")).into()) - } - - #[cfg(not(unix))] - unreachable!("Tmux execution is only implemented in Unix"); - } else { - let mut args = vec!["-t", hostname]; - - if let Some(ssh_arguments) = ctx.config().ssh_arguments() { - args.extend(ssh_arguments.split_whitespace()); - } - - let env = format!("TOPGRADE_PREFIX={}", hostname); - args.extend(&["env", &env, topgrade]); - - if ctx.config().yes() { - args.push("-y"); - } - - print_separator(format!("Remote ({})", hostname)); - println!("Connecting to {}...", hostname); - - ctx.run_type().execute(&ssh).args(&args).check_run() - } -} diff --git a/src/steps/mod.rs b/src/steps/mod.rs index 7757c070..1c3cf5cb 100644 --- a/src/steps/mod.rs +++ b/src/steps/mod.rs @@ -4,9 +4,9 @@ pub mod git; pub mod node; pub mod os; pub mod powershell; +pub mod remote; #[cfg(unix)] pub mod tmux; -pub mod vagrant; pub mod vim; #[cfg(unix)] pub mod zsh; diff --git a/src/steps/remote/mod.rs b/src/steps/remote/mod.rs new file mode 100644 index 00000000..bb8eb966 --- /dev/null +++ b/src/steps/remote/mod.rs @@ -0,0 +1,2 @@ +pub mod ssh; +pub mod vagrant; diff --git a/src/steps/remote/ssh.rs b/src/steps/remote/ssh.rs new file mode 100644 index 00000000..9ca7cdee --- /dev/null +++ b/src/steps/remote/ssh.rs @@ -0,0 +1,46 @@ +#[cfg(unix)] +use crate::error::SkipStep; +use crate::{execution_context::ExecutionContext, terminal::print_separator, utils}; +use anyhow::Result; + +pub fn ssh_step(ctx: &ExecutionContext, hostname: &str) -> Result<()> { + let ssh = utils::require("ssh")?; + + let topgrade = ctx.config().remote_topgrade_path(); + + if ctx.config().run_in_tmux() && !ctx.run_type().dry() { + #[cfg(unix)] + { + let command = format!( + "{ssh} -t {hostname} env TOPGRADE_PREFIX={hostname} TOPGRADE_KEEP_END=1 {topgrade}", + ssh = ssh.display(), + hostname = hostname, + topgrade = topgrade + ); + + crate::tmux::run_command(ctx, &command)?; + Err(SkipStep(String::from("Remote Topgrade launched in Tmux")).into()) + } + + #[cfg(not(unix))] + unreachable!("Tmux execution is only implemented in Unix"); + } else { + let mut args = vec!["-t", hostname]; + + if let Some(ssh_arguments) = ctx.config().ssh_arguments() { + args.extend(ssh_arguments.split_whitespace()); + } + + let env = format!("TOPGRADE_PREFIX={}", hostname); + args.extend(&["env", &env, topgrade]); + + if ctx.config().yes() { + args.push("-y"); + } + + print_separator(format!("Remote ({})", hostname)); + println!("Connecting to {}...", hostname); + + ctx.run_type().execute(&ssh).args(&args).check_run() + } +} diff --git a/src/steps/vagrant.rs b/src/steps/remote/vagrant.rs similarity index 100% rename from src/steps/vagrant.rs rename to src/steps/remote/vagrant.rs diff --git a/src/steps/tmux.rs b/src/steps/tmux.rs index d47f06b9..9edf0ac4 100644 --- a/src/steps/tmux.rs +++ b/src/steps/tmux.rs @@ -1,12 +1,15 @@ use crate::executor::RunType; use crate::terminal::print_separator; -use crate::utils::{which, Check, PathExt}; +use crate::{ + execution_context::ExecutionContext, + utils::{which, Check, PathExt}, +}; use anyhow::Result; use directories::BaseDirs; use std::env; use std::io; use std::os::unix::process::CommandExt; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::process::{exit, Command}; pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> { @@ -104,14 +107,8 @@ pub fn run_in_tmux(args: &Option) -> ! { } } -pub fn run_remote_topgrade(hostname: &str, ssh: &Path, topgrade: &str, tmux_args: &Option) -> Result<()> { - let command = format!( - "{ssh} -t {hostname} env TOPGRADE_PREFIX={hostname} TOPGRADE_KEEP_END=1 {topgrade}", - ssh = ssh.display(), - hostname = hostname, - topgrade = topgrade - ); - Tmux::new(tmux_args) +pub fn run_command(ctx: &ExecutionContext, command: &str) -> Result<()> { + Tmux::new(ctx.config().tmux_arguments()) .build() .args(&["new-window", "-a", "-t", "topgrade:1", &command]) .env_remove("TMUX")