diff --git a/CHANGELOG.md b/CHANGELOG.md index a7c66b4..727c796 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.1.9 +* New concat command. * Improve wget input parsing ### v0.1.8 (2020-01-24) diff --git a/docs/sdk.md b/docs/sdk.md index 05d7913..8327d2a 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -59,6 +59,7 @@ * [std::process::Execute (exec)](#std__process__Execute) * [std::process::Exit (exit, quit, q)](#std__process__Exit) * [std::scope::Clear (clear_scope)](#std__scope__Clear) +* [std::string::Concat (concat)](#std__string__Concat) * [std::string::Contains (contains)](#std__string__Contains) * [std::string::EndsWith (ends_with)](#std__string__EndsWith) * [std::string::Equals (equals, eq)](#std__string__Equals) @@ -2197,6 +2198,83 @@ assert_false ${defined} #### Aliases: clear_scope + +## std::string::Concat + +```sh +var = wget [--method=HTTP-method] [--post-data=payload] [-O file] URL +``` + +Invokes a HTTP request.
+The request method by default is GET but can be modified by the ```--method``` parameter.
+The ```-O``` parameter will redirect a valid response output to the provided file, otherwise all response text will be set to the +output variable.
+When redirecting to file, the output would be the response size.
+The ```--post-data``` parameter enables to pass a payload to POST http requests.
+In case of errors or error HTTP response codes, false will be returned. + +#### Parameters + +* Optional HTTP Method, for example --method=HTTP-GET or --method=HTTP-POST (currently only GET and POST are supported). +* Optional post payload via ```--post-data``` parameter. +* Optional redirection of output to file via ```-O``` parameter. +* The target URL + +#### Return Value + +The response text or in case of output redirection to file, the response size.
+In case of errors, it will return false. + +#### Examples + +```sh +function test_get + response = wget https://www.rust-lang.org/ + + found = contains ${response} Rust + + assert ${found} +end + +function test_get_to_file + file = set ./target/_duckscript_test/wget/page.html + rm ${file} + + response_size = wget -O ${file} https://www.rust-lang.org/ + + response = readfile ${file} + found = contains ${response} Rust + + assert ${found} + assert ${response_size} +end + +function test_post + payload = set {\"login\":\"login\",\"password\":\"password\"} + response = wget --method=HTTP-POST --post-data=${payload} https://reqbin.com/echo/post/json + + found = contains ${response} success + + assert ${found} +end +``` + + +#### Source: + +```sh +scope::concat::output = set "" +for scope::concat::arg in ${scope::concat::arguments} + scope::concat::output = set "${scope::concat::output}${scope::concat::arg}" +end + +set ${scope::concat::output} +``` + + +#### Aliases: +concat + ## std::string::Contains ```sh diff --git a/duckscript_sdk/src/sdk/std/string/concat/help.md b/duckscript_sdk/src/sdk/std/string/concat/help.md new file mode 100644 index 0000000..bf372e0 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/string/concat/help.md @@ -0,0 +1,57 @@ +```sh +var = wget [--method=HTTP-method] [--post-data=payload] [-O file] URL +``` + +Invokes a HTTP request.
+The request method by default is GET but can be modified by the ```--method``` parameter.
+The ```-O``` parameter will redirect a valid response output to the provided file, otherwise all response text will be set to the +output variable.
+When redirecting to file, the output would be the response size.
+The ```--post-data``` parameter enables to pass a payload to POST http requests.
+In case of errors or error HTTP response codes, false will be returned. + +#### Parameters + +* Optional HTTP Method, for example --method=HTTP-GET or --method=HTTP-POST (currently only GET and POST are supported). +* Optional post payload via ```--post-data``` parameter. +* Optional redirection of output to file via ```-O``` parameter. +* The target URL + +#### Return Value + +The response text or in case of output redirection to file, the response size.
+In case of errors, it will return false. + +#### Examples + +```sh +function test_get + response = wget https://www.rust-lang.org/ + + found = contains ${response} Rust + + assert ${found} +end + +function test_get_to_file + file = set ./target/_duckscript_test/wget/page.html + rm ${file} + + response_size = wget -O ${file} https://www.rust-lang.org/ + + response = readfile ${file} + found = contains ${response} Rust + + assert ${found} + assert ${response_size} +end + +function test_post + payload = set {\"login\":\"login\",\"password\":\"password\"} + response = wget --method=HTTP-POST --post-data=${payload} https://reqbin.com/echo/post/json + + found = contains ${response} success + + assert ${found} +end +``` diff --git a/duckscript_sdk/src/sdk/std/string/concat/mod.rs b/duckscript_sdk/src/sdk/std/string/concat/mod.rs new file mode 100755 index 0000000..c647738 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/string/concat/mod.rs @@ -0,0 +1,22 @@ +use crate::types::command::create_alias_command; +use crate::utils::pckg; +use duckscript::types::command::Command; +use duckscript::types::error::ScriptError; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +pub(crate) fn create(package: &str) -> Result, ScriptError> { + let name = pckg::concat(package, "Concat"); + let command = create_alias_command( + name, + vec!["concat".to_string()], + include_str!("help.md").to_string(), + "concat".to_string(), + include_str!("script.ds").to_string(), + 2, + )?; + + Ok(Box::new(command)) +} diff --git a/duckscript_sdk/src/sdk/std/string/concat/mod_test.rs b/duckscript_sdk/src/sdk/std/string/concat/mod_test.rs new file mode 100644 index 0000000..cbc4367 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/string/concat/mod_test.rs @@ -0,0 +1,7 @@ +use super::*; +use crate::test; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("").unwrap()); +} diff --git a/duckscript_sdk/src/sdk/std/string/concat/script.ds b/duckscript_sdk/src/sdk/std/string/concat/script.ds new file mode 100644 index 0000000..6763ff1 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/string/concat/script.ds @@ -0,0 +1,7 @@ + +scope::concat::output = set "" +for scope::concat::arg in ${scope::concat::arguments} + scope::concat::output = set "${scope::concat::output}${scope::concat::arg}" +end + +set ${scope::concat::output} diff --git a/duckscript_sdk/src/sdk/std/string/mod.rs b/duckscript_sdk/src/sdk/std/string/mod.rs index b93968c..df7ed4a 100755 --- a/duckscript_sdk/src/sdk/std/string/mod.rs +++ b/duckscript_sdk/src/sdk/std/string/mod.rs @@ -1,3 +1,4 @@ +mod concat; mod contains; mod ends_with; pub(crate) mod equals; @@ -21,6 +22,7 @@ static PACKAGE: &str = "string"; pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> { let package = pckg::concat(parent, PACKAGE); + commands.set(concat::create(&package)?)?; commands.set(contains::create(&package))?; commands.set(ends_with::create(&package))?; commands.set(equals::create(&package))?; diff --git a/test/std/string/concat_test.ds b/test/std/string/concat_test.ds new file mode 100644 index 0000000..23e7b20 --- /dev/null +++ b/test/std/string/concat_test.ds @@ -0,0 +1,48 @@ + +function test_empty + output = concat "" "" + empty = is_empty ${output} + + assert_eq ${output} "" + assert ${empty} +end + +function test_first_empty + output = concat "" test + empty = is_empty ${output} + + assert_eq ${output} test + assert_false ${empty} +end + +function test_second_empty + output = concat test "" + empty = is_empty ${output} + + assert_eq ${output} test + assert_false ${empty} +end + +function test_both_not_empty + output = concat 1 2 + empty = is_empty ${output} + + assert_eq ${output} 12 + assert_false ${empty} +end + +function test_multiple + output = concat 1 2 3 4 + empty = is_empty ${output} + + assert_eq ${output} 1234 + assert_false ${empty} +end + +function test_with_spaces + output = concat 1 "2 3" 4 + empty = is_empty ${output} + + assert_eq ${output} "12 34" + assert_false ${empty} +end