New get_home_dir command

This commit is contained in:
sagie gur ari 2020-01-25 16:34:22 +00:00
parent 2805a3c04c
commit 13dd8b9d91
9 changed files with 185 additions and 19 deletions

View file

@ -2,6 +2,7 @@
### v0.1.9
* New get_home_dir command.
* New array_join command.
* The read_properties command now support **--prefix** flag.
* New array_concat command.

View file

@ -30,6 +30,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::GetHomeDirectory (get_home_dir)](#std__env__GetHomeDirectory)
* [std::env::GetVar (get_env)](#std__env__GetVar)
* [std::env::PrintCurrentDirectory (pwd)](#std__env__PrintCurrentDirectory)
* [std::env::SetCurrentDirectory (cd, set_current_dir)](#std__env__SetCurrentDirectory)
@ -833,28 +834,49 @@ array_is_empty
## std::collections::ArrayJoin
```sh
handle = array_concat [handle]*
var = array_join handle separator
```
Concats all provided arrays and returns a handle to a new array with all items.
Joins all values in the provided array with the provided separator in between each value.
#### Parameters
Any number of array handles.
* An array handle
* The separator to put between each item pair
#### Return Value
A handle to the new array.
The joined string value
#### Examples
```sh
input1 = range 1 4
input2 = range 4 6
input3 = range 6 8
function test_to_string
arr = array hello world
string = array_join ${arr} ", "
# new array will contain values from 1-7
arr = array_concat ${input1} ${input2} ${input3}
release ${arr}
assert_eq ${string} "hello, world"
end
function test_numbers
arr = range 1 5
string = array_join ${arr} ", "
release ${arr}
assert_eq ${string} "1, 2, 3, 4"
end
function test_empty_separator
arr = range 1 5
string = array_join ${arr} ""
release ${arr}
assert_eq ${string} "1234"
end
```
@ -1264,6 +1286,33 @@ assert found
#### Aliases:
dump_variables
<a name="std__env__GetHomeDirectory"></a>
## std::env::GetHomeDirectory
```sh
var = get_home_dir
```
Returns the user home directory path.<br>
In case of any error, false will be returned.
#### Parameters
None
#### Return Value
The user home directory path or false in case of any error.
#### Examples
```sh
directory = get_home_dir
```
#### Aliases:
get_home_dir
<a name="std__env__GetVar"></a>
## std::env::GetVar
```sh

View file

@ -1,24 +1,45 @@
```sh
handle = array_concat [handle]*
var = array_join handle separator
```
Concats all provided arrays and returns a handle to a new array with all items.
Joins all values in the provided array with the provided separator in between each value.
#### Parameters
Any number of array handles.
* An array handle
* The separator to put between each item pair
#### Return Value
A handle to the new array.
The joined string value
#### Examples
```sh
input1 = range 1 4
input2 = range 4 6
input3 = range 6 8
function test_to_string
arr = array hello world
string = array_join ${arr} ", "
# new array will contain values from 1-7
arr = array_concat ${input1} ${input2} ${input3}
release ${arr}
assert_eq ${string} "hello, world"
end
function test_numbers
arr = range 1 5
string = array_join ${arr} ", "
release ${arr}
assert_eq ${string} "1, 2, 3, 4"
end
function test_empty_separator
arr = range 1 5
string = array_join ${arr} ""
release ${arr}
assert_eq ${string} "1234"
end
```

View file

@ -0,0 +1,20 @@
```sh
var = get_home_dir
```
Returns the user home directory path.<br>
In case of any error, false will be returned.
#### Parameters
None
#### Return Value
The user home directory path or false in case of any error.
#### Examples
```sh
directory = get_home_dir
```

View file

@ -0,0 +1,46 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use home;
#[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, "GetHomeDirectory")
}
fn aliases(&self) -> Vec<String> {
vec!["get_home_dir".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 run(&self, _arguments: Vec<String>) -> CommandResult {
match home::home_dir() {
Some(directory) => {
let directory_str = directory.to_string_lossy().into_owned();
CommandResult::Continue(Some(directory_str))
}
None => CommandResult::Error("Unable to find user home directory.".to_string()),
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,18 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_valid() {
let directory = home::home_dir().unwrap().to_string_lossy().into_owned();
test::run_script_and_validate(
vec![create("")],
"out = get_home_dir",
CommandValidation::Match("out".to_string(), directory),
);
}

View file

@ -1,5 +1,6 @@
mod cd;
mod get;
mod get_home_dir;
mod pwd;
mod set;
mod unset;
@ -15,6 +16,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(cd::create(&package))?;
commands.set(get::create(&package))?;
commands.set(get_home_dir::create(&package))?;
commands.set(pwd::create(&package))?;
commands.set(set::create(&package))?;
commands.set(unset::create(&package))?;

View file

@ -26,7 +26,7 @@ function test_empty_array
assert_eq ${string} ""
end
function test_separator_array
function test_empty_separator
arr = range 1 5
string = array_join ${arr} ""

9
test/std/env/get_home_dir_test.ds vendored Normal file
View file

@ -0,0 +1,9 @@
function test_get
directory = get_home_dir
empty = is_empty ${directory}
not_false = not equals false ${directory}
assert_false ${empty}
assert ${not_false}
end