Added CLI options to set starting working directory on both local and remote hosts

This commit is contained in:
veeso
2021-02-25 14:27:34 +01:00
parent 7f24d6db5c
commit 4ff7fc079c
6 changed files with 52 additions and 16 deletions

View File

@@ -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

View File

@@ -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 <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:

View File

@@ -84,6 +84,7 @@ impl ActivityManager {
protocol: FileTransferProtocol,
username: Option<String>,
password: Option<String>,
entry_directory: Option<PathBuf>,
) {
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;
}

View File

@@ -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 <https://github.com/veeso/termscp>");
}
@@ -120,6 +122,7 @@ fn main() {
}
// Check free args
let extra_args: Vec<String> = 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<PathBuf> = 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);

View File

@@ -69,6 +69,7 @@ pub struct FileTransferParams {
pub protocol: FileTransferProtocol,
pub username: Option<String>,
pub password: Option<String>,
pub entry_directory: Option<PathBuf>,
}
/// ### InputField

View File

@@ -67,6 +67,14 @@ impl FileTransferActivity {
.as_ref(),
);
}
// Try to change directory to entry directory
let mut remote_chdir: Option<PathBuf> = 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,7 +615,8 @@ 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() {
self.local
.set_abs_index(match self.local.get_current_file() {
Some(_) => self.local.get_index(),
None => match self.local.count() {
0 => 0,
@@ -636,7 +645,8 @@ 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() {
self.remote
.set_abs_index(match self.remote.get_current_file() {
Some(_) => self.remote.get_index(),
None => match self.remote.count() {
0 => 0,