coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 8b0c22d1aa1118c407a948ea848197f346c3865a
parent 5f07237d80cfb5b7a4b50f25511fbfc44d55a05e
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Tue,  2 Jul 2024 12:21:42 -0700

Implement ssh::send_command function

Diffstat:
Msrc/ssh.rs | 34++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/ssh.rs b/src/ssh.rs @@ -80,6 +80,24 @@ fn send_dir(src: &str, dst: &str, host: &str) -> Result<(), String> { Ok(()) } +/// Execute a command on another machine via SSH +#[allow(dead_code)] +pub fn send_command(command: &str, host: &str) -> Result<(), String> { + let mut cmd = Command::new("ssh"); + if host == "test@localhost" { + // SSH options and port for test server hard coded for now + cmd.args(["-o", "StrictHostKeyChecking=no", "-p", "2222"]); + } + println!("{host} {command} running"); + cmd.args([host, command]); + + let status = cmd.status().map_err(|why| why.to_string())?; + if !status.success() { + return Err(format!("SSH exited with {status}")); + } + Ok(()) +} + #[cfg(test)] mod tests { #![allow(unused_imports)] @@ -274,4 +292,20 @@ mod tests { assert_eq!(dst_baz.exists(), true); assert_eq!(read_file(&dst_baz), "old contents of baz"); } + + #[test] + #[cfg(target_family = "unix")] + fn test_send_command_basic() { + let tmp = setup_integration("test_send_command_basic"); + + let dst = "~/test_send_command_basic/foo"; + let dst_real = tmp.ssh.join("foo"); + let cmd = format!("echo 'contents of foo' > {}", dst); + + let result = send_command(&cmd, SSH_HOST); + + assert_eq!(result, Ok(())); + assert_eq!(dst_real.exists(), true); + assert_eq!(read_file(&dst_real), "contents of foo\n"); + } }