mirror of
https://github.com/house-of-vanity/rexec.git
synced 2025-07-06 16:44:08 +00:00
Format Rust code using rustfmt
This commit is contained in:
committed by
GitHub
parent
6dc2ab74b6
commit
c562af6f85
89
src/main.rs
89
src/main.rs
@ -146,7 +146,7 @@ fn shorten_hostname(hostname: &str, common_suffix: &Option<String>) -> String {
|
|||||||
Some(suffix) if hostname.ends_with(suffix) => {
|
Some(suffix) if hostname.ends_with(suffix) => {
|
||||||
let short_name = hostname[..hostname.len() - suffix.len()].to_string();
|
let short_name = hostname[..hostname.len() - suffix.len()].to_string();
|
||||||
format!("{}{}", short_name, "*")
|
format!("{}{}", short_name, "*")
|
||||||
},
|
}
|
||||||
_ => hostname.to_string(),
|
_ => hostname.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,7 +281,13 @@ fn expand_string(s: &str) -> Vec<Host> {
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Result<i32, String>` - Exit code on success or error message
|
/// * `Result<i32, String>` - Exit code on success or error message
|
||||||
fn execute_ssh_command(hostname: &str, username: &str, command: &str, common_suffix: &Option<String>, code_only: bool) -> Result<i32, String> {
|
fn execute_ssh_command(
|
||||||
|
hostname: &str,
|
||||||
|
username: &str,
|
||||||
|
command: &str,
|
||||||
|
common_suffix: &Option<String>,
|
||||||
|
code_only: bool,
|
||||||
|
) -> Result<i32, String> {
|
||||||
let display_name = shorten_hostname(hostname, common_suffix);
|
let display_name = shorten_hostname(hostname, common_suffix);
|
||||||
|
|
||||||
// Display execution start message with shortened hostname
|
// Display execution start message with shortened hostname
|
||||||
@ -289,12 +295,15 @@ fn execute_ssh_command(hostname: &str, username: &str, command: &str, common_suf
|
|||||||
|
|
||||||
// Build the SSH command with appropriate options
|
// Build the SSH command with appropriate options
|
||||||
let mut ssh_cmd = Command::new("ssh");
|
let mut ssh_cmd = Command::new("ssh");
|
||||||
ssh_cmd.arg("-o").arg("StrictHostKeyChecking=no")
|
ssh_cmd
|
||||||
.arg("-o").arg("BatchMode=yes")
|
.arg("-o")
|
||||||
.arg(format!("{}@{}", username, hostname))
|
.arg("StrictHostKeyChecking=no")
|
||||||
.arg(command)
|
.arg("-o")
|
||||||
.stdout(Stdio::piped())
|
.arg("BatchMode=yes")
|
||||||
.stderr(Stdio::piped());
|
.arg(format!("{}@{}", username, hostname))
|
||||||
|
.arg(command)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped());
|
||||||
|
|
||||||
// Execute the command
|
// Execute the command
|
||||||
let mut child = match ssh_cmd.spawn() {
|
let mut child = match ssh_cmd.spawn() {
|
||||||
@ -314,9 +323,15 @@ fn execute_ssh_command(hostname: &str, username: &str, command: &str, common_suf
|
|||||||
match line {
|
match line {
|
||||||
Ok(line) => {
|
Ok(line) => {
|
||||||
if !code_only_stdout {
|
if !code_only_stdout {
|
||||||
println!("{} {} {} {}", prefix, display_name_stdout.yellow(), prefix, line);
|
println!(
|
||||||
|
"{} {} {} {}",
|
||||||
|
prefix,
|
||||||
|
display_name_stdout.yellow(),
|
||||||
|
prefix,
|
||||||
|
line
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -334,9 +349,15 @@ fn execute_ssh_command(hostname: &str, username: &str, command: &str, common_suf
|
|||||||
match line {
|
match line {
|
||||||
Ok(line) => {
|
Ok(line) => {
|
||||||
if !code_only_stderr {
|
if !code_only_stderr {
|
||||||
println!("{} {} {} {}", prefix, display_name_stderr.yellow(), prefix, line);
|
println!(
|
||||||
|
"{} {} {} {}",
|
||||||
|
prefix,
|
||||||
|
display_name_stderr.yellow(),
|
||||||
|
prefix,
|
||||||
|
line
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -361,7 +382,11 @@ fn execute_ssh_command(hostname: &str, username: &str, command: &str, common_suf
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Display completion message
|
// Display completion message
|
||||||
println!("{} - COMPLETED (Exit code: [{}])", display_name.yellow().bold(), code_string);
|
println!(
|
||||||
|
"{} - COMPLETED (Exit code: [{}])",
|
||||||
|
display_name.yellow().bold(),
|
||||||
|
code_string
|
||||||
|
);
|
||||||
|
|
||||||
Ok(exit_code)
|
Ok(exit_code)
|
||||||
}
|
}
|
||||||
@ -435,8 +460,9 @@ fn main() {
|
|||||||
// Results are stored with original indices to maintain order
|
// Results are stored with original indices to maintain order
|
||||||
let resolved_ips_with_indices = Arc::new(Mutex::new(Vec::<(String, IpAddr, usize)>::new()));
|
let resolved_ips_with_indices = Arc::new(Mutex::new(Vec::<(String, IpAddr, usize)>::new()));
|
||||||
|
|
||||||
host_with_indices.par_iter().for_each(|(host, idx)| {
|
host_with_indices
|
||||||
match lookup_host(&host.name) {
|
.par_iter()
|
||||||
|
.for_each(|(host, idx)| match lookup_host(&host.name) {
|
||||||
Ok(ips) if !ips.is_empty() => {
|
Ok(ips) if !ips.is_empty() => {
|
||||||
let ip = ips[0];
|
let ip = ips[0];
|
||||||
let mut results = resolved_ips_with_indices.lock().unwrap();
|
let mut results = resolved_ips_with_indices.lock().unwrap();
|
||||||
@ -444,14 +470,21 @@ fn main() {
|
|||||||
}
|
}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let mut results = resolved_ips_with_indices.lock().unwrap();
|
let mut results = resolved_ips_with_indices.lock().unwrap();
|
||||||
results.push((host.name.clone(), IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)), *idx));
|
results.push((
|
||||||
|
host.name.clone(),
|
||||||
|
IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
|
||||||
|
*idx,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let mut results = resolved_ips_with_indices.lock().unwrap();
|
let mut results = resolved_ips_with_indices.lock().unwrap();
|
||||||
results.push((host.name.clone(), IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)), *idx));
|
results.push((
|
||||||
|
host.name.clone(),
|
||||||
|
IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
|
||||||
|
*idx,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Sort hosts by original index to maintain consistent display order
|
// Sort hosts by original index to maintain consistent display order
|
||||||
let mut resolved_hosts = resolved_ips_with_indices.lock().unwrap().clone();
|
let mut resolved_hosts = resolved_ips_with_indices.lock().unwrap().clone();
|
||||||
@ -479,12 +512,18 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find common domain suffix to optimize display
|
// Find common domain suffix to optimize display
|
||||||
let hostnames: Vec<String> = valid_hosts.iter().map(|(hostname, _, _)| hostname.clone()).collect();
|
let hostnames: Vec<String> = valid_hosts
|
||||||
|
.iter()
|
||||||
|
.map(|(hostname, _, _)| hostname.clone())
|
||||||
|
.collect();
|
||||||
let common_suffix = find_common_suffix(&hostnames);
|
let common_suffix = find_common_suffix(&hostnames);
|
||||||
|
|
||||||
// Inform user about display optimization if common suffix found
|
// Inform user about display optimization if common suffix found
|
||||||
if let Some(suffix) = &common_suffix {
|
if let Some(suffix) = &common_suffix {
|
||||||
info!("Common domain suffix found: '{}' (will be displayed as '*')", suffix);
|
info!(
|
||||||
|
"Common domain suffix found: '{}' (will be displayed as '*')",
|
||||||
|
suffix
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ask for confirmation before proceeding (unless --noconfirm is specified)
|
// Ask for confirmation before proceeding (unless --noconfirm is specified)
|
||||||
@ -526,7 +565,13 @@ fn main() {
|
|||||||
|
|
||||||
// Execute SSH command in a separate thread
|
// Execute SSH command in a separate thread
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
match execute_ssh_command(&hostname, &username, &command, &common_suffix_clone, code_only) {
|
match execute_ssh_command(
|
||||||
|
&hostname,
|
||||||
|
&username,
|
||||||
|
&command,
|
||||||
|
&common_suffix_clone,
|
||||||
|
code_only,
|
||||||
|
) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => error!("Error executing command on {}: {}", hostname, e),
|
Err(e) => error!("Error executing command on {}: {}", hostname, e),
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user