New env_to_map command #96

This commit is contained in:
sagie gur ari 2020-04-14 13:36:27 +00:00
parent 6fc8077245
commit 281beaa44a
13 changed files with 165 additions and 4 deletions

View file

@ -2,6 +2,7 @@
### v0.3.3
* New env_to_map command #96
* New map_keys command.
* New temp_dir command.
* Runtime - Use default trait.

View file

@ -36,6 +36,7 @@
* [std::debug::DumpInstructions (dump_instructions)](#std__debug__DumpInstructions)
* [std::debug::DumpState (dump_state)](#std__debug__DumpState)
* [std::debug::DumpVariables (dump_variables)](#std__debug__DumpVariables)
* [std::env::EnvToMap (env_to_map)](#std__env__EnvToMap)
* [std::env::GetHomeDirectory (get_home_dir)](#std__env__GetHomeDirectory)
* [std::env::GetOSFamily (os_family)](#std__env__GetOSFamily)
* [std::env::GetOSName (os_name)](#std__env__GetOSName)
@ -1475,6 +1476,39 @@ assert found
#### Aliases:
dump_variables
<a name="std__env__EnvToMap"></a>
## std::env::EnvToMap
```sh
handle = env_to_map
```
Converts all environment variables to a map and returns the map handle.
#### Parameters
None
#### Return Value
The map handle.
#### Examples
```sh
set_env env_to_map_test test_value
handle = env_to_map
value = map_get ${handle} env_to_map_test
assert_eq ${value} test_value
release ${handle}
```
#### Aliases:
env_to_map
<a name="std__env__GetHomeDirectory"></a>
## std::env::GetHomeDirectory
```sh

View file

@ -0,0 +1,26 @@
```sh
handle = env_to_map
```
Converts all environment variables to a map and returns the map handle.
#### Parameters
None
#### Return Value
The map handle.
#### Examples
```sh
set_env env_to_map_test test_value
handle = env_to_map
value = map_get ${handle} env_to_map_test
assert_eq ${value} test_value
release ${handle}
```

View file

@ -0,0 +1,66 @@
use crate::utils::pckg;
use crate::utils::state::put_handle;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use std::collections::HashMap;
use std::env;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "EnvToMap")
}
fn aliases(&self) -> Vec<String> {
vec!["env_to_map".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
_arguments: Vec<String>,
state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
let all_vars = env::vars();
let mut map = HashMap::new();
for (var_key, var_value) in all_vars {
map.insert(var_key, StateValue::String(var_value.to_string()));
}
let key = put_handle(state, StateValue::SubState(map));
CommandResult::Continue(Some(key))
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,17 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_valid() {
test::run_script_and_validate(
vec![create("")],
r#"out = env_to_map"#,
CommandValidation::Contains("out".to_string(), "handle:".to_string()),
);
}

View file

@ -1,4 +1,5 @@
mod get;
mod env_to_map;
mod get_env;
mod get_home_dir;
mod get_user_name;
mod is_windows;
@ -7,8 +8,8 @@ mod os_name;
mod os_release;
mod os_version;
mod print_current_directory;
mod set;
mod set_current_directory;
mod set_env;
mod uname;
mod unset;
@ -21,7 +22,8 @@ static PACKAGE: &str = "env";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(get::create(&package))?;
commands.set(env_to_map::create(&package))?;
commands.set(get_env::create(&package))?;
commands.set(get_home_dir::create(&package))?;
commands.set(get_user_name::create(&package))?;
commands.set(is_windows::create(&package)?)?;
@ -30,8 +32,8 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(os_release::create(&package))?;
commands.set(os_version::create(&package))?;
commands.set(print_current_directory::create(&package))?;
commands.set(set::create(&package))?;
commands.set(set_current_directory::create(&package))?;
commands.set(set_env::create(&package))?;
commands.set(uname::create(&package)?)?;
commands.set(unset::create(&package))?;

15
test/std/env/env_to_map_test.ds vendored Normal file
View file

@ -0,0 +1,15 @@
fn test_env_to_map
set_env env_to_map_test1 test_value1
handle = env_to_map
set_env env_to_map_test2 test_value2
value = map_get ${handle} env_to_map_test1
assert_eq ${value} test_value1
value = map_get ${handle} env_to_map_test2
assert_false ${value}
release ${handle}
end