feat: Enhance documentation and add .gitignore entries
- Add new documentation sections for PostgreSQL installer functions and usage examples. Improves clarity and completeness of the documentation. - Add new files and patterns to .gitignore to prevent unnecessary files from being committed to the repository. Improves repository cleanliness and reduces clutter.
This commit is contained in:
@@ -116,7 +116,7 @@ pub use os::copy as os_copy;
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// ```ignore
|
||||
/// use rhai::Engine;
|
||||
/// use sal::rhai;
|
||||
///
|
||||
@@ -124,7 +124,8 @@ pub use os::copy as os_copy;
|
||||
/// rhai::register(&mut engine);
|
||||
///
|
||||
/// // Now you can use SAL functions in Rhai scripts
|
||||
/// let result = engine.eval::<bool>("exist('some_file.txt')").unwrap();
|
||||
/// // You can evaluate Rhai scripts with SAL functions
|
||||
/// let result = engine.eval::<i64>("exist('some_file.txt')").unwrap();
|
||||
/// ```
|
||||
pub fn register(engine: &mut Engine) -> Result<(), Box<rhai::EvalAltResult>> {
|
||||
// Register OS module functions
|
||||
|
@@ -26,6 +26,12 @@ pub fn register_postgresclient_module(engine: &mut Engine) -> Result<(), Box<Eva
|
||||
engine.register_fn("pg_query", pg_query);
|
||||
engine.register_fn("pg_query_one", pg_query_one);
|
||||
|
||||
// Register installer functions
|
||||
engine.register_fn("pg_install", pg_install);
|
||||
engine.register_fn("pg_create_database", pg_create_database);
|
||||
engine.register_fn("pg_execute_sql", pg_execute_sql);
|
||||
engine.register_fn("pg_is_running", pg_is_running);
|
||||
|
||||
// Builder pattern functions will be implemented in a future update
|
||||
|
||||
Ok(())
|
||||
@@ -180,3 +186,171 @@ pub fn pg_query_one(query: &str) -> Result<Map, Box<EvalAltResult>> {
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Install PostgreSQL using nerdctl
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `container_name` - Name for the PostgreSQL container
|
||||
/// * `version` - PostgreSQL version to install (e.g., "latest", "15", "14")
|
||||
/// * `port` - Port to expose PostgreSQL on
|
||||
/// * `username` - Username for PostgreSQL
|
||||
/// * `password` - Password for PostgreSQL
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<bool, Box<EvalAltResult>>` - true if successful, error otherwise
|
||||
pub fn pg_install(
|
||||
container_name: &str,
|
||||
version: &str,
|
||||
port: i64,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> Result<bool, Box<EvalAltResult>> {
|
||||
// Create the installer configuration
|
||||
let config = postgresclient::PostgresInstallerConfig::new()
|
||||
.container_name(container_name)
|
||||
.version(version)
|
||||
.port(port as u16)
|
||||
.username(username)
|
||||
.password(password);
|
||||
|
||||
// Install PostgreSQL
|
||||
match postgresclient::install_postgres(config) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("PostgreSQL installer error: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new database in PostgreSQL
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `container_name` - Name of the PostgreSQL container
|
||||
/// * `db_name` - Database name to create
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<bool, Box<EvalAltResult>>` - true if successful, error otherwise
|
||||
pub fn pg_create_database(container_name: &str, db_name: &str) -> Result<bool, Box<EvalAltResult>> {
|
||||
// Create a container reference
|
||||
let container = crate::virt::nerdctl::Container {
|
||||
name: container_name.to_string(),
|
||||
container_id: Some(container_name.to_string()), // Use name as ID for simplicity
|
||||
image: None,
|
||||
config: std::collections::HashMap::new(),
|
||||
ports: Vec::new(),
|
||||
volumes: Vec::new(),
|
||||
env_vars: std::collections::HashMap::new(),
|
||||
network: None,
|
||||
network_aliases: Vec::new(),
|
||||
cpu_limit: None,
|
||||
memory_limit: None,
|
||||
memory_swap_limit: None,
|
||||
cpu_shares: None,
|
||||
restart_policy: None,
|
||||
health_check: None,
|
||||
detach: false,
|
||||
snapshotter: None,
|
||||
};
|
||||
|
||||
// Create the database
|
||||
match postgresclient::create_database(&container, db_name) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("PostgreSQL error: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a SQL script in PostgreSQL
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `container_name` - Name of the PostgreSQL container
|
||||
/// * `db_name` - Database name
|
||||
/// * `sql` - SQL script to execute
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<String, Box<EvalAltResult>>` - Output of the command if successful, error otherwise
|
||||
pub fn pg_execute_sql(
|
||||
container_name: &str,
|
||||
db_name: &str,
|
||||
sql: &str,
|
||||
) -> Result<String, Box<EvalAltResult>> {
|
||||
// Create a container reference
|
||||
let container = crate::virt::nerdctl::Container {
|
||||
name: container_name.to_string(),
|
||||
container_id: Some(container_name.to_string()), // Use name as ID for simplicity
|
||||
image: None,
|
||||
config: std::collections::HashMap::new(),
|
||||
ports: Vec::new(),
|
||||
volumes: Vec::new(),
|
||||
env_vars: std::collections::HashMap::new(),
|
||||
network: None,
|
||||
network_aliases: Vec::new(),
|
||||
cpu_limit: None,
|
||||
memory_limit: None,
|
||||
memory_swap_limit: None,
|
||||
cpu_shares: None,
|
||||
restart_policy: None,
|
||||
health_check: None,
|
||||
detach: false,
|
||||
snapshotter: None,
|
||||
};
|
||||
|
||||
// Execute the SQL script
|
||||
match postgresclient::execute_sql(&container, db_name, sql) {
|
||||
Ok(output) => Ok(output),
|
||||
Err(e) => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("PostgreSQL error: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if PostgreSQL is running
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `container_name` - Name of the PostgreSQL container
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<bool, Box<EvalAltResult>>` - true if running, false otherwise, or error
|
||||
pub fn pg_is_running(container_name: &str) -> Result<bool, Box<EvalAltResult>> {
|
||||
// Create a container reference
|
||||
let container = crate::virt::nerdctl::Container {
|
||||
name: container_name.to_string(),
|
||||
container_id: Some(container_name.to_string()), // Use name as ID for simplicity
|
||||
image: None,
|
||||
config: std::collections::HashMap::new(),
|
||||
ports: Vec::new(),
|
||||
volumes: Vec::new(),
|
||||
env_vars: std::collections::HashMap::new(),
|
||||
network: None,
|
||||
network_aliases: Vec::new(),
|
||||
cpu_limit: None,
|
||||
memory_limit: None,
|
||||
memory_swap_limit: None,
|
||||
cpu_shares: None,
|
||||
restart_policy: None,
|
||||
health_check: None,
|
||||
detach: false,
|
||||
snapshotter: None,
|
||||
};
|
||||
|
||||
// Check if PostgreSQL is running
|
||||
match postgresclient::is_postgres_running(&container) {
|
||||
Ok(running) => Ok(running),
|
||||
Err(e) => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("PostgreSQL error: {}", e).into(),
|
||||
rhai::Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user