Compare commits

..

No commits in common. "master" and "26-integration-tests" have entirely different histories.

7 changed files with 70 additions and 165 deletions

View File

@ -1,10 +1,10 @@
[package] [package]
name = "multi-ssh" name = "multi-ssh"
version = "0.2.0" version = "0.2.0"
edition = "2024" edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.6.6", features = ["derive"] } clap = { version = "4.5.23", features = ["derive"] }
lazy-regex = "3.6.1" lazy-regex = "3.3.0"
shell-words = "1.1.1" shell-words = "1.1.0"
homedir = "0.3.6" homedir = "0.3.4"

View File

@ -2,16 +2,16 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1787962033, "lastModified": 1759580034,
"narHash": "sha256-u6z9VTZA4Kf3RkHQo9sQI7NI4Ei/uiU9vrMqOiwWP1Y=", "narHash": "sha256-YWo57PL7mGZU7D4WeKFMiW4ex/O6ZolUS6UNBHTZfkI=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "c5c4a43b0e8056328ec4529f735cabdb8f1942bb", "rev": "3bcc93c5f7a4b30335d31f21e2f1281cba68c318",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-26.05", "ref": "nixos-25.05",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
@ -43,11 +43,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1788165049, "lastModified": 1759631821,
"narHash": "sha256-en4IoUeCqvq9F66YhwOrUFw1nc70OBhrJwrzfalezvY=", "narHash": "sha256-V8A1L0FaU/aSXZ1QNJScxC12uP4hANeRBgI4YdhHeRM=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "d03cd474bd97389dcc2e8cd3b3bb6b8c6e346b1a", "rev": "1d7cbdaad90f8a5255a89a6eddd8af24dc89cafe",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -2,7 +2,7 @@
description = "Flake of https://dev.stupstech.de/Mr_Steppy/multi-ssh"; description = "Flake of https://dev.stupstech.de/Mr_Steppy/multi-ssh";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
rust-overlay.url = "github:oxalica/rust-overlay"; rust-overlay.url = "github:oxalica/rust-overlay";
}; };

View File

@ -1,6 +1,6 @@
use crate::shell_interface::{CommandOutput, CommandResult, ExitStatus, ShellCommand, use crate::shell_interface::{
ShellInterface, StartError, build_command_from_shell_command, CommandOutput, CommandResult, ExitStatus, ShellCommand,
build_command_from_shell_command, ShellInterface, StartError,
}; };
use std::env::VarError; use std::env::VarError;
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
@ -27,14 +27,10 @@ pub trait Environment {
V: AsRef<OsStr>; V: AsRef<OsStr>;
fn get_home_directory(&self) -> Option<PathBuf>; fn get_home_directory(&self) -> Option<PathBuf>;
fn read_line(&mut self) -> Result<String, io::Error>; fn read_line(&mut self) -> Result<String, io::Error>;
fn is_ssh_agent_started(&self) -> bool;
fn set_ssh_agent_started(&mut self, enabled: bool);
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Prod { pub struct Prod;
ssh_agent_started: bool,
}
impl Environment for Prod { impl Environment for Prod {
fn args_os(&self) -> Vec<OsString> { fn args_os(&self) -> Vec<OsString> {
@ -42,8 +38,8 @@ impl Environment for Prod {
} }
fn var_os<K>(&self, key: K) -> Option<OsString> fn var_os<K>(&self, key: K) -> Option<OsString>
where where
K: AsRef<OsStr>, K: AsRef<OsStr>
{ {
env::var_os(key) env::var_os(key)
} }
@ -60,10 +56,7 @@ impl Environment for Prod {
K: AsRef<OsStr>, K: AsRef<OsStr>,
V: AsRef<OsStr>, V: AsRef<OsStr>,
{ {
unsafe { env::set_var(key, value);
//multi-ssh is single threaded
env::set_var(key, value);
}
} }
fn get_home_directory(&self) -> Option<PathBuf> { fn get_home_directory(&self) -> Option<PathBuf> {
@ -75,14 +68,6 @@ impl Environment for Prod {
io::stdin().read_line(&mut buffer)?; io::stdin().read_line(&mut buffer)?;
Ok(buffer.trim().to_string()) Ok(buffer.trim().to_string())
} }
fn is_ssh_agent_started(&self) -> bool {
self.ssh_agent_started
}
fn set_ssh_agent_started(&mut self, enabled: bool) {
self.ssh_agent_started = enabled;
}
} }
impl ShellInterface for Prod { impl ShellInterface for Prod {

View File

@ -74,14 +74,6 @@ impl Environment for TestEnvironment {
fn read_line(&mut self) -> Result<String, Error> { fn read_line(&mut self) -> Result<String, Error> {
self.stdin.pop_front().ok_or_else(|| Error::other("Unexpected call to read_line: No input prepared")) self.stdin.pop_front().ok_or_else(|| Error::other("Unexpected call to read_line: No input prepared"))
} }
fn is_ssh_agent_started(&self) -> bool {
self.ssh_agent_started
}
fn set_ssh_agent_started(&mut self, enabled: bool) {
self.ssh_agent_started = enabled;
}
} }
impl ShellInterface for TestEnvironment { impl ShellInterface for TestEnvironment {

View File

@ -18,7 +18,7 @@ use crate::os_string_builder::ReplaceWithOsStr;
use crate::server::{RelativeLocalPathAnker, ServerAddress}; use crate::server::{RelativeLocalPathAnker, ServerAddress};
use crate::shell_interface::{ScpParam, ServerCommand, ShellCommand, ShellInterface}; use crate::shell_interface::{ScpParam, ServerCommand, ShellCommand, ShellInterface};
use clap::{Parser, Subcommand, ValueEnum}; use clap::{Parser, Subcommand, ValueEnum};
use lazy_regex::{Lazy, Regex, lazy_regex}; use lazy_regex::{lazy_regex, Lazy, Regex};
use server::{Server, ServerReference}; use server::{Server, ServerReference};
use std::cell::LazyCell; use std::cell::LazyCell;
use std::ffi::OsString; use std::ffi::OsString;
@ -170,30 +170,6 @@ where
}, },
}; };
macro_rules! start_ssh_agent {
() => {
self.start_ssh_agent(&logger)?;
};
}
macro_rules! ssh {
($($field:ident $(: $value:expr)?$(,)? )*) => {{
start_ssh_agent!();
ShellCommand::Ssh {
$( $field $(: $value)? ),*
}
}};
}
macro_rules! scp {
($($field:ident $(: $value:expr)?$(,)? )*) => {{
start_ssh_agent!();
ShellCommand::Scp {
$( $field $(: $value)? ),*
}
}};
}
let mut configured_servers = LazyCell::new(|| self.parse_server_configuration_from_env()); let mut configured_servers = LazyCell::new(|| self.parse_server_configuration_from_env());
let servers = args let servers = args
.servers .servers
@ -247,6 +223,8 @@ where
None => None, None => None,
}; };
self.start_ssh_agent(&logger)?;
//make sure files exist //make sure files exist
match &file_server { match &file_server {
Some(file_server) => match &file_server.address { Some(file_server) => match &file_server.address {
@ -255,7 +233,7 @@ where
files = files files = files
.iter() .iter()
.map(|file| { .map(|file| {
let output = ssh! { let output = ShellCommand::Ssh {
address: ssh_address.to_string(), address: ssh_address.to_string(),
server_command: ServerCommand::Realpath { server_command: ServerCommand::Realpath {
path: file_server.server_directory_path.join(file), path: file_server.server_directory_path.join(file),
@ -335,7 +313,7 @@ where
server, server,
actions: { actions: {
let present_file_names: Vec<OsString> = match &server.address { let present_file_names: Vec<OsString> = match &server.address {
ServerAddress::Ssh { ssh_address } => ssh! { ServerAddress::Ssh { ssh_address } => ShellCommand::Ssh {
address: ssh_address.to_string(), address: ssh_address.to_string(),
server_command: ServerCommand::Ls { server_command: ServerCommand::Ls {
dir: working_directory.clone(), dir: working_directory.clone(),
@ -486,40 +464,27 @@ where
for file_action in server_actions.actions { for file_action in server_actions.actions {
match file_action.kind { match file_action.kind {
Action::Add | Action::Replace => { Action::Add | Action::Replace => {
//don't use scp on localhost let source = match &file_server {
if file_server.is_none() && matches!(server.address, ServerAddress::Localhost) { Some(file_server) => ScpParam::from((
let file = file_action.file; file_server,
let dest = server_actions.working_directory.join(file_action.file_name); file_server.server_directory_path.join(&file_action.file),
fs::copy(&file, &dest).map_err(|e| { )),
format!( None => ScpParam::from(file_action.file.as_path()),
"Failed to copy from {} to {}: {e}", };
file.to_string_lossy(), let destination = ScpParam::from((server, &server_actions.working_directory));
dest.to_string_lossy() ShellCommand::Scp {
) source,
})?; destination,
} else {
let source = match &file_server {
Some(file_server) => ScpParam::from((
file_server,
file_server.server_directory_path.join(&file_action.file),
)),
None => ScpParam::from(&file_action.file),
};
let destination = ScpParam::from((server, &server_actions.working_directory));
scp! {
source,
destination,
}
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("upload failure: {e}"))?;
} }
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("upload failure: {e}"))?;
} }
Action::Delete => match &server.address { Action::Delete => match &server.address {
ServerAddress::Ssh { ssh_address } => { ServerAddress::Ssh { ssh_address } => {
ssh! { ShellCommand::Ssh {
address: ssh_address.to_string(), address: ssh_address.to_string(),
server_command: ServerCommand::Rm { server_command: ServerCommand::Rm {
file: server_actions.working_directory.join(&file_action.file), file: server_actions.working_directory.join(&file_action.file),
@ -538,7 +503,7 @@ where
}, },
Action::Rename { new_name } => match &server.address { Action::Rename { new_name } => match &server.address {
ServerAddress::Ssh { ssh_address } => { ServerAddress::Ssh { ssh_address } => {
ssh! { ShellCommand::Ssh {
address: ssh_address.to_string(), address: ssh_address.to_string(),
server_command: ServerCommand::Mv { server_command: ServerCommand::Mv {
source: server_actions.working_directory.join(&file_action.file), source: server_actions.working_directory.join(&file_action.file),
@ -564,12 +529,13 @@ where
log!(logger, "Done!"); log!(logger, "Done!");
} }
Command::Command { command } => { Command::Command { command } => {
self.start_ssh_agent(&logger)?;
Self::require_non_empty_servers(&servers)?; Self::require_non_empty_servers(&servers)?;
for server in servers { for server in servers {
log!(logger, "Running command on '{}'...", server.get_name()); log!(logger, "Running command on '{}'...", server.get_name());
match &server.address { match &server.address {
ServerAddress::Ssh { ssh_address } => { ServerAddress::Ssh { ssh_address } => {
ssh! { ShellCommand::Ssh {
address: ssh_address.to_string(), address: ssh_address.to_string(),
server_command: ServerCommand::Execute { server_command: ServerCommand::Execute {
working_directory: server.server_directory_path.clone(), working_directory: server.server_directory_path.clone(),
@ -659,37 +625,28 @@ where
} }
Self::require_non_empty_servers(&servers)?; Self::require_non_empty_servers(&servers)?;
self.start_ssh_agent(&logger)?;
for server in servers { for server in servers {
log!(logger, "Getting file from {}...", server.get_name()); log!(logger, "Getting file from {}...", server.get_name());
let file_path = server.server_directory_path.join(&file); let source = ScpParam::from((&server, server.server_directory_path.join(&file)));
let downloaded_file_path = download_directory.join(file_name); ShellCommand::Scp {
if matches!(server.address, ServerAddress::Localhost) { source: source.clone(),
//no need to use scp on localhost destination: ScpParam::from(download_directory.as_path()),
fs::copy(&file_path, &downloaded_file_path).map_err(|e| {
format!(
"failed to copy {} to {}: {e}",
file_path.to_string_lossy(),
downloaded_file_path.to_string_lossy()
)
})?;
} else {
scp! {
source: ScpParam::from((&server, &file_path)),
destination: ScpParam::from(&download_directory),
}
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("download failure: {e}"))?;
} }
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("download failure: {e}"))?;
//open file in editor //open file in editor
let editor_command = shell_words::split(&editor) let editor_command = shell_words::split(&editor)
.map_err(|e| format!("failed to parse editor command: {e}"))? .map_err(|e| format!("failed to parse editor command: {e}"))?
.into_iter() .into_iter()
.map(|part| part.replace_with_os_str(FILE_PLACEHOLDER, &file_path)) .map(|part| {
part.replace_with_os_str(FILE_PLACEHOLDER, download_directory.join(file_name))
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ShellCommand::Editor(editor_command) ShellCommand::Editor(editor_command)
@ -699,26 +656,16 @@ where
.into_result_with_error_logging(&logger) .into_result_with_error_logging(&logger)
.map_err(|e| format!("failed to open file in editor: {e}"))?; .map_err(|e| format!("failed to open file in editor: {e}"))?;
//upload file again; don't use scp on localhost //upload file again
if matches!(server.address, ServerAddress::Localhost) { ShellCommand::Scp {
fs::copy(&downloaded_file_path, &file_path).map_err(|e| { source: ScpParam::from(download_directory.join(file_name).as_path()),
format!( destination: source,
"failed to copy {} to {}: {e}",
file_path.to_string_lossy(),
download_directory.to_string_lossy()
)
})?;
} else {
scp! {
source: ScpParam::from(&downloaded_file_path),
destination: ScpParam::from((&server, &file_path)),
}
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("failed to re-upload file: {e}"))?;
} }
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("failed to re-upload file: {e}"))?;
} }
log!(logger, "Done!"); log!(logger, "Done!");
@ -759,10 +706,6 @@ where
fn start_ssh_agent(&mut self, logger: &Logger) -> Result<(), String> { fn start_ssh_agent(&mut self, logger: &Logger) -> Result<(), String> {
let env = &mut self.environment; let env = &mut self.environment;
if env.is_ssh_agent_started() {
return Ok(());
}
//start the ssh agent //start the ssh agent
let agent_output = ShellCommand::SshAgent let agent_output = ShellCommand::SshAgent
.in_env(env) .in_env(env)
@ -789,9 +732,6 @@ where
.and_expect_success() .and_expect_success()
.into_result_with_error_logging(logger) .into_result_with_error_logging(logger)
.map_err(|e| format!("failed to add ssh-key: {e}"))?; .map_err(|e| format!("failed to add ssh-key: {e}"))?;
env.set_ssh_agent_started(true);
Ok(()) Ok(())
} }
@ -835,14 +775,8 @@ where
} }
} }
fn main() { fn main() -> Result<(), String> {
match Application::<Prod>::default().run() { Application::<Prod>::default().run()
Ok(_) => {}
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
}
} }
fn parse_server_configuration<F>( fn parse_server_configuration<F>(

View File

@ -70,7 +70,7 @@ pub enum ShellCommand {
} }
impl ShellCommand { impl ShellCommand {
pub fn in_env<E>(self, environment: &mut E) -> EnvCommand<'_, E> { pub fn in_env<E>(self, environment: &mut E) -> EnvCommand<E> {
EnvCommand { EnvCommand {
command: self, command: self,
environment, environment,
@ -129,12 +129,6 @@ impl From<&Path> for ScpParam {
} }
} }
impl From<&PathBuf> for ScpParam {
fn from(value: &PathBuf) -> Self {
Self::from(value.as_path())
}
}
impl From<&ScpParam> for OsString { impl From<&ScpParam> for OsString {
fn from(value: &ScpParam) -> Self { fn from(value: &ScpParam) -> Self {
let mut builder = osf!(); let mut builder = osf!();