New which command

This commit is contained in:
sagie gur ari 2020-04-14 22:55:58 +00:00
parent a32c4428b2
commit 6961e6cc46
9 changed files with 150 additions and 1 deletions

View file

@ -2,6 +2,7 @@
### v0.3.3
* New which command.
* New cpu_count command.
* New printenv command #97
* New env_to_map command #96

View file

@ -37,6 +37,7 @@
* [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::FindExecutable (which)](#std__env__FindExecutable)
* [std::env::GetCpuCount (cpu_count, get_cpu_count)](#std__env__GetCpuCount)
* [std::env::GetHomeDirectory (get_home_dir)](#std__env__GetHomeDirectory)
* [std::env::GetOSFamily (os_family)](#std__env__GetOSFamily)
@ -1511,6 +1512,33 @@ release ${handle}
#### Aliases:
env_to_map
<a name="std__env__FindExecutable"></a>
## std::env::FindExecutable
```sh
var = which executable
```
Returns the path to the executable if exists.<br>
If not found it will return an empty string.
#### Parameters
The executable to find.
#### Return Value
The executable path or empty string if not found.
#### Examples
```sh
path = which echo
```
#### Aliases:
which
<a name="std__env__GetCpuCount"></a>
## std::env::GetCpuCount
```sh

View file

@ -36,6 +36,7 @@ meval = "^0.2"
num_cpus = "^1"
rand = "^0.7"
walkdir = "^2"
which = { version = "^3", default-features = false }
whoami = "^0.8"
[target.'cfg(not(windows))'.dependencies]

View file

@ -1,5 +1,6 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fsio::path::from_path::FromPath;
use home;
#[cfg(test)]
@ -31,7 +32,7 @@ impl Command for CommandImpl {
fn run(&self, _arguments: Vec<String>) -> CommandResult {
match home::home_dir() {
Some(directory) => {
let directory_str = directory.to_string_lossy().into_owned();
let directory_str = FromPath::from_path(&directory);
CommandResult::Continue(Some(directory_str))
}
None => CommandResult::Error("Unable to find user home directory.".to_string()),

View file

@ -14,6 +14,7 @@ mod set_current_directory;
mod set_env;
mod uname;
mod unset;
mod which;
use crate::utils::pckg;
use duckscript::types::command::Commands;
@ -40,6 +41,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(set_env::create(&package))?;
commands.set(uname::create(&package)?)?;
commands.set(unset::create(&package))?;
commands.set(which::create(&package))?;
Ok(())
}

View file

@ -0,0 +1,20 @@
```sh
var = which executable
```
Returns the path to the executable if exists.<br>
If not found it will return an empty string.
#### Parameters
The executable to find.
#### Return Value
The executable path or empty string if not found.
#### Examples
```sh
path = which echo
```

51
duckscript_sdk/src/sdk/std/env/which/mod.rs vendored Executable file
View file

@ -0,0 +1,51 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fsio::path::from_path::FromPath;
use which;
#[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, "FindExecutable")
}
fn aliases(&self) -> Vec<String> {
vec!["which".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 {
if arguments.is_empty() {
CommandResult::Error("No executable name provided.".to_string())
} else {
match which::which(&arguments[0]) {
Ok(path) => {
let path_string: String = FromPath::from_path(&path);
CommandResult::Continue(Some(path_string))
}
_ => CommandResult::Continue(None),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,31 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_arguments() {
test::run_script_and_error(vec![create("")], "out = which", "out");
}
#[test]
fn run_not_found() {
test::run_script_and_validate(
vec![create("")],
"out = which bad_executable",
CommandValidation::None,
);
}
#[test]
fn run_found() {
test::run_script_and_validate(
vec![create("")],
"out = which rustc",
CommandValidation::Ignore,
);
}

14
test/std/env/which_test.ds vendored Normal file
View file

@ -0,0 +1,14 @@
fn test_found
path = which rustc
valid = is_empty ${path}
assert_false ${valid}
end
fn test_not_found
path = which bad_executable
valid = is_empty ${path}
assert ${valid}
end