From b88fabb7251ea8060abc74757fa912bd3e785b55 Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Tue, 17 Dec 2024 06:02:25 +0100 Subject: [PATCH] replace print calls with log calls --- src/main.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 29d4982..b9ec631 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,11 +158,7 @@ fn main() -> Result<(), String> { no_confirm, file_name, } => { - if servers.is_empty() { - println!("Please provide some servers to upload to. See --help"); - return Ok(()); - } - + require_non_empty_servers(&servers)?; start_ssh_agent()?; let file_name_info = @@ -248,13 +244,13 @@ fn main() -> Result<(), String> { }) .collect::, String>>()?; - println!("The following actions will be performed:"); + log!(logger, "The following actions will be performed: "); for server_actions in &actions { - println!("{server_actions}"); + log!(logger, "{server_actions}"); } if !no_confirm { - print!("Continue? [Y|n] "); + log!(logger, "Continue? [Y|n] "); std::io::stdout().flush().expect("failed to flush stdout"); let mut buffer = String::new(); std::io::stdin() @@ -262,7 +258,7 @@ fn main() -> Result<(), String> { .expect("failed to read stdin"); match buffer.to_lowercase().trim() { "n" | "no" => { - println!("Aborting..."); + log!(logger, "Aborting..."); return Ok(()); } _ => {} @@ -271,7 +267,7 @@ fn main() -> Result<(), String> { for server_actions in actions { let server = server_actions.server; - println!("Performing actions on {}...", server.ssh_name); + log!(logger, "Performing actions on {}...", server.ssh_name); for file_action in server_actions.actions { match file_action.kind { Action::Add | Action::Replace => { @@ -312,12 +308,13 @@ fn main() -> Result<(), String> { } } - println!("Done!"); + log!(logger, "Done!"); } Command::Command { command } => { start_ssh_agent()?; + require_non_empty_servers(&servers)?; for server in servers { - println!("Running command on '{}'...", server.ssh_name); + log!(logger, "Running command on '{}'...", server.ssh_name); ShellCmd::new("ssh") .arg(server.ssh_name) .arg(osf!("cd ") + server.server_directory_path + "; " + &command) @@ -326,7 +323,7 @@ fn main() -> Result<(), String> { .wait() .map_err(|e| format!("failed to wait for ssh command completion: {e}"))?; } - println!("Done!"); + log!(logger, "Done!"); } Command::Editor { file, @@ -353,16 +350,17 @@ fn main() -> Result<(), String> { .any(|entry| entry.file_name() == file_name) { return Err(format!( - "A file with the name {} already exists in {}. You can override it with --override", + "A file with the name {} already exists in {}. You can override it with --override or -f", file_name.to_string_lossy(), working_directory.to_string_lossy() )); } + require_non_empty_servers(&servers)?; start_ssh_agent()?; for server in servers { - println!("Downloading file from {}...", server.ssh_name); + log!(logger, "Downloading file from {}...", server.ssh_name); ShellCmd::new("scp") .arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file)) .arg(&working_directory) @@ -390,13 +388,21 @@ fn main() -> Result<(), String> { .map_err(|e| format!("failed to upload file again: {e}"))?; } - println!("Done!"); + log!(logger, "Done!"); } } Ok(()) } +fn require_non_empty_servers(servers: &[Server]) -> Result<(), String> { + if servers.is_empty() { + Err("You did not provide any servers for this operation. Please see --help".to_string()) + } else { + Ok(()) + } +} + fn start_ssh_agent() -> Result<(), String> { //start the ssh agent let agent_output = ShellCmd::new("ssh-agent")