Skip to content

Commit a996416

Browse files
committed
Refactor code for improved readability and consistency
1 parent 76c8418 commit a996416

22 files changed

Lines changed: 644 additions & 394 deletions

src/cli/commands/exec.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ use tracing::info;
33

44
use crate::cli::utils::fuzzy_select_connection;
55
use crate::config::AppConfig;
6-
use crate::services::SshService;
7-
use crate::services::transport::{pick_kind, RusshTransport, SubprocessTransport, TransportKind};
86
use crate::services::transport::types::SshTransport;
7+
use crate::services::transport::{pick_kind, RusshTransport, SubprocessTransport, TransportKind};
8+
use crate::services::SshService;
99

10-
pub async fn execute(
11-
target: String,
12-
command: Vec<String>,
13-
config: AppConfig,
14-
) -> Result<()> {
10+
pub async fn execute(target: String, command: Vec<String>, config: AppConfig) -> Result<()> {
1511
if command.is_empty() {
1612
bail!("no command supplied — use: bssh exec <target> -- <command...>");
1713
}
1814

1915
let ssh_service = SshService::new(config.clone())?;
20-
let mut conn_opt = ssh_service.get_connection(&target).await.unwrap_or_default();
16+
let mut conn_opt = ssh_service
17+
.get_connection(&target)
18+
.await
19+
.unwrap_or_default();
2120

2221
if conn_opt.is_none() {
2322
conn_opt = fuzzy_select_connection(&ssh_service, &target, "exec", true).await?;
@@ -38,13 +37,17 @@ pub async fn execute(
3837
match t.exec(&connection, &cmd_str).await {
3938
Err(crate::services::transport::TransportError::Fallback(e)) => {
4039
tracing::warn!("native exec fallback ({e}), retrying with subprocess");
41-
SubprocessTransport::new(config).exec(&connection, &cmd_str).await
40+
SubprocessTransport::new(config)
41+
.exec(&connection, &cmd_str)
42+
.await
4243
}
4344
other => other,
4445
}
4546
}
4647
TransportKind::Subprocess => {
47-
SubprocessTransport::new(config).exec(&connection, &cmd_str).await
48+
SubprocessTransport::new(config)
49+
.exec(&connection, &cmd_str)
50+
.await
4851
}
4952
}
5053
.map_err(|e| anyhow::anyhow!("{e}"))?;

src/cli/commands/forward.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@
99
use anyhow::{bail, Context, Result};
1010

1111
use crate::config::AppConfig;
12-
use crate::services::transport::{pick_kind, RusshTransport, SubprocessTransport, TransportKind};
1312
use crate::services::transport::types::SshTransport;
13+
use crate::services::transport::{pick_kind, RusshTransport, SubprocessTransport, TransportKind};
1414
use crate::services::SshService;
1515

1616
pub async fn execute(target: String, local: String, config: AppConfig) -> Result<()> {
1717
let (bind_host, bind_port, remote_host, remote_port) =
1818
parse_local_spec(&local).context("invalid -L spec")?;
1919

2020
let ssh_service = SshService::new(config.clone())?;
21-
let mut conn_opt = ssh_service.get_connection(&target).await.unwrap_or_default();
21+
let mut conn_opt = ssh_service
22+
.get_connection(&target)
23+
.await
24+
.unwrap_or_default();
2225
if conn_opt.is_none() {
23-
conn_opt = crate::cli::utils::fuzzy_select_connection(&ssh_service, &target, "forward", true).await?;
26+
conn_opt =
27+
crate::cli::utils::fuzzy_select_connection(&ssh_service, &target, "forward", true)
28+
.await?;
2429
}
2530
let connection = match conn_opt {
2631
Some(c) => c,
@@ -36,20 +41,35 @@ pub async fn execute(target: String, local: String, config: AppConfig) -> Result
3641
remote_host,
3742
remote_port,
3843
connection.name,
39-
match kind { TransportKind::Native => "native", TransportKind::Subprocess => "subprocess" }
44+
match kind {
45+
TransportKind::Native => "native",
46+
TransportKind::Subprocess => "subprocess",
47+
}
4048
);
4149
eprintln!(" Press Ctrl+C to stop.\n");
4250

4351
let handle = match kind {
4452
TransportKind::Native => {
4553
let t = RusshTransport::new(config);
46-
t.forward_local(&connection, &bind_host, bind_port, &remote_host, remote_port)
47-
.await
54+
t.forward_local(
55+
&connection,
56+
&bind_host,
57+
bind_port,
58+
&remote_host,
59+
remote_port,
60+
)
61+
.await
4862
}
4963
TransportKind::Subprocess => {
5064
let t = SubprocessTransport::new(config);
51-
t.forward_local(&connection, &bind_host, bind_port, &remote_host, remote_port)
52-
.await
65+
t.forward_local(
66+
&connection,
67+
&bind_host,
68+
bind_port,
69+
&remote_host,
70+
remote_port,
71+
)
72+
.await
5373
}
5474
}
5575
.map_err(|e| anyhow::anyhow!("{e}"))?;
@@ -74,7 +94,12 @@ fn parse_local_spec(spec: &str) -> Result<(String, u16, String, u16)> {
7494
let remote_port: u16 = remote_port_s
7595
.parse()
7696
.with_context(|| format!("invalid remote port '{remote_port_s}'"))?;
77-
Ok(("127.0.0.1".to_string(), bind_port, remote_host.to_string(), remote_port))
97+
Ok((
98+
"127.0.0.1".to_string(),
99+
bind_port,
100+
remote_host.to_string(),
101+
remote_port,
102+
))
78103
}
79104
[bind_addr, bind_port_s, remote_host, remote_port_s] => {
80105
let bind_port: u16 = bind_port_s
@@ -83,11 +108,14 @@ fn parse_local_spec(spec: &str) -> Result<(String, u16, String, u16)> {
83108
let remote_port: u16 = remote_port_s
84109
.parse()
85110
.with_context(|| format!("invalid remote port '{remote_port_s}'"))?;
86-
Ok((bind_addr.to_string(), bind_port, remote_host.to_string(), remote_port))
111+
Ok((
112+
bind_addr.to_string(),
113+
bind_port,
114+
remote_host.to_string(),
115+
remote_port,
116+
))
87117
}
88-
_ => bail!(
89-
"expected [bind_addr:]bind_port:remote_host:remote_port, got '{spec}'"
90-
),
118+
_ => bail!("expected [bind_addr:]bind_port:remote_host:remote_port, got '{spec}'"),
91119
}
92120
}
93121

src/cli/commands/proxy.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@ use crate::services::transport::types::SshTransport;
1717
use crate::services::transport::{pick_kind, RusshTransport, SubprocessTransport, TransportKind};
1818
use crate::services::SshService;
1919

20-
pub async fn execute(
21-
target: String,
22-
port: u16,
23-
bind: String,
24-
config: AppConfig,
25-
) -> Result<()> {
20+
pub async fn execute(target: String, port: u16, bind: String, config: AppConfig) -> Result<()> {
2621
let ssh_service = SshService::new(config.clone())?;
27-
let mut conn_opt = ssh_service.get_connection(&target).await.unwrap_or_default();
22+
let mut conn_opt = ssh_service
23+
.get_connection(&target)
24+
.await
25+
.unwrap_or_default();
2826
if conn_opt.is_none() {
29-
conn_opt = crate::cli::utils::fuzzy_select_connection(
30-
&ssh_service,
31-
&target,
32-
"proxy",
33-
true,
34-
)
35-
.await?;
27+
conn_opt = crate::cli::utils::fuzzy_select_connection(&ssh_service, &target, "proxy", true)
28+
.await?;
3629
}
3730
let connection = match conn_opt {
3831
Some(c) => c,

src/cli/commands/transfer.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ pub async fn execute_upload(
3636

3737
if recursive {
3838
if !local.is_dir() {
39-
bail!("--recursive requires a local directory, but '{}' is not a directory", local.display());
39+
bail!(
40+
"--recursive requires a local directory, but '{}' is not a directory",
41+
local.display()
42+
);
4043
}
4144
let progress: crate::services::transfer::ProgressFn = Box::new(|done, _total| {
4245
eprint!("\r uploaded {done} bytes total ");
@@ -47,7 +50,8 @@ pub async fn execute_upload(
4750
eprintln!();
4851
println!(
4952
"✅ Uploaded {} → {}:{remote} ({files} files, {bytes} bytes)",
50-
local.display(), connection.host
53+
local.display(),
54+
connection.host
5155
);
5256
} else {
5357
if local.is_dir() {
@@ -69,7 +73,11 @@ pub async fn execute_upload(
6973
.await?;
7074
eprintln!();
7175
info!("upload complete: {written} bytes");
72-
println!("✅ Uploaded {} → {}:{remote} ({written} bytes)", local.display(), connection.host);
76+
println!(
77+
"✅ Uploaded {} → {}:{remote} ({written} bytes)",
78+
local.display(),
79+
connection.host
80+
);
7381
}
7482
Ok(())
7583
}
@@ -110,7 +118,8 @@ pub async fn execute_download(
110118
eprintln!();
111119
println!(
112120
"✅ Downloaded {}:{remote} → {} ({files} files, {bytes} bytes)",
113-
connection.host, local.display()
121+
connection.host,
122+
local.display()
114123
);
115124
} else {
116125
let progress: crate::services::transfer::ProgressFn = Box::new(|done, total| {
@@ -126,7 +135,11 @@ pub async fn execute_download(
126135
.await?;
127136
eprintln!();
128137
info!("download complete: {read} bytes");
129-
println!("✅ Downloaded {}:{remote} → {} ({read} bytes)", connection.host, local.display());
138+
println!(
139+
"✅ Downloaded {}:{remote} → {} ({read} bytes)",
140+
connection.host,
141+
local.display()
142+
);
130143
}
131144
Ok(())
132145
}
@@ -138,7 +151,8 @@ async fn resolve_connection(
138151
) -> Result<crate::models::Connection> {
139152
let mut conn_opt = ssh_service.get_connection(target).await.unwrap_or_default();
140153
if conn_opt.is_none() {
141-
conn_opt = crate::cli::utils::fuzzy_select_connection(ssh_service, target, action, true).await?;
154+
conn_opt =
155+
crate::cli::utils::fuzzy_select_connection(ssh_service, target, action, true).await?;
142156
}
143157
match conn_opt {
144158
Some(c) => Ok(c),

0 commit comments

Comments
 (0)