Skip to content

Commit

Permalink
feat: host bridge builder and params
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Oct 5, 2024
1 parent 245799d commit cc6c4a8
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 38 deletions.
25 changes: 9 additions & 16 deletions src/activity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,15 +226,15 @@ 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");
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,
None => {
error!("Failed to start FileTransferActivity: file transfer params is None");
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/filetransfer/host_bridge_builder.rs
Original file line number Diff line number Diff line change
@@ -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),
)),
}
}
}
8 changes: 5 additions & 3 deletions src/filetransfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/filetransfer/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -314,15 +314,15 @@ 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]
#[cfg(smb)]
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]
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
20 changes: 15 additions & 5 deletions src/ui/activities/filetransfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
5 changes: 0 additions & 5 deletions src/ui/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cc6c4a8

Please sign in to comment.