From 4ff7fc079cc185733acfb4c88fefc755f48ec3d5 Mon Sep 17 00:00:00 2001 From: veeso Date: Thu, 25 Feb 2021 14:27:34 +0100 Subject: [PATCH] Added CLI options to set starting working directory on both local and remote hosts --- CHANGELOG.md | 1 + README.md | 4 ++ src/activity_manager.rs | 3 ++ src/main.rs | 21 +++++++++- .../activities/filetransfer_activity/mod.rs | 1 + .../filetransfer_activity/session.rs | 38 ++++++++++++------- 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9df2260..191af5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Released on ??? - Enhancements: - Default choice for deleting file set to "NO" (way too easy to delete files by mistake) + - Added CLI options to set starting workind directory on both local and remote hosts ## 0.3.2 diff --git a/README.md b/README.md index ef02e87..34e810a 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ brew install termscp TermSCP can be started with the following options: +`termscp [options]... [protocol://user@address:port] [local-wrkdir] [remote-wrkdir]` + - `-P, --password ` if address is provided, password will be this argument - `-v, --version` Print version info - `-h, --help` Print help page @@ -171,6 +173,8 @@ TermSCP can be started in two different mode, if no extra arguments is provided, Alternatively, the user can provide an address as argument to skip the authentication form and starting directly the connection to the remote server. +If address argument is provided you can also provide the start working directory for both local and remote hosts. + ### Address argument 🌎 The address argument has the following syntax: diff --git a/src/activity_manager.rs b/src/activity_manager.rs index 130381e..9e1fe57 100644 --- a/src/activity_manager.rs +++ b/src/activity_manager.rs @@ -84,6 +84,7 @@ impl ActivityManager { protocol: FileTransferProtocol, username: Option, password: Option, + entry_directory: Option, ) { self.ftparams = Some(FileTransferParams { address, @@ -91,6 +92,7 @@ impl ActivityManager { protocol, username, password, + entry_directory, }); } @@ -164,6 +166,7 @@ impl ActivityManager { _ => Some(activity.password.clone()), }, protocol: activity.protocol, + entry_directory: None, // Has use only when accessing with address }); break; } diff --git a/src/main.rs b/src/main.rs index 76d4580..7488b6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,9 @@ use filetransfer::FileTransferProtocol; /// Print usage fn print_usage(opts: Options) { - let brief = String::from("Usage: termscp [options]... [protocol://user@address:port]"); + let brief = String::from( + "Usage: termscp [options]... [protocol://user@address:port] [local-wrkdir] [remote-wrkdir]", + ); print!("{}", opts.usage(&brief)); println!("\nPlease, report issues to "); } @@ -120,6 +122,7 @@ fn main() { } // Check free args let extra_args: Vec = matches.free; + // Remote argument if let Some(remote) = extra_args.get(0) { // Parse address match utils::parser::parse_remote_opt(remote) { @@ -137,6 +140,20 @@ fn main() { } } } + // Local directory + if let Some(localdir) = extra_args.get(1) { + // Change working directory if local dir is set + let localdir: PathBuf = PathBuf::from(localdir); + if let Err(err) = env::set_current_dir(localdir.as_path()) { + eprintln!("Bad working directory argument: {}", err); + std::process::exit(255); + } + } + // Remote directory + let remote_wrkdir: Option = match extra_args.get(2) { + Some(p) => Some(PathBuf::from(p)), + None => None, + }; // Get working directory let wrkdir: PathBuf = match env::current_dir() { Ok(dir) => dir, @@ -174,7 +191,7 @@ fn main() { }; // Set file transfer params if set if let Some(address) = address { - manager.set_filetransfer_params(address, port, protocol, username, password); + manager.set_filetransfer_params(address, port, protocol, username, password, remote_wrkdir); } // Run manager.run(start_activity); diff --git a/src/ui/activities/filetransfer_activity/mod.rs b/src/ui/activities/filetransfer_activity/mod.rs index 727e9eb..62ebe4b 100644 --- a/src/ui/activities/filetransfer_activity/mod.rs +++ b/src/ui/activities/filetransfer_activity/mod.rs @@ -69,6 +69,7 @@ pub struct FileTransferParams { pub protocol: FileTransferProtocol, pub username: Option, pub password: Option, + pub entry_directory: Option, } /// ### InputField diff --git a/src/ui/activities/filetransfer_activity/session.rs b/src/ui/activities/filetransfer_activity/session.rs index 0d60f49..ff508e4 100644 --- a/src/ui/activities/filetransfer_activity/session.rs +++ b/src/ui/activities/filetransfer_activity/session.rs @@ -67,6 +67,14 @@ impl FileTransferActivity { .as_ref(), ); } + // Try to change directory to entry directory + let mut remote_chdir: Option = None; + if let Some(entry_directory) = &self.params.entry_directory { + remote_chdir = Some(entry_directory.clone()); + } + if let Some(entry_directory) = remote_chdir { + self.remote_changedir(entry_directory.as_path(), false); + } // Set state to explorer self.popup = None; self.reload_remote_dir(); @@ -607,13 +615,14 @@ impl FileTransferActivity { // Restore index self.local.set_abs_index(prev_index); // Set index; keep if possible, otherwise set to last item - self.local.set_abs_index(match self.local.get_current_file() { - Some(_) => self.local.get_index(), - None => match self.local.count() { - 0 => 0, - _ => self.local.count() - 1, - }, - }); + self.local + .set_abs_index(match self.local.get_current_file() { + Some(_) => self.local.get_index(), + None => match self.local.count() { + 0 => 0, + _ => self.local.count() - 1, + }, + }); } Err(err) => { self.log_and_alert( @@ -636,13 +645,14 @@ impl FileTransferActivity { // Restore index self.remote.set_abs_index(prev_index); // Set index; keep if possible, otherwise set to last item - self.remote.set_abs_index(match self.remote.get_current_file() { - Some(_) => self.remote.get_index(), - None => match self.remote.count() { - 0 => 0, - _ => self.remote.count() - 1, - }, - }); + self.remote + .set_abs_index(match self.remote.get_current_file() { + Some(_) => self.remote.get_index(), + None => match self.remote.count() { + 0 => 0, + _ => self.remote.count() - 1, + }, + }); } Err(err) => { self.log_and_alert(