...
This commit is contained in:
39
rhaiexamples/01_hello_world.rhai
Normal file
39
rhaiexamples/01_hello_world.rhai
Normal file
@@ -0,0 +1,39 @@
|
||||
// 01_hello_world.rhai
|
||||
// A simple hello world script to demonstrate basic Rhai functionality
|
||||
|
||||
// Print a message
|
||||
println("Hello from Rhai!");
|
||||
|
||||
// Define a function
|
||||
fn greet(name) {
|
||||
"Hello, " + name + "!"
|
||||
}
|
||||
|
||||
// Call the function and print the result
|
||||
let greeting = greet("SAL User");
|
||||
println(greeting);
|
||||
|
||||
// Do some basic calculations
|
||||
let a = 5;
|
||||
let b = 7;
|
||||
println(`${a} + ${b} = ${a + b}`);
|
||||
println(`${a} * ${b} = ${a * b}`);
|
||||
|
||||
// Create and use an array
|
||||
let numbers = [1, 2, 3, 4, 5];
|
||||
println("Numbers: " + numbers);
|
||||
println("Sum of numbers: " + numbers.reduce(|sum, n| sum + n, 0));
|
||||
|
||||
// Create and use a map
|
||||
let person = #{
|
||||
name: "John Doe",
|
||||
age: 30,
|
||||
occupation: "Developer"
|
||||
};
|
||||
|
||||
println("Person: " + person);
|
||||
println("Name: " + person.name);
|
||||
println("Age: " + person.age);
|
||||
|
||||
// Return a success message
|
||||
"Hello world script completed successfully!"
|
61
rhaiexamples/02_file_operations.rhai
Normal file
61
rhaiexamples/02_file_operations.rhai
Normal file
@@ -0,0 +1,61 @@
|
||||
// 02_file_operations.rhai
|
||||
// Demonstrates file system operations using SAL
|
||||
|
||||
// Create a test directory
|
||||
let test_dir = "rhai_test_dir";
|
||||
println(`Creating directory: ${test_dir}`);
|
||||
let mkdir_result = mkdir(test_dir);
|
||||
println(`Directory creation result: ${mkdir_result}`);
|
||||
|
||||
// Check if the directory exists
|
||||
let dir_exists = exist(test_dir);
|
||||
println(`Directory exists: ${dir_exists}`);
|
||||
|
||||
// Create a test file
|
||||
let test_file = test_dir + "/test_file.txt";
|
||||
let file_content = "This is a test file created by Rhai script.";
|
||||
|
||||
// Create the file using a different approach
|
||||
println(`Creating file: ${test_file}`);
|
||||
// First ensure the directory exists
|
||||
run_command(`mkdir -p ${test_dir}`);
|
||||
// Then create the file using a different approach
|
||||
// Use run_command with sh -c to properly handle shell features
|
||||
let write_cmd = `sh -c 'touch ${test_file} && echo "${file_content}" > ${test_file}'`;
|
||||
let write_result = run_command(write_cmd);
|
||||
println(`File creation result: ${write_result.success}`);
|
||||
|
||||
// Wait a moment to ensure the file is created
|
||||
run_command("sleep 1");
|
||||
|
||||
// Check if the file exists
|
||||
let file_exists = exist(test_file);
|
||||
println(`File exists: ${file_exists}`);
|
||||
|
||||
// Get file size
|
||||
if file_exists {
|
||||
let size = file_size(test_file);
|
||||
println(`File size: ${size} bytes`);
|
||||
}
|
||||
|
||||
// Copy the file
|
||||
let copied_file = test_dir + "/copied_file.txt";
|
||||
println(`Copying file to: ${copied_file}`);
|
||||
let copy_result = copy(test_file, copied_file);
|
||||
println(`File copy result: ${copy_result}`);
|
||||
|
||||
// Find files in the directory
|
||||
println("Finding files in the test directory:");
|
||||
let files = find_files(test_dir, "*.txt");
|
||||
for file in files {
|
||||
println(` - ${file}`);
|
||||
}
|
||||
|
||||
// Clean up (uncomment to actually delete the files)
|
||||
// println("Cleaning up...");
|
||||
// delete(copied_file);
|
||||
// delete(test_file);
|
||||
// delete(test_dir);
|
||||
// println("Cleanup complete");
|
||||
|
||||
"File operations script completed successfully!"
|
64
rhaiexamples/03_process_management.rhai
Normal file
64
rhaiexamples/03_process_management.rhai
Normal file
@@ -0,0 +1,64 @@
|
||||
// 03_process_management.rhai
|
||||
// Demonstrates process management operations using SAL
|
||||
|
||||
// Check if common commands exist
|
||||
println("Checking if common commands exist:");
|
||||
let commands = ["ls", "echo", "cat", "grep"];
|
||||
for cmd in commands {
|
||||
let exists = which(cmd);
|
||||
println(` - ${cmd}: ${exists}`);
|
||||
}
|
||||
|
||||
// Run a simple command
|
||||
println("\nRunning a simple echo command:");
|
||||
let echo_result = run_command("echo 'Hello from Rhai process management!'");
|
||||
println(`Command output: ${echo_result.stdout}`);
|
||||
// The CommandResult type doesn't have an exit_code property
|
||||
println(`Success: ${echo_result.success}`);
|
||||
|
||||
// Run a command silently (no output to console)
|
||||
println("\nRunning a command silently:");
|
||||
let silent_result = run_silent("ls -la");
|
||||
println(`Command success: ${silent_result.success}`);
|
||||
println(`Command output length: ${silent_result.stdout.len()} characters`);
|
||||
|
||||
// Create custom run options
|
||||
println("\nRunning a command with custom options:");
|
||||
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 test'", options);
|
||||
println(`Command success: ${custom_result.success}`);
|
||||
println(`Command output: ${custom_result.stdout}`);
|
||||
|
||||
// List processes
|
||||
println("\nListing processes (limited to 5):");
|
||||
let processes = process_list("");
|
||||
let count = 0;
|
||||
for proc in processes {
|
||||
if count >= 5 {
|
||||
break;
|
||||
}
|
||||
// Just print the PID since we're not sure what other properties are available
|
||||
println(` - PID: ${proc.pid}`);
|
||||
count += 1;
|
||||
}
|
||||
println(`Total processes: ${processes.len()}`);
|
||||
|
||||
// Run a command that will create a background process
|
||||
// Note: This is just for demonstration, the process will be short-lived
|
||||
println("\nRunning a background process:");
|
||||
let bg_options = new_run_options();
|
||||
bg_options["async_exec"] = true;
|
||||
// Fix the command to avoid issues with shell interpretation
|
||||
let bg_result = run("sleep 1", bg_options);
|
||||
println("Background process started");
|
||||
|
||||
// Wait a moment to let the background process run
|
||||
run_command("sleep 0.5");
|
||||
println("Main script continuing while background process runs");
|
||||
|
||||
"Process management script completed successfully!"
|
112
rhaiexamples/04_buildah_operations.rhai
Normal file
112
rhaiexamples/04_buildah_operations.rhai
Normal file
@@ -0,0 +1,112 @@
|
||||
// 04_buildah_operations.rhai
|
||||
// Demonstrates container operations using SAL's buildah integration
|
||||
// Note: This script requires buildah to be installed and may need root privileges
|
||||
|
||||
// Check if buildah is installed
|
||||
let buildah_exists = which("buildah");
|
||||
println(`Buildah exists: ${buildah_exists}`);
|
||||
|
||||
// List available images (only if buildah is installed)
|
||||
println("Listing available container images:");
|
||||
if buildah_exists != "" {
|
||||
// This try/catch block will handle any errors that might occur
|
||||
// when trying to use buildah functions
|
||||
try {
|
||||
let images = buildah_images();
|
||||
println(`Found ${images.len()} images`);
|
||||
|
||||
// Print image details (limited to 3)
|
||||
let count = 0;
|
||||
for img in images {
|
||||
if count >= 3 {
|
||||
break;
|
||||
}
|
||||
println(` - ID: ${img.id}, Name: ${img.name}, Created: ${img.created}`);
|
||||
count += 1;
|
||||
}
|
||||
} catch(err) {
|
||||
println(`Error accessing buildah: ${err}`);
|
||||
}
|
||||
} else {
|
||||
println("Buildah is not installed. Skipping container image operations.");
|
||||
}
|
||||
|
||||
// Remove the duplicate code that was causing the error
|
||||
|
||||
// The following operations are commented out as they require buildah to be installed
|
||||
// and may need root privileges. Uncomment them if you want to try them out.
|
||||
|
||||
// Create a container from an image
|
||||
// println("\nCreating a container from alpine image:");
|
||||
// let container = from("alpine:latest");
|
||||
// println(`Container ID: ${container.stdout.trim()}`);
|
||||
|
||||
// Run a command in the container
|
||||
// println("\nRunning a command in the container:");
|
||||
// let run_result = run(container.stdout.trim(), "echo 'Hello from container'");
|
||||
// println(`Command output: ${run_result.stdout}`);
|
||||
|
||||
// Add a file to the container
|
||||
// println("\nAdding a file to the container:");
|
||||
// let test_file = "test_file.txt";
|
||||
// run_command(`echo "Test content" > ${test_file}`);
|
||||
// let add_result = add(container.stdout.trim(), test_file, "/");
|
||||
// println(`Add result: ${add_result.success}`);
|
||||
|
||||
// Commit the container to create a new image
|
||||
// println("\nCommitting the container to create a new image:");
|
||||
// let commit_result = commit(container.stdout.trim(), "my-custom-image:latest");
|
||||
// println(`Commit result: ${commit_result.success}`);
|
||||
|
||||
// Remove the container
|
||||
// println("\nRemoving the container:");
|
||||
// let remove_result = remove(container.stdout.trim());
|
||||
// println(`Remove result: ${remove_result.success}`);
|
||||
|
||||
// Clean up the test file
|
||||
// delete(test_file);
|
||||
|
||||
// Only demonstrate buildah options if buildah is installed
|
||||
if buildah_exists != "" {
|
||||
try {
|
||||
// Demonstrate build options
|
||||
println("\nDemonstrating build options:");
|
||||
let build_options = buildah_new_build_options();
|
||||
build_options.tag = "example-image:latest";
|
||||
build_options.context_dir = ".";
|
||||
build_options.file = "example_Dockerfile";
|
||||
|
||||
println("Build options configured:");
|
||||
println(` - Tag: ${build_options.tag}`);
|
||||
println(` - Context: ${build_options.context_dir}`);
|
||||
println(` - Dockerfile: ${build_options.file}`);
|
||||
|
||||
// Demonstrate commit options
|
||||
println("\nDemonstrating commit options:");
|
||||
let commit_options = buildah_new_commit_options();
|
||||
commit_options.format = "docker";
|
||||
commit_options.squash = true;
|
||||
commit_options.rm = true;
|
||||
|
||||
println("Commit options configured:");
|
||||
println(` - Format: ${commit_options.format}`);
|
||||
println(` - Squash: ${commit_options.squash}`);
|
||||
println(` - Remove container: ${commit_options.rm}`);
|
||||
|
||||
// Demonstrate config options
|
||||
println("\nDemonstrating config options:");
|
||||
let config_options = buildah_new_config_options();
|
||||
config_options.author = "Rhai Example";
|
||||
config_options.cmd = "/bin/sh -c 'echo Hello from Buildah'";
|
||||
|
||||
println("Config options configured:");
|
||||
println(` - Author: ${config_options.author}`);
|
||||
println(` - Command: ${config_options.cmd}`);
|
||||
} catch(err) {
|
||||
println(`Error accessing buildah options: ${err}`);
|
||||
}
|
||||
} else {
|
||||
println("\nSkipping buildah options demonstration since buildah is not installed.");
|
||||
}
|
||||
|
||||
"Buildah operations script completed successfully!"
|
Reference in New Issue
Block a user