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