mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New concat command
This commit is contained in:
parent
95917edb77
commit
78a63fe5bc
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.9
|
||||
|
||||
* New concat command.
|
||||
* Improve wget input parsing
|
||||
|
||||
### v0.1.8 (2020-01-24)
|
||||
|
|
78
docs/sdk.md
78
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
|
||||
|
||||
<a name="std__string__Concat"></a>
|
||||
## std::string::Concat
|
||||
|
||||
```sh
|
||||
var = wget [--method=HTTP-method] [--post-data=payload] [-O file] URL
|
||||
```
|
||||
|
||||
Invokes a HTTP request.<br>
|
||||
The request method by default is GET but can be modified by the ```--method``` parameter.<br>
|
||||
The ```-O``` parameter will redirect a valid response output to the provided file, otherwise all response text will be set to the
|
||||
output variable.<br>
|
||||
When redirecting to file, the output would be the response size.<br>
|
||||
The ```--post-data``` parameter enables to pass a payload to POST http requests.<br>
|
||||
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.<br>
|
||||
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
|
||||
|
||||
<a name="std__string__Contains"></a>
|
||||
## std::string::Contains
|
||||
```sh
|
||||
|
|
57
duckscript_sdk/src/sdk/std/string/concat/help.md
Normal file
57
duckscript_sdk/src/sdk/std/string/concat/help.md
Normal file
|
@ -0,0 +1,57 @@
|
|||
```sh
|
||||
var = wget [--method=HTTP-method] [--post-data=payload] [-O file] URL
|
||||
```
|
||||
|
||||
Invokes a HTTP request.<br>
|
||||
The request method by default is GET but can be modified by the ```--method``` parameter.<br>
|
||||
The ```-O``` parameter will redirect a valid response output to the provided file, otherwise all response text will be set to the
|
||||
output variable.<br>
|
||||
When redirecting to file, the output would be the response size.<br>
|
||||
The ```--post-data``` parameter enables to pass a payload to POST http requests.<br>
|
||||
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.<br>
|
||||
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
|
||||
```
|
22
duckscript_sdk/src/sdk/std/string/concat/mod.rs
Executable file
22
duckscript_sdk/src/sdk/std/string/concat/mod.rs
Executable file
|
@ -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<Box<dyn Command>, 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))
|
||||
}
|
7
duckscript_sdk/src/sdk/std/string/concat/mod_test.rs
Normal file
7
duckscript_sdk/src/sdk/std/string/concat/mod_test.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create("").unwrap());
|
||||
}
|
7
duckscript_sdk/src/sdk/std/string/concat/script.ds
Normal file
7
duckscript_sdk/src/sdk/std/string/concat/script.ds
Normal file
|
@ -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}
|
|
@ -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))?;
|
||||
|
|
48
test/std/string/concat_test.ds
Normal file
48
test/std/string/concat_test.ds
Normal file
|
@ -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
|
Loading…
Reference in a new issue