mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
feat: host bridge builder and params
This commit is contained in:
@@ -9,8 +9,8 @@ use std::time::Duration;
|
||||
|
||||
use remotefs_ssh::SshKeyStorage as SshKeyStorageTrait;
|
||||
|
||||
use crate::filetransfer::{FileTransferParams, FileTransferProtocol};
|
||||
use crate::host::{HostError, Localhost};
|
||||
use crate::filetransfer::{FileTransferParams, FileTransferProtocol, HostBridgeParams};
|
||||
use crate::host::HostError;
|
||||
use crate::system::bookmarks_client::BookmarksClient;
|
||||
use crate::system::config_client::ConfigClient;
|
||||
use crate::system::environment;
|
||||
@@ -226,7 +226,7 @@ impl ActivityManager {
|
||||
fn run_filetransfer(&mut self) -> Option<NextActivity> {
|
||||
info!("Starting FileTransferActivity");
|
||||
// Get context
|
||||
let mut ctx: Context = match self.context.take() {
|
||||
let ctx: Context = match self.context.take() {
|
||||
Some(ctx) => ctx,
|
||||
None => {
|
||||
error!("Failed to start FileTransferActivity: context is None");
|
||||
@@ -234,7 +234,7 @@ impl ActivityManager {
|
||||
}
|
||||
};
|
||||
// If ft params is None, return None
|
||||
let ft_params: &FileTransferParams = match ctx.ft_params() {
|
||||
let remote_params: &FileTransferParams = match ctx.ft_params() {
|
||||
Some(ft_params) => ft_params,
|
||||
None => {
|
||||
error!("Failed to start FileTransferActivity: file transfer params is None");
|
||||
@@ -246,24 +246,17 @@ impl ActivityManager {
|
||||
// - if set in file transfer params, get it from there
|
||||
// - otherwise is env current dir
|
||||
// - otherwise is /
|
||||
let local_wrkdir = ft_params
|
||||
let local_wrkdir = remote_params
|
||||
.local_path
|
||||
.clone()
|
||||
.or(std::env::current_dir().ok())
|
||||
.unwrap_or(PathBuf::from("/"));
|
||||
|
||||
// Prepare activity
|
||||
let host: Localhost = match Localhost::new(local_wrkdir) {
|
||||
Ok(host) => host,
|
||||
Err(err) => {
|
||||
// Set error in context
|
||||
error!("Failed to initialize localhost: {}", err);
|
||||
ctx.set_error(format!("Could not initialize localhost: {err}"));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
// TODO: get host params from prev activity
|
||||
let host_bridge_params = HostBridgeParams::Localhost(local_wrkdir);
|
||||
|
||||
let mut activity: FileTransferActivity =
|
||||
FileTransferActivity::new(host, ft_params, self.ticks);
|
||||
FileTransferActivity::new(host_bridge_params, remote_params, self.ticks);
|
||||
// Prepare result
|
||||
let result: Option<NextActivity>;
|
||||
// Create activity
|
||||
|
||||
21
src/filetransfer/host_bridge_builder.rs
Normal file
21
src/filetransfer/host_bridge_builder.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use super::{HostBridgeParams, RemoteFsBuilder};
|
||||
use crate::host::{HostBridge, Localhost, RemoteBridged};
|
||||
use crate::system::config_client::ConfigClient;
|
||||
|
||||
pub struct HostBridgeBuilder;
|
||||
|
||||
impl HostBridgeBuilder {
|
||||
/// Build Host Bridge from parms
|
||||
///
|
||||
/// if protocol and parameters are inconsistent, the function will panic.
|
||||
pub fn build(params: HostBridgeParams, config_client: &ConfigClient) -> Box<dyn HostBridge> {
|
||||
match params {
|
||||
HostBridgeParams::Localhost(path) => {
|
||||
Box::new(Localhost::new(path).expect("Failed to create Localhost"))
|
||||
}
|
||||
HostBridgeParams::Remote(protocol, params) => Box::new(RemoteBridged::from(
|
||||
RemoteFsBuilder::build(protocol, params, config_client),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,14 @@
|
||||
//!
|
||||
//! `filetransfer` is the module which provides the file transfer protocols and remotefs builders
|
||||
|
||||
mod builder;
|
||||
mod host_bridge_builder;
|
||||
pub mod params;
|
||||
mod remotefs_builder;
|
||||
|
||||
// -- export types
|
||||
pub use builder::Builder;
|
||||
pub use params::{FileTransferParams, ProtocolParams};
|
||||
pub use host_bridge_builder::HostBridgeBuilder;
|
||||
pub use params::{FileTransferParams, HostBridgeParams, ProtocolParams};
|
||||
pub use remotefs_builder::RemoteFsBuilder;
|
||||
|
||||
/// This enum defines the different transfer protocol available in termscp
|
||||
|
||||
|
||||
@@ -15,6 +15,15 @@ pub use self::smb::SmbParams;
|
||||
pub use self::webdav::WebDAVProtocolParams;
|
||||
use super::FileTransferProtocol;
|
||||
|
||||
/// Host bridge params
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum HostBridgeParams {
|
||||
/// Localhost with starting working directory
|
||||
Localhost(PathBuf),
|
||||
/// Remote host with protocol and file transfer params
|
||||
Remote(FileTransferProtocol, ProtocolParams),
|
||||
}
|
||||
|
||||
/// Holds connection parameters for file transfers
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FileTransferParams {
|
||||
|
||||
@@ -27,9 +27,9 @@ use crate::system::sshkey_storage::SshKeyStorage;
|
||||
use crate::utils::ssh as ssh_utils;
|
||||
|
||||
/// Remotefs builder
|
||||
pub struct Builder;
|
||||
pub struct RemoteFsBuilder;
|
||||
|
||||
impl Builder {
|
||||
impl RemoteFsBuilder {
|
||||
/// Build RemoteFs client from protocol and params.
|
||||
///
|
||||
/// if protocol and parameters are inconsistent, the function will panic.
|
||||
@@ -262,7 +262,7 @@ mod test {
|
||||
.session_token(Some("gerry-scotti")),
|
||||
);
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::AwsS3, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::AwsS3, params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -275,7 +275,7 @@ mod test {
|
||||
.password(Some("qwerty123")),
|
||||
);
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::Ftp(true), params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::Ftp(true), params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -288,7 +288,7 @@ mod test {
|
||||
client_key: Some("client_key".to_string()),
|
||||
});
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::Kube, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::Kube, params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -301,7 +301,7 @@ mod test {
|
||||
.password(Some("qwerty123")),
|
||||
);
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::Scp, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::Scp, params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -314,7 +314,7 @@ mod test {
|
||||
.password(Some("qwerty123")),
|
||||
);
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::Sftp, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::Sftp, params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -322,7 +322,7 @@ mod test {
|
||||
fn should_build_smb_fs() {
|
||||
let params = ProtocolParams::Smb(SmbParams::new("localhost", "share"));
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::Smb, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::Smb, params, &config_client);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -336,7 +336,7 @@ mod test {
|
||||
.password(Some("qwerty123")),
|
||||
);
|
||||
let config_client = get_config_client();
|
||||
let _ = Builder::build(FileTransferProtocol::AwsS3, params, &config_client);
|
||||
let _ = RemoteFsBuilder::build(FileTransferProtocol::AwsS3, params, &config_client);
|
||||
}
|
||||
|
||||
fn get_config_client() -> ConfigClient {
|
||||
@@ -13,6 +13,7 @@ use thiserror::Error;
|
||||
// Locals
|
||||
pub use self::bridge::HostBridge;
|
||||
pub use self::localhost::Localhost;
|
||||
pub use self::remote_bridged::RemoteBridged;
|
||||
|
||||
pub type HostResult<T> = Result<T, HostError>;
|
||||
|
||||
|
||||
@@ -31,8 +31,10 @@ use tuirealm::{Application, EventListenerCfg, NoUserEvent};
|
||||
use super::{Activity, Context, ExitReason};
|
||||
use crate::config::themes::Theme;
|
||||
use crate::explorer::{FileExplorer, FileSorting};
|
||||
use crate::filetransfer::{Builder, FileTransferParams};
|
||||
use crate::host::{HostBridge, Localhost};
|
||||
use crate::filetransfer::{
|
||||
FileTransferParams, HostBridgeBuilder, HostBridgeParams, RemoteFsBuilder,
|
||||
};
|
||||
use crate::host::HostBridge;
|
||||
use crate::system::config_client::ConfigClient;
|
||||
use crate::system::watcher::FsWatcher;
|
||||
|
||||
@@ -235,7 +237,11 @@ pub struct FileTransferActivity {
|
||||
|
||||
impl FileTransferActivity {
|
||||
/// Instantiates a new FileTransferActivity
|
||||
pub fn new(host: Localhost, params: &FileTransferParams, ticks: Duration) -> Self {
|
||||
pub fn new(
|
||||
host_bridge_params: HostBridgeParams,
|
||||
remote_params: &FileTransferParams,
|
||||
ticks: Duration,
|
||||
) -> Self {
|
||||
// Get config client
|
||||
let config_client: ConfigClient = Self::init_config_client();
|
||||
Self {
|
||||
@@ -247,8 +253,12 @@ impl FileTransferActivity {
|
||||
.default_input_listener(ticks),
|
||||
),
|
||||
redraw: true,
|
||||
host_bridge: Box::new(host),
|
||||
client: Builder::build(params.protocol, params.params.clone(), &config_client),
|
||||
host_bridge: HostBridgeBuilder::build(host_bridge_params, &config_client),
|
||||
client: RemoteFsBuilder::build(
|
||||
remote_params.protocol,
|
||||
remote_params.params.clone(),
|
||||
&config_client,
|
||||
),
|
||||
browser: Browser::new(&config_client),
|
||||
log_records: VecDeque::with_capacity(256), // 256 events is enough I guess
|
||||
walkdir: WalkdirStates::default(),
|
||||
|
||||
@@ -97,11 +97,6 @@ impl Context {
|
||||
|
||||
// -- error
|
||||
|
||||
/// Set context error
|
||||
pub fn set_error(&mut self, err: String) {
|
||||
self.error = Some(err);
|
||||
}
|
||||
|
||||
/// Get error message and remove it from the context
|
||||
pub fn error(&mut self) -> Option<String> {
|
||||
self.error.take()
|
||||
|
||||
Reference in New Issue
Block a user