1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use crate::address::Address; use std::ffi::OsStr; use std::path::PathBuf; #[derive(Debug)] pub struct SshTarget { address: Address, identity: PathBuf, user: String, } impl SshTarget { pub fn new(address: Address, identity: PathBuf, user: &str) -> SshTarget { SshTarget { address, identity, user: user.to_string(), } } pub fn command<S: AsRef<OsStr>>(&self, args: &[S]) -> subprocess::Exec { subprocess::Exec::cmd("ssh") .args(&[ "-oStrictHostKeyChecking=no", "-oUserKnownHostsFile=/dev/null", "-oBatchMode=yes", "-i", self.identity.to_str().unwrap(), "-p", &self.address.port_str(), &format!("{}@{}", self.user, self.address.host), ]) .args(args) } } #[cfg(test)] mod tests { use super::*; use std::path::Path; #[test] fn test_command() { let address = Address::parse("localhost:9222").unwrap(); let identity = Path::new("/myIdentity").to_path_buf(); let user = "me"; let target = SshTarget::new(address, identity, user); let cmd = target.command(&["arg1", "arg2"]); assert_eq!(cmd.to_cmdline_lossy(), "ssh '-oStrictHostKeyChecking=no' '-oUserKnownHostsFile=/dev/null' '-oBatchMode=yes' -i /myIdentity -p 9222 'me@localhost' arg1 arg2"); } }