From 93bac918894021e951fa6803941583dc42369b69 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 26 Jun 2020 18:15:54 +0000 Subject: [PATCH] improve ftp commands --- .../src/sdk/std/net/ftp/list/mod.rs | 11 ++----- duckscript_sdk/src/sdk/std/net/ftp/mod.rs | 31 +++++++++++-------- .../src/sdk/std/net/ftp/nlst/mod.rs | 11 ++----- test/std/net/ftp/ftp_list_test.ds | 13 ++++++++ test/std/net/ftp/ftp_nlst_test.ds | 13 ++++++++ 5 files changed, 48 insertions(+), 31 deletions(-) diff --git a/duckscript_sdk/src/sdk/std/net/ftp/list/mod.rs b/duckscript_sdk/src/sdk/std/net/ftp/list/mod.rs index f093c34..bc8089e 100755 --- a/duckscript_sdk/src/sdk/std/net/ftp/list/mod.rs +++ b/duckscript_sdk/src/sdk/std/net/ftp/list/mod.rs @@ -47,17 +47,10 @@ impl Command for CommandImpl { _commands: &mut Commands, _line: usize, ) -> CommandResult { - run_with_connection(&arguments, &mut |options: &Options, + run_with_connection(&arguments, &mut |_options: &Options, ftp_stream: &mut FtpStream| -> CommandResult { - let options_clone = options.clone(); - - let operation_result = match options_clone.path { - Some(value) => ftp_stream.list(Some(value.as_str())), - None => ftp_stream.list(None), - }; - - match operation_result { + match ftp_stream.list(None) { Ok(output) => { let mut array = vec![]; diff --git a/duckscript_sdk/src/sdk/std/net/ftp/mod.rs b/duckscript_sdk/src/sdk/std/net/ftp/mod.rs index ce99005..5fb875c 100644 --- a/duckscript_sdk/src/sdk/std/net/ftp/mod.rs +++ b/duckscript_sdk/src/sdk/std/net/ftp/mod.rs @@ -126,21 +126,26 @@ fn run_in_ftp_connection_context( match FtpStream::connect(&connection_string) { Ok(mut ftp_stream) => { - let operation_result = - if options.user_name.is_some() && options.password.is_some() { - let options_cloned = options.clone(); - let user_name = options_cloned.user_name.unwrap(); - let password = options_cloned.password.unwrap(); + let options_cloned = options.clone(); - ftp_stream.login(&user_name, &password) - } else { - Ok(()) - }; + // login if needed + if options.user_name.is_some() && options.password.is_some() { + let user_name = options_cloned.user_name.unwrap(); + let password = options_cloned.password.unwrap(); - let result = match operation_result { - Ok(_) => func(&mut ftp_stream), - Err(error) => CommandResult::Error(error.to_string()), - }; + if let Err(error) = ftp_stream.login(&user_name, &password) { + return CommandResult::Error(error.to_string()); + } + } + + // move to another directory + if let Some(path) = options_cloned.path { + if let Err(error) = ftp_stream.cwd(path.as_str()) { + return CommandResult::Error(error.to_string()); + } + } + + let result = func(&mut ftp_stream); ftp_stream.quit().unwrap_or(()); diff --git a/duckscript_sdk/src/sdk/std/net/ftp/nlst/mod.rs b/duckscript_sdk/src/sdk/std/net/ftp/nlst/mod.rs index 1a43955..c33d7df 100755 --- a/duckscript_sdk/src/sdk/std/net/ftp/nlst/mod.rs +++ b/duckscript_sdk/src/sdk/std/net/ftp/nlst/mod.rs @@ -47,17 +47,10 @@ impl Command for CommandImpl { _commands: &mut Commands, _line: usize, ) -> CommandResult { - run_with_connection(&arguments, &mut |options: &Options, + run_with_connection(&arguments, &mut |_options: &Options, ftp_stream: &mut FtpStream| -> CommandResult { - let options_clone = options.clone(); - - let operation_result = match options_clone.path { - Some(value) => ftp_stream.nlst(Some(value.as_str())), - None => ftp_stream.nlst(None), - }; - - match operation_result { + match ftp_stream.nlst(None) { Ok(output) => { let mut array = vec![]; diff --git a/test/std/net/ftp/ftp_list_test.ds b/test/std/net/ftp/ftp_list_test.ds index 1e88789..83cd4d0 100644 --- a/test/std/net/ftp/ftp_list_test.ds +++ b/test/std/net/ftp/ftp_list_test.ds @@ -12,3 +12,16 @@ fn test_valid release ${arr} end +fn test_with_path + arr = ftp_list --host test.rebex.net --username demo --password password --path pub + + empty = array_is_empty ${arr} + assert_false ${empty} + + merged = array_join ${arr} , + found = contains ${merged} example + assert ${found} + + release ${arr} +end + diff --git a/test/std/net/ftp/ftp_nlst_test.ds b/test/std/net/ftp/ftp_nlst_test.ds index 9283cb2..ea1acbc 100644 --- a/test/std/net/ftp/ftp_nlst_test.ds +++ b/test/std/net/ftp/ftp_nlst_test.ds @@ -12,3 +12,16 @@ fn test_valid release ${arr} end +fn test_with_path + arr = ftp_nlst --host test.rebex.net --username demo --password password --path pub + + empty = array_is_empty ${arr} + assert_false ${empty} + + merged = array_join ${arr} , + found = contains ${merged} example + assert ${found} + + release ${arr} +end +