#![forbid(unsafe_code)]
#![allow(unused_imports)]
use super::*;
#[derive(Debug, Default, Clone)]
pub struct SftpOptions {
pub password: Option<secrecy::SecretString>,
pub key: Option<String>,
pub key_passphrase: Option<secrecy::SecretString>,
pub timeout: Option<crate::domain::TimeoutMs>,
pub replace_host_key: bool,
pub json: bool,
pub use_agent: bool,
pub agent_socket: Option<String>,
pub recursive: bool,
}
pub(crate) fn apply_sftp_options(record: &mut crate::vps::model::VpsRecord, opts: &SftpOptions) {
if let Some(ref pwd) = opts.password {
record.password = pwd.clone();
}
if let Some(ref k) = opts.key {
if let Ok(kp) = crate::domain::KeyPath::try_new(k.as_str()) {
record.key_path = Some(kp);
}
}
if let Some(ref kp) = opts.key_passphrase {
record.key_passphrase = Some(kp.clone());
}
if let Some(t) = opts.timeout {
record.timeout_ms = t;
}
if opts.use_agent {
record.use_agent = true;
}
if let Some(ref sock) = opts.agent_socket {
record.agent_socket = Some(sock.clone());
record.use_agent = true;
}
}
pub(crate) async fn connect_client(
vps_key: &str,
config_override: Option<&std::path::Path>,
opts: &SftpOptions,
) -> anyhow::Result<SshClient> {
let mut record = vps::find_by_name(config_override, vps_key)?
.ok_or_else(|| SshCliError::VpsNotFound(vps_key.to_owned()))?;
let _ = publish_target(vps_key);
apply_sftp_options(&mut record, opts);
let path = vps::resolve_config_path(config_override)?;
let cfg = vps::build_connection_config(&record, Some(&path), opts.replace_host_key);
let client = SshClient::connect(cfg).await?;
Ok(client)
}
pub(crate) fn remote_str(p: &Path) -> String {
p.to_string_lossy().into_owned()
}