Skip to content

Commit

Permalink
save local file paths in bookmark (#204)
Browse files Browse the repository at this point in the history
* fix: renamed Bookmark 'directory' to 'remote_path' (keep name in file)

* feat: local_path as file transfer parameter and in bookmark
  • Loading branch information
veeso authored Jul 6, 2023
1 parent ee28d34 commit ca005cb
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 98 deletions.
19 changes: 14 additions & 5 deletions src/activity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Deps
// Namespaces
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::time::Duration;

use remotefs_ssh::SshKeyStorage as SshKeyStorageTrait;
Expand Down Expand Up @@ -34,12 +34,11 @@ pub enum NextActivity {
pub struct ActivityManager {
context: Option<Context>,
ticks: Duration,
local_dir: PathBuf,
}

impl ActivityManager {
/// Initializes a new Activity Manager
pub fn new(local_dir: &Path, ticks: Duration) -> Result<ActivityManager, HostError> {
pub fn new(ticks: Duration) -> Result<ActivityManager, HostError> {
// Prepare Context
// Initialize configuration client
let (config_client, error_config): (ConfigClient, Option<String>) =
Expand All @@ -59,7 +58,6 @@ impl ActivityManager {
let ctx: Context = Context::new(bookmarks_client, config_client, theme_provider, error);
Ok(ActivityManager {
context: Some(ctx),
local_dir: local_dir.to_path_buf(),
ticks,
})
}
Expand Down Expand Up @@ -243,8 +241,19 @@ impl ActivityManager {
return None;
}
};

// get local path:
// - if set in file transfer params, get it from there
// - otherwise is env current dir
// - otherwise is /
let local_wrkdir = ft_params
.local_path
.clone()
.or(std::env::current_dir().ok())
.unwrap_or(PathBuf::from("/"));

// Prepare activity
let host: Localhost = match Localhost::new(self.local_dir.clone()) {
let host: Localhost = match Localhost::new(local_wrkdir) {
Ok(host) => host,
Err(err) => {
// Set error in context
Expand Down
85 changes: 64 additions & 21 deletions src/config/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ pub struct Bookmark {
pub username: Option<String>,
/// Password is optional; base64, aes-128 encrypted password
pub password: Option<String>,
/// Remote folder to connect to
pub directory: Option<PathBuf>,
/// Remote folder to connect to (serde rename for legacy reasons)
#[serde(rename = "directory")]
pub remote_path: Option<PathBuf>,
/// local folder to open at startup
pub local_path: Option<PathBuf>,
/// S3 params; optional. When used other fields are empty for sure
pub s3: Option<S3Params>,
/// SMB params; optional. Extra params required for SMB protocol
Expand Down Expand Up @@ -71,7 +74,8 @@ pub struct SmbParams {
impl From<FileTransferParams> for Bookmark {
fn from(params: FileTransferParams) -> Self {
let protocol = params.protocol;
let directory = params.entry_directory;
let remote_path = params.remote_path;
let local_path = params.local_path;
// Create generic or others
match params.params {
ProtocolParams::Generic(params) => Self {
Expand All @@ -80,7 +84,8 @@ impl From<FileTransferParams> for Bookmark {
port: Some(params.port),
username: params.username,
password: params.password,
directory,
remote_path,
local_path,
s3: None,
smb: None,
},
Expand All @@ -90,7 +95,8 @@ impl From<FileTransferParams> for Bookmark {
port: None,
username: None,
password: None,
directory,
remote_path,
local_path,
s3: Some(S3Params::from(params)),
smb: None,
},
Expand All @@ -104,7 +110,8 @@ impl From<FileTransferParams> for Bookmark {
port: None,
username: params.username,
password: params.password,
directory,
remote_path,
local_path,
s3: None,
},
}
Expand Down Expand Up @@ -155,7 +162,8 @@ impl From<Bookmark> for FileTransferParams {
Self::new(bookmark.protocol, ProtocolParams::Smb(params))
}
}
.entry_directory(bookmark.directory) // Set entry directory
.remote_path(bookmark.remote_path) // Set entry remote_path
.local_path(bookmark.local_path) // Set entry local path
}
}

Expand Down Expand Up @@ -246,7 +254,8 @@ mod tests {
protocol: FileTransferProtocol::Sftp,
username: Some(String::from("root")),
password: Some(String::from("password")),
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: None,
};
Expand All @@ -256,7 +265,8 @@ mod tests {
protocol: FileTransferProtocol::Scp,
username: Some(String::from("admin")),
password: Some(String::from("password")),
directory: Some(PathBuf::from("/home")),
remote_path: Some(PathBuf::from("/home")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: None,
};
Expand All @@ -273,9 +283,13 @@ mod tests {
assert_eq!(bookmark.username.as_deref().unwrap(), "root");
assert_eq!(bookmark.password.as_deref().unwrap(), "password");
assert_eq!(
bookmark.directory.as_deref().unwrap(),
bookmark.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert_eq!(
bookmark.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
let bookmark: &Bookmark = hosts
.recents
.get(&String::from("ISO20201218T181432"))
Expand All @@ -286,9 +300,13 @@ mod tests {
assert_eq!(bookmark.username.as_deref().unwrap(), "admin");
assert_eq!(bookmark.password.as_deref().unwrap(), "password");
assert_eq!(
bookmark.directory.as_deref().unwrap(),
bookmark.remote_path.as_deref().unwrap(),
std::path::Path::new("/home")
);
assert_eq!(
bookmark.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
}

#[test]
Expand All @@ -300,17 +318,22 @@ mod tests {
password: Some(String::from("omar")),
});
let params: FileTransferParams = FileTransferParams::new(FileTransferProtocol::Scp, params)
.entry_directory(Some(PathBuf::from("/home")));
.remote_path(Some(PathBuf::from("/home")))
.local_path(Some(PathBuf::from("/tmp")));
let bookmark = Bookmark::from(params);
assert_eq!(bookmark.protocol, FileTransferProtocol::Scp);
assert_eq!(bookmark.address.as_deref().unwrap(), "127.0.0.1");
assert_eq!(bookmark.port.unwrap(), 10222);
assert_eq!(bookmark.username.as_deref().unwrap(), "root");
assert_eq!(bookmark.password.as_deref().unwrap(), "omar");
assert_eq!(
bookmark.directory.as_deref().unwrap(),
bookmark.remote_path.as_deref().unwrap(),
std::path::Path::new("/home")
);
assert_eq!(
bookmark.local_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert!(bookmark.s3.is_none());
}

Expand Down Expand Up @@ -345,16 +368,21 @@ mod tests {
protocol: FileTransferProtocol::Sftp,
username: Some(String::from("root")),
password: Some(String::from("password")),
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: None,
};
let params = FileTransferParams::from(bookmark);
assert_eq!(params.protocol, FileTransferProtocol::Sftp);
assert_eq!(
params.entry_directory.as_deref().unwrap(),
params.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert_eq!(
params.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
let gparams = params.params.generic_params().unwrap();
assert_eq!(gparams.address.as_str(), "192.168.1.1");
assert_eq!(gparams.port, 22);
Expand All @@ -370,7 +398,8 @@ mod tests {
port: None,
username: None,
password: None,
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: Some(S3Params {
bucket: String::from("veeso"),
region: Some(String::from("eu-west-1")),
Expand All @@ -385,9 +414,13 @@ mod tests {
let params = FileTransferParams::from(bookmark);
assert_eq!(params.protocol, FileTransferProtocol::AwsS3);
assert_eq!(
params.entry_directory.as_deref().unwrap(),
params.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert_eq!(
params.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
let gparams = params.params.s3_params().unwrap();
assert_eq!(gparams.bucket_name.as_str(), "veeso");
assert_eq!(gparams.region.as_deref().unwrap(), "eu-west-1");
Expand All @@ -407,7 +440,8 @@ mod tests {
port: Some(445),
username: Some("foo".to_string()),
password: Some("bar".to_string()),
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: Some(SmbParams {
share: "test".to_string(),
Expand All @@ -418,9 +452,13 @@ mod tests {
let params = FileTransferParams::from(bookmark);
assert_eq!(params.protocol, FileTransferProtocol::Smb);
assert_eq!(
params.entry_directory.as_deref().unwrap(),
params.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert_eq!(
params.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
let smb_params = params.params.smb_params().unwrap();
assert_eq!(smb_params.address.as_str(), "localhost");
assert_eq!(smb_params.port, 445);
Expand All @@ -439,7 +477,8 @@ mod tests {
port: Some(445),
username: None,
password: None,
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: Some(SmbParams {
share: "test".to_string(),
Expand All @@ -450,9 +489,13 @@ mod tests {
let params = FileTransferParams::from(bookmark);
assert_eq!(params.protocol, FileTransferProtocol::Smb);
assert_eq!(
params.entry_directory.as_deref().unwrap(),
params.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
assert_eq!(
params.local_path.as_deref().unwrap(),
std::path::Path::new("/usr")
);
let smb_params = params.params.smb_params().unwrap();
assert_eq!(smb_params.address.as_str(), "localhost");
assert_eq!(smb_params.share.as_str(), "test");
Expand Down
19 changes: 12 additions & 7 deletions src/config/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ mod tests {
assert_eq!(host.username.as_deref().unwrap(), "cvisintin");
assert_eq!(host.password.as_deref().unwrap(), "mysecret");
assert_eq!(
host.directory.as_deref().unwrap(),
host.remote_path.as_deref().unwrap(),
std::path::Path::new("/tmp")
);
let host: &Bookmark = hosts.bookmarks.get("aws-server-prod1").unwrap();
Expand Down Expand Up @@ -441,7 +441,8 @@ mod tests {
protocol: FileTransferProtocol::Sftp,
username: Some(String::from("root")),
password: None,
directory: None,
remote_path: None,
local_path: None,
s3: None,
smb: None,
},
Expand All @@ -454,7 +455,8 @@ mod tests {
protocol: FileTransferProtocol::Sftp,
username: Some(String::from("cvisintin")),
password: Some(String::from("password")),
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: None,
},
Expand All @@ -467,7 +469,8 @@ mod tests {
protocol: FileTransferProtocol::AwsS3,
username: None,
password: None,
directory: None,
remote_path: None,
local_path: None,
s3: Some(S3Params {
bucket: "veeso".to_string(),
region: Some("eu-west-1".to_string()),
Expand All @@ -492,7 +495,8 @@ mod tests {
protocol: FileTransferProtocol::Smb,
username: None,
password: None,
directory: None,
remote_path: None,
local_path: None,
s3: None,
smb: smb_params,
},
Expand All @@ -506,7 +510,8 @@ mod tests {
protocol: FileTransferProtocol::Scp,
username: Some(String::from("omar")),
password: Some(String::from("aaa")),
directory: Some(PathBuf::from("/tmp")),
remote_path: Some(PathBuf::from("/tmp")),
local_path: Some(PathBuf::from("/usr")),
s3: None,
smb: None,
},
Expand Down Expand Up @@ -547,7 +552,7 @@ mod tests {
let file_content: &str = r#"
[bookmarks]
raspberrypi2 = { address = "192.168.1.31", port = 22, protocol = "SFTP", username = "root", password = "mypassword" }
msi-estrem = { address = "192.168.1.30", port = 22, protocol = "SFTP", username = "cvisintin", password = "mysecret", directory = "/tmp" }
msi-estrem = { address = "192.168.1.30", port = 22, protocol = "SFTP", username = "cvisintin", password = "mysecret", directory = "/tmp", local_path = "/usr" }
aws-server-prod1 = { address = "51.23.67.12", port = 21, protocol = "FTPS", username = "aws001" }
[bookmarks.my-bucket]
Expand Down
Loading

0 comments on commit ca005cb

Please sign in to comment.