Skip to content

Commit

Permalink
Support ssh port
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Jun 21, 2022
1 parent 1de4067 commit b3fbb27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct Cli {
/// the sshpass word to use
#[clap(long, requires = "ssh", conflicts_with = "adb_group")]
sshpass: Option<String>,

/// the ssh port to use
#[clap(short = 'p', long, requires = "ssh", conflicts_with = "adb_group")]
port: Option<u32>,
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -200,7 +204,7 @@ pub fn run() {
let cli = Cli::parse();

let remote_op: Box<dyn RemoteOp> = if let Some(ssh_uri) = &cli.ssh {
Box::new(Ssh::new(ssh_uri, cli.sshpass))
Box::new(Ssh::new(ssh_uri, cli.sshpass, cli.port))
} else {
Box::new(Adb::new(cli.serial, cli.tcp_device, cli.usb_device))
};
Expand Down
22 changes: 19 additions & 3 deletions src/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,37 @@ use anyhow::{Result, ensure};
pub struct Ssh {
uri: String,
pass: Option<String>,
port: Option<u32>,
}

impl Ssh {
pub fn new(uri: impl Into<String>, pass: Option<String>) -> Self {
pub fn new(uri: impl Into<String>, pass: Option<String>, port: Option<u32>) -> Self {
Ssh {
uri: uri.into(),
pass,
port
}
}

fn get_cmd_prefix(&self, cmd: &str) -> String {
let scp = cmd == "scp";
if let Some(pass) = &self.pass {
format!("sshpass -p {} {}", pass, cmd)
format!("sshpass -p {} {} {}", pass, cmd, self.get_port_str(scp))
} else {
cmd.to_string()
format!("{} {}", cmd, self.get_port_str(scp))
}
}

fn get_port_str(&self, scp: bool) -> String {
if let Some(port) = &self.port {
if scp {
// scp use -P port(Upercase P) while ssh use -p port(lowercase p)
format!("-P {}", port)
} else {
format!("-p {}", port)
}
} else {
"".to_string()
}
}
}
Expand Down

0 comments on commit b3fbb27

Please sign in to comment.