New cpu_count command

This commit is contained in:
sagie gur ari 2020-04-14 22:15:34 +00:00
parent f2b62e3b96
commit a32c4428b2
8 changed files with 116 additions and 0 deletions

View File

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

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::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)
* [std::env::GetOSName (os_name)](#std__env__GetOSName)
@ -1510,6 +1511,32 @@ release ${handle}
#### Aliases:
env_to_map
<a name="std__env__GetCpuCount"></a>
## std::env::GetCpuCount
```sh
var = cpu_count
```
Returns the number of CPUs.
#### Parameters
None
#### Return Value
The CPU count.
#### Examples
```sh
count = cpu_count
```
#### Aliases:
cpu_count, get_cpu_count
<a name="std__env__GetHomeDirectory"></a>
## std::env::GetHomeDirectory
```sh

View File

@ -33,6 +33,7 @@ glob = "^0.3"
home = "^0.5"
java-properties = "^1"
meval = "^0.2"
num_cpus = "^1"
rand = "^0.7"
walkdir = "^2"
whoami = "^0.8"

View File

@ -0,0 +1,19 @@
```sh
var = cpu_count
```
Returns the number of CPUs.
#### Parameters
None
#### Return Value
The CPU count.
#### Examples
```sh
count = cpu_count
```

View File

@ -0,0 +1,42 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use num_cpus;
#[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, "GetCpuCount")
}
fn aliases(&self) -> Vec<String> {
vec!["cpu_count".to_string(), "get_cpu_count".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 {
let num = num_cpus::get();
CommandResult::Continue(Some(num.to_string()))
}
}
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("")],
"out = cpu_count",
CommandValidation::Ignore,
);
}

View File

@ -1,3 +1,4 @@
mod cpu_count;
mod env_to_map;
mod get_env;
mod get_home_dir;
@ -23,6 +24,7 @@ static PACKAGE: &str = "env";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(cpu_count::create(&package))?;
commands.set(env_to_map::create(&package))?;
commands.set(get_env::create(&package))?;
commands.set(get_home_dir::create(&package))?;

7
test/std/env/cpu_count_test.ds vendored Normal file
View File

@ -0,0 +1,7 @@
fn test_cpu_count
value = cpu_count
valid = greater_than ${value} 0
assert ${valid}
end