This commit is contained in:
2025-04-04 21:21:50 +02:00
parent bf5eb2f6fc
commit 5b006ff328
33 changed files with 2406 additions and 201 deletions

View File

@@ -16,38 +16,38 @@ use crate::process::CommandResult;
/// # Returns
///
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_buildah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
pub fn register_bah_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// Register types
register_buildah_types(engine)?;
register_bah_types(engine)?;
// Register container functions
engine.register_fn("buildah_from", from);
engine.register_fn("buildah_run", run);
engine.register_fn("buildah_run_with_isolation", run_with_isolation);
engine.register_fn("buildah_copy", copy);
engine.register_fn("buildah_add", add);
engine.register_fn("buildah_commit", commit);
engine.register_fn("buildah_remove", remove);
engine.register_fn("buildah_list", list);
engine.register_fn("buildah_build", build_with_options);
engine.register_fn("buildah_new_build_options", new_build_options);
engine.register_fn("bah_from", bah_from);
engine.register_fn("bah_run", bah_run);
engine.register_fn("bah_run_with_isolation", bah_run_with_isolation);
engine.register_fn("bah_copy", bah_copy);
engine.register_fn("bah_add", bah_add);
engine.register_fn("bah_commit", bah_commit);
engine.register_fn("bah_remove", bah_remove);
engine.register_fn("bah_list", bah_list);
engine.register_fn("bah_build", bah_build_with_options);
engine.register_fn("bah_new_build_options", new_build_options);
// Register image functions
engine.register_fn("buildah_images", images);
engine.register_fn("buildah_image_remove", image_remove);
engine.register_fn("buildah_image_push", image_push);
engine.register_fn("buildah_image_tag", image_tag);
engine.register_fn("buildah_image_pull", image_pull);
engine.register_fn("buildah_image_commit", image_commit_with_options);
engine.register_fn("buildah_new_commit_options", new_commit_options);
engine.register_fn("buildah_config", config_with_options);
engine.register_fn("buildah_new_config_options", new_config_options);
engine.register_fn("bah_images", images);
engine.register_fn("bah_image_remove", image_remove);
engine.register_fn("bah_image_push", image_push);
engine.register_fn("bah_image_tag", image_tag);
engine.register_fn("bah_image_pull", image_pull);
engine.register_fn("bah_image_commit", image_commit_with_options);
engine.register_fn("bah_new_commit_options", new_commit_options);
engine.register_fn("bah_config", config_with_options);
engine.register_fn("bah_new_config_options", new_config_options);
Ok(())
}
/// Register Buildah module types with the Rhai engine
fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
fn register_bah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// Register Image type and methods
engine.register_type_with_name::<Image>("BuildahImage");
@@ -60,6 +60,14 @@ fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
}
array
});
// Add a 'name' getter that returns the first name or a default
engine.register_get("name", |img: &mut Image| {
if img.names.is_empty() {
"<none>".to_string()
} else {
img.names[0].clone()
}
});
engine.register_get("size", |img: &mut Image| img.size.clone());
engine.register_get("created", |img: &mut Image| img.created.clone());
@@ -67,7 +75,7 @@ fn register_buildah_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
}
// Helper functions for error conversion
fn buildah_error_to_rhai_error<T>(result: Result<T, BuildahError>) -> Result<T, Box<EvalAltResult>> {
fn bah_error_to_rhai_error<T>(result: Result<T, BuildahError>) -> Result<T, Box<EvalAltResult>> {
result.map_err(|e| {
Box::new(EvalAltResult::ErrorRuntime(
format!("Buildah error: {}", e).into(),
@@ -107,57 +115,67 @@ pub fn new_config_options() -> Map {
/// Wrapper for buildah::from
///
/// Create a container from an image.
pub fn from(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::from(image))
pub fn bah_from(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
let result = bah_error_to_rhai_error(buildah::from(image))?;
// Create a new CommandResult with trimmed stdout
let trimmed_result = CommandResult {
stdout: result.stdout.trim().to_string(),
stderr: result.stderr.trim().to_string(),
success: result.success,
code: result.code,
};
Ok(trimmed_result)
}
/// Wrapper for buildah::run
///
/// Run a command in a container.
pub fn run(container: &str, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::run(container, command))
pub fn bah_run(container: &str, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::run(container, command))
}
/// Wrapper for buildah::run_with_isolation
///
/// Run a command in a container with specified isolation.
pub fn run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::run_with_isolation(container, command, isolation))
pub fn bah_run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_run_with_isolation(container, command, isolation))
}
/// Wrapper for buildah::copy
///
/// Copy files into a container.
pub fn copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::copy(container, source, dest))
pub fn bah_copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_copy(container, source, dest))
}
/// Wrapper for buildah::add
///
/// Add files into a container.
pub fn add(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::add(container, source, dest))
pub fn bah_add(container: &str, source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_add(container, source, dest))
}
/// Wrapper for buildah::commit
///
/// Commit a container to an image.
pub fn commit(container: &str, image_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::commit(container, image_name))
pub fn bah_commit(container: &str, image_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_commit(container, image_name))
}
/// Wrapper for buildah::remove
///
/// Remove a container.
pub fn remove(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::remove(container))
pub fn bah_remove(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_remove(container))
}
/// Wrapper for buildah::list
///
/// List containers.
pub fn list() -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::list())
pub fn bah_list() -> Result<CommandResult, Box<EvalAltResult>> {
bah_error_to_rhai_error(buildah::bah_list())
}
/// Build an image with options specified in a Map
@@ -167,14 +185,14 @@ pub fn list() -> Result<CommandResult, Box<EvalAltResult>> {
/// # Example
///
/// ```rhai
/// let options = buildah_new_build_options();
/// let options = bah_new_build_options();
/// options.tag = "my-image:latest";
/// options.context_dir = ".";
/// options.file = "Dockerfile";
/// options.isolation = "chroot";
/// let result = buildah_build(options);
/// let result = bah_build(options);
/// ```
pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
pub fn bah_build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
// Extract options from the map
let tag_option = match options.get("tag") {
Some(tag) => {
@@ -241,7 +259,7 @@ pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResu
let isolation_ref = isolation_option.as_deref();
// Call the buildah build function
buildah_error_to_rhai_error(buildah::build(tag_ref, &context_dir, &file, isolation_ref))
bah_error_to_rhai_error(buildah::bah_build(tag_ref, &context_dir, &file, isolation_ref))
}
//
@@ -252,7 +270,7 @@ pub fn build_with_options(options: Map) -> Result<CommandResult, Box<EvalAltResu
///
/// List images in local storage.
pub fn images() -> Result<Array, Box<EvalAltResult>> {
let images = buildah_error_to_rhai_error(buildah::images())?;
let images = bah_error_to_rhai_error(buildah::images())?;
// Convert Vec<Image> to Rhai Array
let mut array = Array::new();
@@ -267,28 +285,28 @@ pub fn images() -> Result<Array, Box<EvalAltResult>> {
///
/// Remove one or more images.
pub fn image_remove(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::image_remove(image))
bah_error_to_rhai_error(buildah::image_remove(image))
}
/// Wrapper for buildah::image_push
///
/// Push an image to a registry.
pub fn image_push(image: &str, destination: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::image_push(image, destination, tls_verify))
bah_error_to_rhai_error(buildah::image_push(image, destination, tls_verify))
}
/// Wrapper for buildah::image_tag
///
/// Add an additional name to a local image.
pub fn image_tag(image: &str, new_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::image_tag(image, new_name))
bah_error_to_rhai_error(buildah::image_tag(image, new_name))
}
/// Wrapper for buildah::image_pull
///
/// Pull an image from a registry.
pub fn image_pull(image: &str, tls_verify: bool) -> Result<CommandResult, Box<EvalAltResult>> {
buildah_error_to_rhai_error(buildah::image_pull(image, tls_verify))
bah_error_to_rhai_error(buildah::image_pull(image, tls_verify))
}
/// Commit a container to an image with options specified in a Map
@@ -298,11 +316,11 @@ pub fn image_pull(image: &str, tls_verify: bool) -> Result<CommandResult, Box<Ev
/// # Example
///
/// ```rhai
/// let options = buildah_new_commit_options();
/// let options = bah_new_commit_options();
/// options.format = "docker";
/// options.squash = true;
/// options.rm = true;
/// let result = buildah_image_commit("my-container", "my-image:latest", options);
/// let result = bah_image_commit("my-container", "my-image:latest", options);
/// ```
pub fn image_commit_with_options(container: &str, image_name: &str, options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
// Extract options from the map
@@ -354,7 +372,7 @@ pub fn image_commit_with_options(container: &str, image_name: &str, options: Map
let format_ref = format_option.as_deref();
// Call the buildah image_commit function
buildah_error_to_rhai_error(buildah::image_commit(container, image_name, format_ref, squash, rm))
bah_error_to_rhai_error(buildah::image_commit(container, image_name, format_ref, squash, rm))
}
/// Configure a container with options specified in a Map
@@ -364,11 +382,11 @@ pub fn image_commit_with_options(container: &str, image_name: &str, options: Map
/// # Example
///
/// ```rhai
/// let options = buildah_new_config_options();
/// let options = bah_new_config_options();
/// options.author = "John Doe";
/// options.cmd = "echo Hello";
/// options.entrypoint = "/bin/sh -c";
/// let result = buildah_config("my-container", options);
/// let result = bah_config("my-container", options);
/// ```
pub fn config_with_options(container: &str, options: Map) -> Result<CommandResult, Box<EvalAltResult>> {
// Convert Rhai Map to Rust HashMap
@@ -387,5 +405,5 @@ pub fn config_with_options(container: &str, options: Map) -> Result<CommandResul
}
// Call the buildah config function
buildah_error_to_rhai_error(buildah::config(container, config_options))
bah_error_to_rhai_error(buildah::bah_config(container, config_options))
}

View File

@@ -30,25 +30,14 @@ pub use os::{
pub use process::{
register_process_module,
// Run functions
run_command, run_silent, run_with_options, new_run_options,
run, run_silent, run_with_options, new_run_options,
// Process management functions
which, kill, process_list, process_get
};
pub use buildah::{
register_buildah_module,
// Container functions
from, run, run_with_isolation, add, commit, remove, list,
build_with_options, new_build_options,
// Image functions
images, image_remove, image_push, image_tag, image_pull,
image_commit_with_options, new_commit_options,
config_with_options, new_config_options
};
pub use buildah::*;
// Rename copy functions to avoid conflicts
pub use os::copy as os_copy;
pub use buildah::copy as buildah_copy;
/// Register all SAL modules with the Rhai engine
///
@@ -76,7 +65,7 @@ pub fn register(engine: &mut Engine) -> Result<(), Box<rhai::EvalAltResult>> {
process::register_process_module(engine)?;
// Register Buildah module functions
buildah::register_buildah_module(engine)?;
buildah::register_bah_module(engine)?;
// Future modules can be registered here

View File

@@ -31,6 +31,9 @@ pub fn register_os_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
engine.register_fn("file_size", file_size);
engine.register_fn("rsync", rsync);
engine.register_fn("chdir", chdir);
engine.register_fn("file_read", file_read);
engine.register_fn("file_write", file_write);
engine.register_fn("file_write_append", file_write_append);
// Register download functions
engine.register_fn("download", download);
@@ -136,6 +139,27 @@ pub fn chdir(path: &str) -> Result<String, Box<EvalAltResult>> {
os::chdir(path).to_rhai_error()
}
/// Wrapper for os::file_read
///
/// Read the contents of a file.
pub fn file_read(path: &str) -> Result<String, Box<EvalAltResult>> {
os::file_read(path).to_rhai_error()
}
/// Wrapper for os::file_write
///
/// Write content to a file (creates the file if it doesn't exist, overwrites if it does).
pub fn file_write(path: &str, content: &str) -> Result<String, Box<EvalAltResult>> {
os::file_write(path, content).to_rhai_error()
}
/// Wrapper for os::file_write_append
///
/// Append content to a file (creates the file if it doesn't exist).
pub fn file_write_append(path: &str, content: &str) -> Result<String, Box<EvalAltResult>> {
os::file_write_append(path, content).to_rhai_error()
}
//
// Download Function Wrappers
//

View File

@@ -19,9 +19,9 @@ pub fn register_process_module(engine: &mut Engine) -> Result<(), Box<EvalAltRes
register_process_types(engine)?;
// Register run functions
engine.register_fn("run_command", run_command);
engine.register_fn("run", run);
engine.register_fn("run_silent", run_silent);
engine.register_fn("run", run_with_options);
engine.register_fn("run_with_options", run_with_options);
engine.register_fn("new_run_options", new_run_options);
// Register process management functions
@@ -92,7 +92,7 @@ pub fn new_run_options() -> Map {
/// Wrapper for process::run_command
///
/// Run a command or multiline script with arguments.
pub fn run_command(command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
pub fn run(command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
run_error_to_rhai_error(process::run_command(command))
}