Compare commits
3 Commits
fe7a676cac
...
88e4a2a4b1
Author | SHA1 | Date | |
---|---|---|---|
88e4a2a4b1 | |||
8605e08a65 | |||
749c60e131 |
@ -1,7 +1,6 @@
|
||||
use std::process::Command;
|
||||
use std::path::Path;
|
||||
use std::fs;
|
||||
use std::env;
|
||||
use regex::Regex;
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
@ -6,7 +6,6 @@ use redis::Cmd;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::redisclient;
|
||||
use crate::git::git::parse_git_url;
|
||||
|
||||
// Define a custom error type for GitExecutor operations
|
||||
#[derive(Debug)]
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
use rhai::{Engine, EvalAltResult, Array, Dynamic, Map};
|
||||
use std::collections::HashMap;
|
||||
use crate::virt::buildah::{self, BuildahError, Image, Builder, ContentOperations};
|
||||
use crate::virt::buildah::{BuildahError, Image, Builder, ContentOperations};
|
||||
use crate::process::CommandResult;
|
||||
|
||||
/// Register Buildah module functions with the Rhai engine
|
||||
|
@ -67,6 +67,9 @@ pub use crate::text::{
|
||||
dedent, prefix
|
||||
};
|
||||
|
||||
// Re-export TextReplacer functions
|
||||
pub use text::*;
|
||||
|
||||
// Rename copy functions to avoid conflicts
|
||||
pub use os::copy as os_copy;
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
//! This module provides Rhai wrappers for the functions in the Nerdctl module.
|
||||
|
||||
use rhai::{Engine, EvalAltResult, Array, Dynamic, Map};
|
||||
use std::collections::HashMap;
|
||||
use crate::virt::nerdctl::{self, NerdctlError, Image, Container};
|
||||
use crate::process::CommandResult;
|
||||
|
||||
@ -51,52 +50,52 @@ pub fn container_from_image(name: &str, image: &str) -> Result<Container, Box<Ev
|
||||
}
|
||||
|
||||
/// Reset the container configuration to defaults while keeping the name and image
|
||||
pub fn container_reset(mut container: Container) -> Container {
|
||||
pub fn container_reset(container: Container) -> Container {
|
||||
container.reset()
|
||||
}
|
||||
|
||||
/// Add a port mapping to a Container
|
||||
pub fn container_with_port(mut container: Container, port: &str) -> Container {
|
||||
pub fn container_with_port(container: Container, port: &str) -> Container {
|
||||
container.with_port(port)
|
||||
}
|
||||
|
||||
/// Add a volume mount to a Container
|
||||
pub fn container_with_volume(mut container: Container, volume: &str) -> Container {
|
||||
pub fn container_with_volume(container: Container, volume: &str) -> Container {
|
||||
container.with_volume(volume)
|
||||
}
|
||||
|
||||
/// Add an environment variable to a Container
|
||||
pub fn container_with_env(mut container: Container, key: &str, value: &str) -> Container {
|
||||
pub fn container_with_env(container: Container, key: &str, value: &str) -> Container {
|
||||
container.with_env(key, value)
|
||||
}
|
||||
|
||||
/// Set the network for a Container
|
||||
pub fn container_with_network(mut container: Container, network: &str) -> Container {
|
||||
pub fn container_with_network(container: Container, network: &str) -> Container {
|
||||
container.with_network(network)
|
||||
}
|
||||
|
||||
/// Add a network alias to a Container
|
||||
pub fn container_with_network_alias(mut container: Container, alias: &str) -> Container {
|
||||
pub fn container_with_network_alias(container: Container, alias: &str) -> Container {
|
||||
container.with_network_alias(alias)
|
||||
}
|
||||
|
||||
/// Set CPU limit for a Container
|
||||
pub fn container_with_cpu_limit(mut container: Container, cpus: &str) -> Container {
|
||||
pub fn container_with_cpu_limit(container: Container, cpus: &str) -> Container {
|
||||
container.with_cpu_limit(cpus)
|
||||
}
|
||||
|
||||
/// Set memory limit for a Container
|
||||
pub fn container_with_memory_limit(mut container: Container, memory: &str) -> Container {
|
||||
pub fn container_with_memory_limit(container: Container, memory: &str) -> Container {
|
||||
container.with_memory_limit(memory)
|
||||
}
|
||||
|
||||
/// Set restart policy for a Container
|
||||
pub fn container_with_restart_policy(mut container: Container, policy: &str) -> Container {
|
||||
pub fn container_with_restart_policy(container: Container, policy: &str) -> Container {
|
||||
container.with_restart_policy(policy)
|
||||
}
|
||||
|
||||
/// Set health check for a Container
|
||||
pub fn container_with_health_check(mut container: Container, cmd: &str) -> Container {
|
||||
pub fn container_with_health_check(container: Container, cmd: &str) -> Container {
|
||||
container.with_health_check(cmd)
|
||||
}
|
||||
|
||||
@ -145,18 +144,18 @@ pub fn container_with_network_aliases(mut container: Container, aliases: Array)
|
||||
}
|
||||
|
||||
/// Set memory swap limit for a Container
|
||||
pub fn container_with_memory_swap_limit(mut container: Container, memory_swap: &str) -> Container {
|
||||
pub fn container_with_memory_swap_limit(container: Container, memory_swap: &str) -> Container {
|
||||
container.with_memory_swap_limit(memory_swap)
|
||||
}
|
||||
|
||||
/// Set CPU shares for a Container
|
||||
pub fn container_with_cpu_shares(mut container: Container, shares: &str) -> Container {
|
||||
pub fn container_with_cpu_shares(container: Container, shares: &str) -> Container {
|
||||
container.with_cpu_shares(shares)
|
||||
}
|
||||
|
||||
/// Set health check with options for a Container
|
||||
pub fn container_with_health_check_options(
|
||||
mut container: Container,
|
||||
container: Container,
|
||||
cmd: &str,
|
||||
interval: Option<&str>,
|
||||
timeout: Option<&str>,
|
||||
@ -169,12 +168,12 @@ pub fn container_with_health_check_options(
|
||||
}
|
||||
|
||||
/// Set snapshotter for a Container
|
||||
pub fn container_with_snapshotter(mut container: Container, snapshotter: &str) -> Container {
|
||||
pub fn container_with_snapshotter(container: Container, snapshotter: &str) -> Container {
|
||||
container.with_snapshotter(snapshotter)
|
||||
}
|
||||
|
||||
/// Set detach mode for a Container
|
||||
pub fn container_with_detach(mut container: Container, detach: bool) -> Container {
|
||||
pub fn container_with_detach(container: Container, detach: bool) -> Container {
|
||||
container.with_detach(detach)
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! This module provides Rhai wrappers for the functions in the Text module.
|
||||
|
||||
use rhai::{Engine, EvalAltResult, Array, Dynamic, Map, Position};
|
||||
use rhai::{Engine, EvalAltResult, Array, Map, Position};
|
||||
use std::collections::HashMap;
|
||||
use crate::text::{
|
||||
TextReplacer, TextReplacerBuilder,
|
||||
@ -24,39 +24,41 @@ pub fn register_text_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult
|
||||
// Register types
|
||||
register_text_types(engine)?;
|
||||
|
||||
// Register TextReplacer functions
|
||||
// Register TextReplacer constructor
|
||||
engine.register_fn("text_replacer_new", text_replacer_new);
|
||||
|
||||
// Register TextReplacer instance methods
|
||||
engine.register_fn("pattern", builder_pattern);
|
||||
engine.register_fn("replacement", builder_replacement);
|
||||
engine.register_fn("regex", builder_regex);
|
||||
engine.register_fn("case_insensitive", builder_case_insensitive);
|
||||
engine.register_fn("and", builder_and);
|
||||
engine.register_fn("build", builder_build);
|
||||
engine.register_fn("replace", replacer_replace);
|
||||
engine.register_fn("replace_file", replacer_replace_file);
|
||||
engine.register_fn("replace_file_in_place", replacer_replace_file_in_place);
|
||||
engine.register_fn("replace_file_to", replacer_replace_file_to);
|
||||
// Register TextReplacerBuilder instance methods
|
||||
engine.register_fn("pattern", pattern);
|
||||
engine.register_fn("replacement", replacement);
|
||||
engine.register_fn("regex", regex);
|
||||
engine.register_fn("case_insensitive", case_insensitive);
|
||||
engine.register_fn("and", and);
|
||||
engine.register_fn("build", build);
|
||||
|
||||
// Register TemplateBuilder functions
|
||||
// Register TextReplacer instance methods
|
||||
engine.register_fn("replace", replace);
|
||||
engine.register_fn("replace_file", replace_file);
|
||||
engine.register_fn("replace_file_in_place", replace_file_in_place);
|
||||
engine.register_fn("replace_file_to", replace_file_to);
|
||||
|
||||
// Register TemplateBuilder constructor
|
||||
engine.register_fn("template_builder_open", template_builder_open);
|
||||
|
||||
// Register TemplateBuilder instance methods
|
||||
engine.register_fn("add_var", template_add_var_string);
|
||||
engine.register_fn("add_var", template_add_var_int);
|
||||
engine.register_fn("add_var", template_add_var_float);
|
||||
engine.register_fn("add_var", template_add_var_bool);
|
||||
engine.register_fn("add_var", template_add_var_array);
|
||||
engine.register_fn("add_vars", template_add_vars);
|
||||
engine.register_fn("render", template_render);
|
||||
engine.register_fn("render_to_file", template_render_to_file);
|
||||
engine.register_fn("add_var", add_var_string);
|
||||
engine.register_fn("add_var", add_var_int);
|
||||
engine.register_fn("add_var", add_var_float);
|
||||
engine.register_fn("add_var", add_var_bool);
|
||||
engine.register_fn("add_var", add_var_array);
|
||||
engine.register_fn("add_vars", add_vars);
|
||||
engine.register_fn("render", render);
|
||||
engine.register_fn("render_to_file", render_to_file);
|
||||
|
||||
// Register Fix functions
|
||||
// Register Fix functions directly from text module
|
||||
engine.register_fn("name_fix", crate::text::name_fix);
|
||||
engine.register_fn("path_fix", crate::text::path_fix);
|
||||
|
||||
// Register Dedent functions
|
||||
// Register Dedent functions directly from text module
|
||||
engine.register_fn("dedent", crate::text::dedent);
|
||||
engine.register_fn("prefix", crate::text::prefix);
|
||||
|
||||
@ -74,10 +76,6 @@ fn register_text_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
||||
// Register TemplateBuilder type
|
||||
engine.register_type_with_name::<TemplateBuilder>("TemplateBuilder");
|
||||
|
||||
// Register getters for TextReplacer properties (if any)
|
||||
|
||||
// Register getters for TemplateBuilder properties (if any)
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -117,52 +115,52 @@ pub fn text_replacer_new() -> TextReplacerBuilder {
|
||||
}
|
||||
|
||||
/// Sets the pattern to search for
|
||||
pub fn builder_pattern(builder: TextReplacerBuilder, pat: &str) -> TextReplacerBuilder {
|
||||
pub fn pattern(builder: TextReplacerBuilder, pat: &str) -> TextReplacerBuilder {
|
||||
builder.pattern(pat)
|
||||
}
|
||||
|
||||
/// Sets the replacement text
|
||||
pub fn builder_replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder {
|
||||
pub fn replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder {
|
||||
builder.replacement(rep)
|
||||
}
|
||||
|
||||
/// Sets whether to use regex
|
||||
pub fn builder_regex(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
|
||||
pub fn regex(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
|
||||
builder.regex(yes)
|
||||
}
|
||||
|
||||
/// Sets whether the replacement should be case-insensitive
|
||||
pub fn builder_case_insensitive(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
|
||||
pub fn case_insensitive(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
|
||||
builder.case_insensitive(yes)
|
||||
}
|
||||
|
||||
/// Adds another replacement operation to the chain and resets the builder for a new operation
|
||||
pub fn builder_and(builder: TextReplacerBuilder) -> TextReplacerBuilder {
|
||||
pub fn and(builder: TextReplacerBuilder) -> TextReplacerBuilder {
|
||||
builder.and()
|
||||
}
|
||||
|
||||
/// Builds the TextReplacer with all configured replacement operations
|
||||
pub fn builder_build(builder: TextReplacerBuilder) -> Result<TextReplacer, Box<EvalAltResult>> {
|
||||
pub fn build(builder: TextReplacerBuilder) -> Result<TextReplacer, Box<EvalAltResult>> {
|
||||
string_error_to_rhai_error(builder.build())
|
||||
}
|
||||
|
||||
/// Applies all configured replacement operations to the input text
|
||||
pub fn replacer_replace(replacer: &mut TextReplacer, input: &str) -> String {
|
||||
pub fn replace(replacer: &mut TextReplacer, input: &str) -> String {
|
||||
replacer.replace(input)
|
||||
}
|
||||
|
||||
/// Reads a file, applies all replacements, and returns the result as a string
|
||||
pub fn replacer_replace_file(replacer: &mut TextReplacer, path: &str) -> Result<String, Box<EvalAltResult>> {
|
||||
pub fn replace_file(replacer: &mut TextReplacer, path: &str) -> Result<String, Box<EvalAltResult>> {
|
||||
io_error_to_rhai_error(replacer.replace_file(path))
|
||||
}
|
||||
|
||||
/// Reads a file, applies all replacements, and writes the result back to the file
|
||||
pub fn replacer_replace_file_in_place(replacer: &mut TextReplacer, path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn replace_file_in_place(replacer: &mut TextReplacer, path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
io_error_to_rhai_error(replacer.replace_file_in_place(path))
|
||||
}
|
||||
|
||||
/// Reads a file, applies all replacements, and writes the result to a new file
|
||||
pub fn replacer_replace_file_to(replacer: &mut TextReplacer, input_path: &str, output_path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn replace_file_to(replacer: &mut TextReplacer, input_path: &str, output_path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
io_error_to_rhai_error(replacer.replace_file_to(input_path, output_path))
|
||||
}
|
||||
|
||||
@ -174,29 +172,29 @@ pub fn template_builder_open(template_path: &str) -> Result<TemplateBuilder, Box
|
||||
}
|
||||
|
||||
/// Adds a string variable to the template context
|
||||
pub fn template_add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder {
|
||||
pub fn add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder {
|
||||
builder.add_var(name, value)
|
||||
}
|
||||
|
||||
/// Adds an integer variable to the template context
|
||||
pub fn template_add_var_int(builder: TemplateBuilder, name: &str, value: i64) -> TemplateBuilder {
|
||||
pub fn add_var_int(builder: TemplateBuilder, name: &str, value: i64) -> TemplateBuilder {
|
||||
builder.add_var(name, value)
|
||||
}
|
||||
|
||||
/// Adds a float variable to the template context
|
||||
pub fn template_add_var_float(builder: TemplateBuilder, name: &str, value: f64) -> TemplateBuilder {
|
||||
pub fn add_var_float(builder: TemplateBuilder, name: &str, value: f64) -> TemplateBuilder {
|
||||
builder.add_var(name, value)
|
||||
}
|
||||
|
||||
/// Adds a boolean variable to the template context
|
||||
pub fn template_add_var_bool(builder: TemplateBuilder, name: &str, value: bool) -> TemplateBuilder {
|
||||
pub fn add_var_bool(builder: TemplateBuilder, name: &str, value: bool) -> TemplateBuilder {
|
||||
builder.add_var(name, value)
|
||||
}
|
||||
|
||||
/// Adds an array variable to the template context
|
||||
pub fn template_add_var_array(builder: TemplateBuilder, name: &str, value: Array) -> TemplateBuilder {
|
||||
pub fn add_var_array(builder: TemplateBuilder, name: &str, array: Array) -> TemplateBuilder {
|
||||
// Convert Rhai Array to Vec<String>
|
||||
let vec: Vec<String> = value.iter()
|
||||
let vec: Vec<String> = array.iter()
|
||||
.filter_map(|v| v.clone().into_string().ok())
|
||||
.collect();
|
||||
|
||||
@ -204,7 +202,7 @@ pub fn template_add_var_array(builder: TemplateBuilder, name: &str, value: Array
|
||||
}
|
||||
|
||||
/// Adds multiple variables to the template context from a Map
|
||||
pub fn template_add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder {
|
||||
pub fn add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder {
|
||||
// Convert Rhai Map to Rust HashMap
|
||||
let mut hash_map = HashMap::new();
|
||||
|
||||
@ -219,11 +217,11 @@ pub fn template_add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder
|
||||
}
|
||||
|
||||
/// Renders the template with the current context
|
||||
pub fn template_render(builder: &mut TemplateBuilder) -> Result<String, Box<EvalAltResult>> {
|
||||
pub fn render(builder: &mut TemplateBuilder) -> Result<String, Box<EvalAltResult>> {
|
||||
tera_error_to_rhai_error(builder.render())
|
||||
}
|
||||
|
||||
/// Renders the template and writes the result to a file
|
||||
pub fn template_render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
pub fn render_to_file(builder: &mut TemplateBuilder, output_path: &str) -> Result<(), Box<EvalAltResult>> {
|
||||
io_error_to_rhai_error(builder.render_to_file(output_path))
|
||||
}
|
@ -6,7 +6,7 @@ println("===== TextReplacer Examples =====");
|
||||
|
||||
// Create a temporary file for testing
|
||||
let temp_file = "text_replacer_test.txt";
|
||||
write_file(temp_file, "This is a foo bar example with FOO and foo occurrences.\nAnother line with foo and bar.");
|
||||
file_write(temp_file, "This is a foo bar example with FOO and foo occurrences.\nAnother line with foo and bar.");
|
||||
|
||||
// Example 1: Simple replacement
|
||||
println("\n--- Example 1: Simple replacement ---");
|
||||
@ -68,7 +68,7 @@ println("\n\n===== TemplateBuilder Examples =====");
|
||||
|
||||
// Create a temporary template file
|
||||
let template_file = "template_test.txt";
|
||||
write_file(template_file, "Hello, {{ name }}! Welcome to {{ place }}.\n{% if show_greeting %}Glad to have you here!{% endif %}\nYour items:\n{% for item in items %} - {{ item }}{% if not loop.last %}\n{% endif %}{% endfor %}\n");
|
||||
file_write(template_file, "Hello, {{ name }}! Welcome to {{ place }}.\n{% if show_greeting %}Glad to have you here!{% endif %}\nYour items:\n{% for item in items %} - {{ item }}{% if not loop.last %}\n{% endif %}{% endfor %}\n");
|
||||
|
||||
// Example 1: Simple template rendering
|
||||
println("\n--- Example 1: Simple template rendering ---");
|
||||
@ -108,7 +108,7 @@ let template = template_builder_open(template_file)
|
||||
|
||||
template.render_to_file(output_file);
|
||||
println(`Template rendered to file: ${output_file}`);
|
||||
println(`Content of the rendered file:\n${read_file(output_file)}`);
|
||||
println(`Content of the rendered file:\n${file_read(output_file)}`);
|
||||
|
||||
// Clean up temporary files
|
||||
delete(template_file);
|
||||
|
@ -1,6 +1,6 @@
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::io::{self, Read, Seek, SeekFrom};
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
/// Represents the type of replacement to perform.
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Basic buildah operations for container management
|
||||
use std::process::Command;
|
||||
use crate::process::CommandResult;
|
||||
use super::{BuildahError, Builder};
|
||||
use super::BuildahError;
|
||||
|
||||
|
||||
/// Execute a buildah command and return the result
|
||||
|
@ -1,10 +1,9 @@
|
||||
// File: /root/code/git.ourworld.tf/herocode/sal/src/virt/nerdctl/container.rs
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::process::CommandResult;
|
||||
use crate::virt::nerdctl::{execute_nerdctl_command, NerdctlError};
|
||||
use crate::os;
|
||||
use super::container_types::{Container, HealthCheck, ContainerStatus, ResourceUsage};
|
||||
use super::container_types::Container;
|
||||
|
||||
impl Container {
|
||||
/// Create a new container reference with the given name
|
||||
|
@ -12,7 +12,7 @@ impl Container {
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Self` - The container instance for method chaining
|
||||
pub fn reset(mut self) -> Self {
|
||||
pub fn reset(self) -> Self {
|
||||
let name = self.name;
|
||||
let image = self.image.clone();
|
||||
|
||||
|
@ -1,10 +1,9 @@
|
||||
// File: /root/code/git.ourworld.tf/herocode/sal/src/virt/nerdctl/images.rs
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::virt::nerdctl::execute_nerdctl_command;
|
||||
use crate::process::CommandResult;
|
||||
use super::NerdctlError;
|
||||
use serde_json::{self, Value};
|
||||
use serde_json::{self};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Represents a container image
|
||||
|
Loading…
Reference in New Issue
Block a user