This commit is contained in:
2025-04-04 18:21:16 +02:00
parent eca7e6f552
commit c9b4010089
18 changed files with 1018 additions and 45 deletions

View File

@@ -0,0 +1,74 @@
//! Example of using the Rhai integration with SAL Buildah module
//!
//! This example demonstrates how to use the Rhai scripting language
//! with the System Abstraction Layer (SAL) Buildah 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 println function
engine.register_fn("println", |s: &str| println!("{}", s));
// Register SAL functions with the engine
sal::rhai::register(&mut engine)?;
// Run a Rhai script that uses SAL Buildah functions
let script = r#"
// List available images
println("Listing available images:");
let images = buildah_images();
println("Found " + images.len() + " images");
// Create a container from an image (uncomment if you have a valid image)
// let container = buildah_from("alpine:latest");
// println("Created container: " + container.stdout.trim());
// Build an image using options
let build_options = buildah_new_build_options();
build_options.tag = "example-image:latest";
build_options.context_dir = ".";
build_options.file = "example_Dockerfile";
println("Building image with options:");
println(" Tag: " + build_options.tag);
println(" Context: " + build_options.context_dir);
println(" Dockerfile: " + build_options.file);
// Uncomment to actually build the image
// let build_result = buildah_build(build_options);
// println("Build result: " + build_result.success);
// Create a container configuration
let config_options = buildah_new_config_options();
config_options.author = "Rhai Example";
config_options.cmd = "/bin/sh -c 'echo Hello from Buildah'";
println("Container config options:");
println(" Author: " + config_options.author);
println(" Command: " + config_options.cmd);
// Commit options
let commit_options = buildah_new_commit_options();
commit_options.format = "docker";
commit_options.squash = true;
commit_options.rm = true;
println("Commit options:");
println(" Format: " + commit_options.format);
println(" Squash: " + commit_options.squash);
println(" Remove container: " + commit_options.rm);
// Return success
true
"#;
// Evaluate the script
let result = engine.eval::<bool>(script)?;
println!("Script execution successful: {}", result);
Ok(())
}

View File

@@ -2,9 +2,9 @@
//!
//! This example demonstrates how to use the Rhai scripting language
//! with the System Abstraction Layer (SAL) library.
use rhai::Engine;
use sal::rhai;
use sal::rhai::{self, Engine};
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {

View File

@@ -3,65 +3,52 @@
//! This example demonstrates how to use the Rhai scripting language
//! with the System Abstraction Layer (SAL) Process module.
use rhai::{Engine, Map, Dynamic};
use sal::rhai;
use rhai::Engine;
use std::error::Error;
fn main() -> Result<(), Box<dyn 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
rhai::register(&mut 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 a map with all the results
{
ls_exists: ls_exists,
echo_stdout: echo_result.stdout,
echo_success: echo_result.success,
silent_success: silent_result.success,
process_count: processes.len()
}
// Return success
true
"#;
// Evaluate the script and get the results
let result = engine.eval::<Map>(script)?;
// Print the results
println!("Script results:");
if let Some(ls_exists) = result.get("ls_exists") {
println!(" ls command exists: {}", ls_exists);
}
if let Some(echo_stdout) = result.get("echo_stdout") {
println!(" Echo command output: {}", echo_stdout.clone().into_string().unwrap());
}
if let Some(echo_success) = result.get("echo_success") {
println!(" Echo command success: {}", echo_success.clone().as_bool().unwrap());
}
if let Some(silent_success) = result.get("silent_success") {
println!(" Silent command success: {}", silent_success.clone().as_bool().unwrap());
}
if let Some(process_count) = result.get("process_count") {
println!(" Number of processes: {}", process_count.clone().as_int().unwrap());
}
// Evaluate the script
let result = engine.eval::<bool>(script)?;
println!("Script execution successful: {}", result);
Ok(())
}