This commit is contained in:
2025-04-05 09:56:10 +02:00
parent 78db13d738
commit 4be9445702
8 changed files with 185 additions and 16 deletions

View File

@@ -11,6 +11,8 @@ pub struct Builder {
container_id: Option<String>,
/// Base image
image: String,
/// Debug mode
debug: bool,
}
impl Builder {
@@ -37,6 +39,7 @@ impl Builder {
name: name.to_string(),
container_id: Some(container_id),
image: image.to_string(),
debug: false,
})
},
Err(BuildahError::CommandFailed(error_msg)) => {
@@ -58,6 +61,7 @@ impl Builder {
name: name.to_string(),
container_id: Some(container_id),
image: image.to_string(),
debug: false,
})
} else {
// Couldn't extract container ID
@@ -85,6 +89,17 @@ impl Builder {
&self.name
}
/// Get the debug mode
pub fn debug(&self) -> bool {
self.debug
}
/// Set the debug mode
pub fn set_debug(&mut self, debug: bool) -> &mut Self {
self.debug = debug;
self
}
/// Get the base image
pub fn image(&self) -> &str {
&self.image

View File

@@ -0,0 +1,70 @@
use crate::process::CommandResult;
use crate::virt::buildah::{execute_buildah_command, BuildahError};
use std::fs::File;
use std::io::{Read, Write};
use tempfile::NamedTempFile;
/// Functions for working with file content in buildah containers
pub struct ContentOperations;
impl ContentOperations {
/// Write content to a file in the container
///
/// # Arguments
///
/// * `container_id` - The container ID
/// * `content` - The content to write
/// * `dest_path` - Destination path in the container
///
/// # Returns
///
/// * `Result<CommandResult, BuildahError>` - Command result or error
pub fn write_content(container_id: &str, content: &str, dest_path: &str) -> Result<CommandResult, BuildahError> {
// Create a temporary file
let mut temp_file = NamedTempFile::new()
.map_err(|e| BuildahError::Other(format!("Failed to create temporary file: {}", e)))?;
// Write content to the temporary file
temp_file.write_all(content.as_bytes())
.map_err(|e| BuildahError::Other(format!("Failed to write to temporary file: {}", e)))?;
// Flush the file to ensure content is written
temp_file.flush()
.map_err(|e| BuildahError::Other(format!("Failed to flush temporary file: {}", e)))?;
// Copy the temporary file to the container
let temp_path = temp_file.path().to_string_lossy().to_string();
execute_buildah_command(&["copy", container_id, &temp_path, dest_path])
}
/// Read content from a file in the container
///
/// # Arguments
///
/// * `container_id` - The container ID
/// * `source_path` - Source path in the container
///
/// # Returns
///
/// * `Result<String, BuildahError>` - File content or error
pub fn read_content(container_id: &str, source_path: &str) -> Result<String, BuildahError> {
// Create a temporary file
let temp_file = NamedTempFile::new()
.map_err(|e| BuildahError::Other(format!("Failed to create temporary file: {}", e)))?;
let temp_path = temp_file.path().to_string_lossy().to_string();
// Copy the file from the container to the temporary file
execute_buildah_command(&["copy", container_id, source_path, &temp_path])?;
// Read the content from the temporary file
let mut file = File::open(temp_file.path())
.map_err(|e| BuildahError::Other(format!("Failed to open temporary file: {}", e)))?;
let mut content = String::new();
file.read_to_string(&mut content)
.map_err(|e| BuildahError::Other(format!("Failed to read from temporary file: {}", e)))?;
Ok(content)
}
}

View File

@@ -2,6 +2,7 @@ mod containers;
mod images;
mod cmd;
mod builder;
mod content;
#[cfg(test)]
mod containers_test;
@@ -52,4 +53,5 @@ pub use builder::Builder;
pub use containers::*;
#[deprecated(since = "0.2.0", note = "Use Builder methods instead")]
pub use images::*;
pub use cmd::*;
pub use cmd::*;
pub use content::ContentOperations;