Start ssh agent only when needed
This commit is contained in:
parent
df26eb435f
commit
952ca847cd
44
src/main.rs
44
src/main.rs
@ -170,6 +170,30 @@ where
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
macro_rules! start_ssh_agent {
|
||||||
|
() => {
|
||||||
|
self.start_ssh_agent(&logger)?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! ssh {
|
||||||
|
($($field:ident $(: $value:expr)?$(,)? )*) => {{
|
||||||
|
start_ssh_agent!();
|
||||||
|
ShellCommand::Ssh {
|
||||||
|
$( $field $(: $value)? ),*
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! scp {
|
||||||
|
($($field:ident $(: $value:expr)?$(,)? )*) => {{
|
||||||
|
start_ssh_agent!();
|
||||||
|
ShellCommand::Scp {
|
||||||
|
$( $field $(: $value)? ),*
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
let mut configured_servers = LazyCell::new(|| self.parse_server_configuration_from_env());
|
let mut configured_servers = LazyCell::new(|| self.parse_server_configuration_from_env());
|
||||||
let servers = args
|
let servers = args
|
||||||
.servers
|
.servers
|
||||||
@ -223,8 +247,6 @@ where
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.start_ssh_agent(&logger)?;
|
|
||||||
|
|
||||||
//make sure files exist
|
//make sure files exist
|
||||||
match &file_server {
|
match &file_server {
|
||||||
Some(file_server) => match &file_server.address {
|
Some(file_server) => match &file_server.address {
|
||||||
@ -233,7 +255,7 @@ where
|
|||||||
files = files
|
files = files
|
||||||
.iter()
|
.iter()
|
||||||
.map(|file| {
|
.map(|file| {
|
||||||
let output = ShellCommand::Ssh {
|
let output = ssh! {
|
||||||
address: ssh_address.to_string(),
|
address: ssh_address.to_string(),
|
||||||
server_command: ServerCommand::Realpath {
|
server_command: ServerCommand::Realpath {
|
||||||
path: file_server.server_directory_path.join(file),
|
path: file_server.server_directory_path.join(file),
|
||||||
@ -313,7 +335,7 @@ where
|
|||||||
server,
|
server,
|
||||||
actions: {
|
actions: {
|
||||||
let present_file_names: Vec<OsString> = match &server.address {
|
let present_file_names: Vec<OsString> = match &server.address {
|
||||||
ServerAddress::Ssh { ssh_address } => ShellCommand::Ssh {
|
ServerAddress::Ssh { ssh_address } => ssh! {
|
||||||
address: ssh_address.to_string(),
|
address: ssh_address.to_string(),
|
||||||
server_command: ServerCommand::Ls {
|
server_command: ServerCommand::Ls {
|
||||||
dir: working_directory.clone(),
|
dir: working_directory.clone(),
|
||||||
@ -472,7 +494,7 @@ where
|
|||||||
None => ScpParam::from(file_action.file.as_path()),
|
None => ScpParam::from(file_action.file.as_path()),
|
||||||
};
|
};
|
||||||
let destination = ScpParam::from((server, &server_actions.working_directory));
|
let destination = ScpParam::from((server, &server_actions.working_directory));
|
||||||
ShellCommand::Scp {
|
scp! {
|
||||||
source,
|
source,
|
||||||
destination,
|
destination,
|
||||||
}
|
}
|
||||||
@ -484,7 +506,7 @@ where
|
|||||||
}
|
}
|
||||||
Action::Delete => match &server.address {
|
Action::Delete => match &server.address {
|
||||||
ServerAddress::Ssh { ssh_address } => {
|
ServerAddress::Ssh { ssh_address } => {
|
||||||
ShellCommand::Ssh {
|
ssh! {
|
||||||
address: ssh_address.to_string(),
|
address: ssh_address.to_string(),
|
||||||
server_command: ServerCommand::Rm {
|
server_command: ServerCommand::Rm {
|
||||||
file: server_actions.working_directory.join(&file_action.file),
|
file: server_actions.working_directory.join(&file_action.file),
|
||||||
@ -503,7 +525,7 @@ where
|
|||||||
},
|
},
|
||||||
Action::Rename { new_name } => match &server.address {
|
Action::Rename { new_name } => match &server.address {
|
||||||
ServerAddress::Ssh { ssh_address } => {
|
ServerAddress::Ssh { ssh_address } => {
|
||||||
ShellCommand::Ssh {
|
ssh! {
|
||||||
address: ssh_address.to_string(),
|
address: ssh_address.to_string(),
|
||||||
server_command: ServerCommand::Mv {
|
server_command: ServerCommand::Mv {
|
||||||
source: server_actions.working_directory.join(&file_action.file),
|
source: server_actions.working_directory.join(&file_action.file),
|
||||||
@ -529,13 +551,12 @@ where
|
|||||||
log!(logger, "Done!");
|
log!(logger, "Done!");
|
||||||
}
|
}
|
||||||
Command::Command { command } => {
|
Command::Command { command } => {
|
||||||
self.start_ssh_agent(&logger)?;
|
|
||||||
Self::require_non_empty_servers(&servers)?;
|
Self::require_non_empty_servers(&servers)?;
|
||||||
for server in servers {
|
for server in servers {
|
||||||
log!(logger, "Running command on '{}'...", server.get_name());
|
log!(logger, "Running command on '{}'...", server.get_name());
|
||||||
match &server.address {
|
match &server.address {
|
||||||
ServerAddress::Ssh { ssh_address } => {
|
ServerAddress::Ssh { ssh_address } => {
|
||||||
ShellCommand::Ssh {
|
ssh! {
|
||||||
address: ssh_address.to_string(),
|
address: ssh_address.to_string(),
|
||||||
server_command: ServerCommand::Execute {
|
server_command: ServerCommand::Execute {
|
||||||
working_directory: server.server_directory_path.clone(),
|
working_directory: server.server_directory_path.clone(),
|
||||||
@ -625,12 +646,11 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
Self::require_non_empty_servers(&servers)?;
|
Self::require_non_empty_servers(&servers)?;
|
||||||
self.start_ssh_agent(&logger)?;
|
|
||||||
|
|
||||||
for server in servers {
|
for server in servers {
|
||||||
log!(logger, "Getting file from {}...", server.get_name());
|
log!(logger, "Getting file from {}...", server.get_name());
|
||||||
let source = ScpParam::from((&server, server.server_directory_path.join(&file)));
|
let source = ScpParam::from((&server, server.server_directory_path.join(&file)));
|
||||||
ShellCommand::Scp {
|
scp! {
|
||||||
source: source.clone(),
|
source: source.clone(),
|
||||||
destination: ScpParam::from(download_directory.as_path()),
|
destination: ScpParam::from(download_directory.as_path()),
|
||||||
}
|
}
|
||||||
@ -657,7 +677,7 @@ where
|
|||||||
.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
|
||||||
ShellCommand::Scp {
|
scp! {
|
||||||
source: ScpParam::from(download_directory.join(file_name).as_path()),
|
source: ScpParam::from(download_directory.join(file_name).as_path()),
|
||||||
destination: source,
|
destination: source,
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user