New base64 command #79

This commit is contained in:
sagie gur ari 2020-01-31 14:05:30 +00:00
parent 65cd9db9aa
commit d150583bb4
11 changed files with 313 additions and 8 deletions

View file

@ -2,6 +2,7 @@
### v0.1.9
* New base64 command #79
* New write_binary_file command #78
* New read_binary_file command #78
* Rename read/write text file commands.

View file

@ -68,6 +68,7 @@
* [std::process::ProcessID (pid, process_id)](#std__process__ProcessID)
* [std::process::Watchdog (watchdog)](#std__process__Watchdog)
* [std::scope::Clear (clear_scope)](#std__scope__Clear)
* [std::string::Base64 (base64)](#std__string__Base64)
* [std::string::Base64Decode (base64_decode)](#std__string__Base64Decode)
* [std::string::Base64Encode (base64_encode)](#std__string__Base64Encode)
* [std::string::BytesToString (bytes_to_string)](#std__string__BytesToString)
@ -756,6 +757,7 @@ arr = array_concat ${input1} ${input2} ${input3}
#### Source:
```sh
for scope::array_concat::arg in ${scope::array_concat::arguments}
if not is_array ${scope::array_concat::arg}
trigger_error "Invalid input, non array handle or array not found."
@ -771,6 +773,7 @@ for scope::array_concat::arg in ${scope::array_concat::arguments}
end
set ${scope::array_concat::array}
```
@ -805,8 +808,10 @@ out = array_is_empty ${values}
#### Source:
```sh
scope::array_is_empty::length = array_length ${scope::array_is_empty::argument::1}
equals 0 ${scope::array_is_empty::length}
```
@ -866,6 +871,7 @@ end
#### Source:
```sh
if not is_array ${scope::array_join::argument::1}
trigger_error "Invalid input, non array handle or array not found."
end
@ -884,6 +890,7 @@ if not array_is_empty ${scope::array_join::argument::1}
end
set ${scope::array_join::string}
```
@ -2347,6 +2354,7 @@ end
#### Source:
```sh
scope::wget::url = array_pop ${scope::wget::arguments}
scope::wget::method = set GET
@ -2370,6 +2378,7 @@ for scope::wget::arg in ${scope::wget::arguments}
end
http_client --method "${scope::wget::method}" --output-file "${scope::wget::file}" --payload "${scope::wget::payload}" ${scope::wget::url}
```
@ -2564,6 +2573,101 @@ assert_false ${defined}
#### Aliases:
clear_scope
<a name="std__string__Base64"></a>
## std::string::Base64
```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::base64::input_data = array_pop ${scope::base64::arguments}
scope::base64::encode = set true
for scope::base64::arg in ${scope::base64::arguments}
if equals ${scope::base64::arg} -e
scope::base64::encode = set true
elif equals ${scope::base64::arg} -encode
scope::base64::encode = set true
elif equals ${scope::base64::arg} -d
scope::base64::encode = set false
elif equals ${scope::base64::arg} -decode
scope::base64::encode = set false
end
end
if ${scope::base64::encode}
scope::base64::output = base64_encode ${scope::base64::input_data}
else
scope::base64::output = base64_decode ${scope::base64::input_data}
end
scope::base64::output = set ${scope::base64::output}
```
#### Aliases:
base64
<a name="std__string__Base64Decode"></a>
## std::string::Base64Decode
```sh
@ -2690,12 +2794,14 @@ assert_eq ${output} "12 34"
#### 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}
```

View 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
```

View 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, "Base64");
let command = create_alias_command(
name,
vec!["base64".to_string()],
include_str!("help.md").to_string(),
"base64".to_string(),
include_str!("script.ds").to_string(),
1,
)?;
Ok(Box::new(command))
}

View file

@ -0,0 +1,7 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create("").unwrap());
}

View file

@ -0,0 +1,23 @@
scope::base64::input_data = array_pop ${scope::base64::arguments}
scope::base64::encode = set true
for scope::base64::arg in ${scope::base64::arguments}
if equals ${scope::base64::arg} -e
scope::base64::encode = set true
elif equals ${scope::base64::arg} -encode
scope::base64::encode = set true
elif equals ${scope::base64::arg} -d
scope::base64::encode = set false
elif equals ${scope::base64::arg} -decode
scope::base64::encode = set false
end
end
if ${scope::base64::encode}
scope::base64::output = base64_encode ${scope::base64::input_data}
else
scope::base64::output = base64_decode ${scope::base64::input_data}
end
scope::base64::output = set ${scope::base64::output}

View file

@ -1,3 +1,4 @@
mod base64;
mod base64_decode;
mod base64_encode;
pub(crate) mod bytes_to_string;
@ -27,6 +28,7 @@ static PACKAGE: &str = "string";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(base64::create(&package)?)?;
commands.set(base64_decode::create(&package))?;
commands.set(base64_encode::create(&package))?;
commands.set(bytes_to_string::create(&package))?;

View file

@ -227,7 +227,7 @@ pub(crate) fn create_alias_command(
script: String,
arguments_amount: usize,
) -> Result<AliasCommand, ScriptError> {
let raw_command = script.trim().to_string();
let raw_command = script.to_string();
let command = AliasCommand::new(
name,

View file

@ -0,0 +1,28 @@
function test_encode_decode
handle = string_to_bytes "hello world"
text = base64_encode ${handle}
release ${handle}
assert_eq ${text} aGVsbG8gd29ybGQ=
handle = base64_decode ${text}
text = bytes_to_string ${handle}
release ${handle}
assert_eq ${text} "hello world"
end
function test_missing_input
error = get_last_error
empty = is_empty ${error}
assert ${empty}
result = base64_decode
error = get_last_error
empty = is_empty ${error}
assert_false ${empty}
assert_false ${result}
end

View file

@ -0,0 +1,23 @@
function test_encode
handle = string_to_bytes "hello world"
text = base64_encode ${handle}
release ${handle}
assert_eq ${text} aGVsbG8gd29ybGQ=
end
function test_missing_input
error = get_last_error
empty = is_empty ${error}
assert ${empty}
result = base64_encode
error = get_last_error
empty = is_empty ${error}
assert_false ${empty}
assert_false ${result}
end

View file

@ -1,23 +1,59 @@
function test_encode
function test_encode_no_flags
handle = string_to_bytes "hello world"
text = base64_encode ${handle}
text = base64 ${handle}
release ${handle}
assert_eq ${text} "aGVsbG8gd29ybGQ="
assert_eq ${text} aGVsbG8gd29ybGQ=
end
function test_encode_decode
function test_encode_e_flag
handle = string_to_bytes "hello world"
text = base64_encode ${handle}
text = base64 -d -e ${handle}
release ${handle}
assert_eq ${text} "aGVsbG8gd29ybGQ="
assert_eq ${text} aGVsbG8gd29ybGQ=
end
handle = base64_decode ${text}
function test_encode_encode_flag
handle = string_to_bytes "hello world"
text = base64 -d -encode ${handle}
release ${handle}
assert_eq ${text} aGVsbG8gd29ybGQ=
end
function test_decode_d_flag
handle = base64 -d aGVsbG8gd29ybGQ=
text = bytes_to_string ${handle}
release ${handle}
assert_eq ${text} "hello world"
end
function test_decode_decode_flag
handle = base64 -decode aGVsbG8gd29ybGQ=
text = bytes_to_string ${handle}
release ${handle}
assert_eq ${text} "hello world"
end
function test_missing_input
error = get_last_error
empty = is_empty ${error}
assert ${empty}
result = base64
error = get_last_error
empty = is_empty ${error}
assert_false ${empty}
assert_false ${result}
end