Add proper logging #9

Merged
Mr_Steppy merged 6 commits from logging into master 2024-12-17 06:03:26 +01:00
Showing only changes of commit b88fabb725 - Show all commits

View File

@ -158,11 +158,7 @@ fn main() -> Result<(), String> {
no_confirm, no_confirm,
file_name, file_name,
} => { } => {
if servers.is_empty() { require_non_empty_servers(&servers)?;
println!("Please provide some servers to upload to. See --help");
return Ok(());
}
start_ssh_agent()?; start_ssh_agent()?;
let file_name_info = let file_name_info =
@ -248,13 +244,13 @@ fn main() -> Result<(), String> {
}) })
.collect::<Result<Vec<_>, String>>()?; .collect::<Result<Vec<_>, String>>()?;
println!("The following actions will be performed:"); log!(logger, "The following actions will be performed: ");
for server_actions in &actions { for server_actions in &actions {
println!("{server_actions}"); log!(logger, "{server_actions}");
} }
if !no_confirm { if !no_confirm {
print!("Continue? [Y|n] "); log!(logger, "Continue? [Y|n] ");
std::io::stdout().flush().expect("failed to flush stdout"); std::io::stdout().flush().expect("failed to flush stdout");
let mut buffer = String::new(); let mut buffer = String::new();
std::io::stdin() std::io::stdin()
@ -262,7 +258,7 @@ fn main() -> Result<(), String> {
.expect("failed to read stdin"); .expect("failed to read stdin");
match buffer.to_lowercase().trim() { match buffer.to_lowercase().trim() {
"n" | "no" => { "n" | "no" => {
println!("Aborting..."); log!(logger, "Aborting...");
return Ok(()); return Ok(());
} }
_ => {} _ => {}
@ -271,7 +267,7 @@ fn main() -> Result<(), String> {
for server_actions in actions { for server_actions in actions {
let server = server_actions.server; 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 { for file_action in server_actions.actions {
match file_action.kind { match file_action.kind {
Action::Add | Action::Replace => { Action::Add | Action::Replace => {
@ -312,12 +308,13 @@ fn main() -> Result<(), String> {
} }
} }
println!("Done!"); log!(logger, "Done!");
} }
Command::Command { command } => { Command::Command { command } => {
start_ssh_agent()?; start_ssh_agent()?;
require_non_empty_servers(&servers)?;
for server in servers { for server in servers {
println!("Running command on '{}'...", server.ssh_name); log!(logger, "Running command on '{}'...", server.ssh_name);
ShellCmd::new("ssh") ShellCmd::new("ssh")
.arg(server.ssh_name) .arg(server.ssh_name)
.arg(osf!("cd ") + server.server_directory_path + "; " + &command) .arg(osf!("cd ") + server.server_directory_path + "; " + &command)
@ -326,7 +323,7 @@ fn main() -> Result<(), String> {
.wait() .wait()
.map_err(|e| format!("failed to wait for ssh command completion: {e}"))?; .map_err(|e| format!("failed to wait for ssh command completion: {e}"))?;
} }
println!("Done!"); log!(logger, "Done!");
} }
Command::Editor { Command::Editor {
file, file,
@ -353,16 +350,17 @@ fn main() -> Result<(), String> {
.any(|entry| entry.file_name() == file_name) .any(|entry| entry.file_name() == file_name)
{ {
return Err(format!( 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(), file_name.to_string_lossy(),
working_directory.to_string_lossy() working_directory.to_string_lossy()
)); ));
} }
require_non_empty_servers(&servers)?;
start_ssh_agent()?; start_ssh_agent()?;
for server in servers { for server in servers {
println!("Downloading file from {}...", server.ssh_name); log!(logger, "Downloading file from {}...", server.ssh_name);
ShellCmd::new("scp") ShellCmd::new("scp")
.arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file)) .arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file))
.arg(&working_directory) .arg(&working_directory)
@ -390,13 +388,21 @@ fn main() -> Result<(), String> {
.map_err(|e| format!("failed to upload file again: {e}"))?; .map_err(|e| format!("failed to upload file again: {e}"))?;
} }
println!("Done!"); log!(logger, "Done!");
} }
} }
Ok(()) 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> { fn start_ssh_agent() -> Result<(), String> {
//start the ssh agent //start the ssh agent
let agent_output = ShellCmd::new("ssh-agent") let agent_output = ShellCmd::new("ssh-agent")