feat: added std::command module to rhai
This commit is contained in:
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
|
||||
- Proper error handling for invalid external module code.
|
||||
- **call-fns** command for calling a Rhai function. Note: The function can only see poll/listen variables as their initial value.
|
||||
- **update** command with **--inject-vars** flag to update widget state. Note: All poll/listen variables will reset to their initial values.
|
||||
- `std::command` module for running shell commands.
|
||||
|
||||
### Changed
|
||||
|
||||
|
||||
27
crates/iirhai/src/providers/stdlib/command.rs
Normal file
27
crates/iirhai/src/providers/stdlib/command.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use rhai::plugin::*;
|
||||
use rhai::EvalAltResult;
|
||||
use std::process::Command;
|
||||
|
||||
#[export_module]
|
||||
pub mod command {
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn run(cmd: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(cmd)
|
||||
.status()
|
||||
.map_err(|e| format!("Failed to run command: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw)]
|
||||
pub fn run_and_read(cmd: &str) -> Result<String, Box<EvalAltResult>> {
|
||||
let output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(cmd)
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run command: {}", e))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod command;
|
||||
pub mod env;
|
||||
pub mod json;
|
||||
pub mod math;
|
||||
@@ -9,7 +10,7 @@ use rhai::module_resolvers::StaticModuleResolver;
|
||||
|
||||
pub fn register_stdlib(resolver: &mut StaticModuleResolver) {
|
||||
use crate::providers::stdlib::{
|
||||
env::env, json::json, math::math, monitor::monitor, text::text,
|
||||
env::env, json::json, math::math, monitor::monitor, text::text, command::command
|
||||
};
|
||||
|
||||
// adding modules
|
||||
@@ -18,6 +19,7 @@ pub fn register_stdlib(resolver: &mut StaticModuleResolver) {
|
||||
let monitor_mod = exported_module!(monitor);
|
||||
let json_mod = exported_module!(json);
|
||||
let math_mod = exported_module!(math);
|
||||
let command_mod = exported_module!(command);
|
||||
|
||||
// inserting modules
|
||||
resolver.insert("std::text", text_mod);
|
||||
@@ -25,4 +27,5 @@ pub fn register_stdlib(resolver: &mut StaticModuleResolver) {
|
||||
resolver.insert("std::monitor", monitor_mod);
|
||||
resolver.insert("std::json", json_mod);
|
||||
resolver.insert("std::math", math_mod);
|
||||
resolver.insert("std::command", command_mod);
|
||||
}
|
||||
|
||||
@@ -260,3 +260,32 @@ All functions in this module work with floating-point numbers (f64).
|
||||
If you pass an integer (e.g. `0`), Rhai will report an error because there is no math::cos(i64). Use a floating-point literal instead (e.g. `0.0`).
|
||||
|
||||
All math functions return `f64`. If you need an integer result, use `to_int` to convert.
|
||||
|
||||
## `std::command`
|
||||
|
||||
The `std::command` module provides functions which you can use to run shell commands on your system.
|
||||
|
||||
### Usage
|
||||
|
||||
```rust,ignore
|
||||
import "std::command" as command;
|
||||
|
||||
// run a command
|
||||
command::run("notify-send Hello!");
|
||||
|
||||
// run a command and read output from stdout
|
||||
let output = command::run_and_read("pwd"); // example output: /home/foo/.config/ewwii/
|
||||
```
|
||||
|
||||
### Functions
|
||||
|
||||
| Function | Description |
|
||||
| ----------------- | -------------------------------------------------- |
|
||||
| `run(x)` | Run the shell command in `x` |
|
||||
| `run_and_read(x)` | Run the shell command in `x` and return the stdout |
|
||||
|
||||
### Note
|
||||
|
||||
The functions in `std::command` execute arbitrary shell commands. Only run scripts you trust, as misuse can compromise your system.
|
||||
|
||||
This, along with features like `poll`, `listen`, `onclick`, `onhover`, etc., which also run shell commands, can be abused by bad actors. Always verify and trust a package before installing it via [eiipm](https://github.com/Ewwii-sh/eiipm). Even if a package is registered in [eii-manifests](https://github.com/Ewwii-sh/eii-manifests), bad actors could change the code of their package without the awareness of maintainers.
|
||||
|
||||
Reference in New Issue
Block a user