61 lines
1.8 KiB
Rust
61 lines
1.8 KiB
Rust
// Basic buildah operations for container management
|
|
use std::process::Command;
|
|
use rhai::{Dynamic, Map};
|
|
|
|
/// A structure to hold command execution results
|
|
#[derive(Clone)]
|
|
pub struct CommandResult {
|
|
pub stdout: String,
|
|
pub stderr: String,
|
|
pub success: bool,
|
|
pub code: i32,
|
|
}
|
|
|
|
impl CommandResult {
|
|
/// Create a result map from CommandResult
|
|
pub fn to_dynamic(&self) -> Dynamic {
|
|
let mut result = Map::new();
|
|
result.insert("stdout".into(), Dynamic::from(self.stdout.clone()));
|
|
result.insert("stderr".into(), Dynamic::from(self.stderr.clone()));
|
|
result.insert("success".into(), Dynamic::from(self.success));
|
|
result.insert("code".into(), Dynamic::from(self.code));
|
|
Dynamic::from(result)
|
|
}
|
|
|
|
/// Create a default failed result with an error message
|
|
pub fn error(message: &str) -> Self {
|
|
Self {
|
|
stdout: String::new(),
|
|
stderr: message.to_string(),
|
|
success: false,
|
|
code: -1,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Execute a buildah command and return the result
|
|
pub fn execute_buildah_command(args: &[&str]) -> Dynamic {
|
|
let output = Command::new("buildah")
|
|
.args(args)
|
|
.output();
|
|
|
|
match output {
|
|
Ok(output) => {
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let result = CommandResult {
|
|
stdout,
|
|
stderr,
|
|
success: output.status.success(),
|
|
code: output.status.code().unwrap_or(-1),
|
|
};
|
|
|
|
result.to_dynamic()
|
|
},
|
|
Err(e) => {
|
|
CommandResult::error(&format!("Failed to execute buildah command: {}", e)).to_dynamic()
|
|
}
|
|
}
|
|
}
|