54 lines
1.9 KiB
Rust
54 lines
1.9 KiB
Rust
//! Example of using the Rhai integration with SAL Process module
|
|
//!
|
|
//! This example demonstrates how to use the Rhai scripting language
|
|
//! with the System Abstraction Layer (SAL) Process module.
|
|
|
|
use rhai::Engine;
|
|
use std::error::Error;
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
// Create a new Rhai engine
|
|
let mut engine = Engine::new();
|
|
|
|
// Register SAL functions with the engine
|
|
sal::rhai::register(&mut engine)?;
|
|
|
|
// Run a Rhai script that uses SAL Process functions
|
|
let script = r#"
|
|
// Check if a command exists
|
|
let ls_exists = which("ls");
|
|
println("ls command exists: " + ls_exists);
|
|
|
|
// Run a simple command
|
|
let echo_result = run_command("echo 'Hello from Rhai!'");
|
|
println("Echo command output: " + echo_result.stdout);
|
|
println("Echo command success: " + echo_result.success);
|
|
|
|
// Run a command silently
|
|
let silent_result = run_silent("ls -la");
|
|
println("Silent command success: " + silent_result.success);
|
|
|
|
// Run a command with custom options using a Map
|
|
let options = new_run_options();
|
|
options["die"] = false; // Don't return error if command fails
|
|
options["silent"] = true; // Suppress output to stdout/stderr
|
|
options["async_exec"] = false; // Run synchronously
|
|
options["log"] = true; // Log command execution
|
|
|
|
let custom_result = run("echo 'Custom options'", options);
|
|
println("Custom command success: " + custom_result.success);
|
|
|
|
// List processes
|
|
let processes = process_list("");
|
|
println("Number of processes: " + processes.len());
|
|
|
|
// Return success
|
|
true
|
|
"#;
|
|
|
|
// Evaluate the script
|
|
let result = engine.eval::<bool>(script)?;
|
|
println!("Script execution successful: {}", result);
|
|
|
|
Ok(())
|
|
} |