Use run function for commands where possible
This commit is contained in:
parent
0c56e837d4
commit
fefdffe1e9
@ -7,6 +7,7 @@ use std::iter::once;
|
|||||||
use std::process::{Command, ExitStatus, Stdio};
|
use std::process::{Command, ExitStatus, Stdio};
|
||||||
|
|
||||||
pub trait LogRunnable {
|
pub trait LogRunnable {
|
||||||
|
//TODO add function to query output on success, could further improve code readability
|
||||||
fn run(&mut self, logger: &Logger) -> Result<(), SpecificExecutionError>;
|
fn run(&mut self, logger: &Logger) -> Result<(), SpecificExecutionError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
51
src/main.rs
51
src/main.rs
@ -1,11 +1,12 @@
|
|||||||
mod action;
|
mod action;
|
||||||
|
mod command;
|
||||||
mod file;
|
mod file;
|
||||||
mod logger;
|
mod logger;
|
||||||
mod os_string_builder;
|
mod os_string_builder;
|
||||||
mod server;
|
mod server;
|
||||||
mod command;
|
|
||||||
|
|
||||||
use crate::action::{Action, FileAction, ServerActions};
|
use crate::action::{Action, FileAction, ServerActions};
|
||||||
|
use crate::command::LogRunnable;
|
||||||
use crate::file::{FileMatcher, FileNameInfo};
|
use crate::file::{FileMatcher, FileNameInfo};
|
||||||
use crate::logger::{LogLevel, Logger};
|
use crate::logger::{LogLevel, Logger};
|
||||||
use crate::os_string_builder::ReplaceWithOsStr;
|
use crate::os_string_builder::ReplaceWithOsStr;
|
||||||
@ -160,7 +161,7 @@ fn main() -> Result<(), String> {
|
|||||||
file_name,
|
file_name,
|
||||||
} => {
|
} => {
|
||||||
require_non_empty_servers(&servers)?;
|
require_non_empty_servers(&servers)?;
|
||||||
start_ssh_agent()?;
|
start_ssh_agent(&logger)?;
|
||||||
|
|
||||||
let file_name_info =
|
let file_name_info =
|
||||||
FileNameInfo::try_from(file.clone()).map_err(|e| format!("bad file: {e}"))?;
|
FileNameInfo::try_from(file.clone()).map_err(|e| format!("bad file: {e}"))?;
|
||||||
@ -275,19 +276,15 @@ fn main() -> Result<(), String> {
|
|||||||
ShellCmd::new("scp")
|
ShellCmd::new("scp")
|
||||||
.arg(file.clone())
|
.arg(file.clone())
|
||||||
.arg(osf!(&server.ssh_name) + ":" + &server_actions.working_directory)
|
.arg(osf!(&server.ssh_name) + ":" + &server_actions.working_directory)
|
||||||
.spawn()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to upload file: {e}"))?
|
.map_err(|e| format!("upload failure: {e}"))?;
|
||||||
.wait()
|
|
||||||
.map_err(|e| format!("failed to wait for upload: {e}"))?;
|
|
||||||
}
|
}
|
||||||
Action::Delete => {
|
Action::Delete => {
|
||||||
ShellCmd::new("ssh")
|
ShellCmd::new("ssh")
|
||||||
.arg(&server.ssh_name)
|
.arg(&server.ssh_name)
|
||||||
.arg(osf!("cd ") + &server_actions.working_directory + "; rm " + &file_action.file)
|
.arg(osf!("cd ") + &server_actions.working_directory + "; rm " + &file_action.file)
|
||||||
.spawn()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to send delete command: {e}"))?
|
.map_err(|e| format!("failed to delete old version: {e}"))?;
|
||||||
.wait()
|
|
||||||
.map_err(|e| format!("failed to wait for delete command: {e}"))?;
|
|
||||||
}
|
}
|
||||||
Action::Rename { new_name } => {
|
Action::Rename { new_name } => {
|
||||||
ShellCmd::new("ssh")
|
ShellCmd::new("ssh")
|
||||||
@ -300,10 +297,8 @@ fn main() -> Result<(), String> {
|
|||||||
+ " "
|
+ " "
|
||||||
+ new_name,
|
+ new_name,
|
||||||
)
|
)
|
||||||
.spawn()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to send rename command: {e}"))?
|
.map_err(|e| format!("failed to rename: {e}"))?;
|
||||||
.wait()
|
|
||||||
.map_err(|e| format!("failed to wait for rename command: {e}"))?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,17 +307,15 @@ fn main() -> Result<(), String> {
|
|||||||
log!(logger, "Done!");
|
log!(logger, "Done!");
|
||||||
}
|
}
|
||||||
Command::Command { command } => {
|
Command::Command { command } => {
|
||||||
start_ssh_agent()?;
|
start_ssh_agent(&logger)?;
|
||||||
require_non_empty_servers(&servers)?;
|
require_non_empty_servers(&servers)?;
|
||||||
for server in servers {
|
for server in servers {
|
||||||
log!(logger, "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)
|
||||||
.spawn()
|
.run(&logger)
|
||||||
.map_err(|_| "failed to start ssh command".to_string())?
|
.map_err(|e| format!("{e}"))?;
|
||||||
.wait()
|
|
||||||
.map_err(|e| format!("failed to wait for ssh command completion: {e}"))?;
|
|
||||||
}
|
}
|
||||||
log!(logger, "Done!");
|
log!(logger, "Done!");
|
||||||
}
|
}
|
||||||
@ -358,15 +351,15 @@ fn main() -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
require_non_empty_servers(&servers)?;
|
require_non_empty_servers(&servers)?;
|
||||||
start_ssh_agent()?;
|
start_ssh_agent(&logger)?;
|
||||||
|
|
||||||
for server in servers {
|
for server in servers {
|
||||||
log!(logger, "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)
|
||||||
.status()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to download file: {e}"))?;
|
.map_err(|e| format!("download failure: {e}"))?;
|
||||||
|
|
||||||
//open file in editor
|
//open file in editor
|
||||||
let mut shell_args = shell_words::split(&editor)
|
let mut shell_args = shell_words::split(&editor)
|
||||||
@ -378,15 +371,15 @@ fn main() -> Result<(), String> {
|
|||||||
let command = shell_args.remove(0);
|
let command = shell_args.remove(0);
|
||||||
ShellCmd::new(command)
|
ShellCmd::new(command)
|
||||||
.args(shell_args)
|
.args(shell_args)
|
||||||
.status()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to open file in editor: {e}"))?;
|
.map_err(|e| format!("failed to open file in editor: {e}"))?;
|
||||||
|
|
||||||
//upload file again
|
//upload file again
|
||||||
ShellCmd::new("scp")
|
ShellCmd::new("scp")
|
||||||
.arg(working_directory.join(file_name))
|
.arg(working_directory.join(file_name))
|
||||||
.arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file))
|
.arg(osf!(&server.ssh_name) + ":" + server.server_directory_path.join(&file))
|
||||||
.status()
|
.run(&logger)
|
||||||
.map_err(|e| format!("failed to upload file again: {e}"))?;
|
.map_err(|e| format!("failed to re-upload file: {e}"))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
log!(logger, "Done!");
|
log!(logger, "Done!");
|
||||||
@ -404,7 +397,7 @@ fn require_non_empty_servers(servers: &[Server]) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_ssh_agent() -> Result<(), String> {
|
fn start_ssh_agent(logger: &Logger) -> Result<(), String> {
|
||||||
//start the ssh agent
|
//start the ssh agent
|
||||||
let agent_output = ShellCmd::new("ssh-agent")
|
let agent_output = ShellCmd::new("ssh-agent")
|
||||||
.arg("-s")
|
.arg("-s")
|
||||||
@ -424,11 +417,7 @@ fn start_ssh_agent() -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//add the ssh key
|
//add the ssh key
|
||||||
ShellCmd::new("ssh-add")
|
ShellCmd::new("ssh-add").run(logger).map_err(|e| format!("failed to add ssh-key: {e}"))?;
|
||||||
.spawn()
|
|
||||||
.map_err(|e| format!("failed to add ssh key: {}", e))?
|
|
||||||
.wait()
|
|
||||||
.expect("failed to wait on ssh-add");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user